Git Basic Commands
Git Basic Commands
Clone a repository
git clone https://gitlab.com/someuser/my-test-repo.git
Configure repository
Set user.name
git config user.name "someuser"
Set user.email
git config user.email some.user@example.org
Set pull.rebase
git config pull.rebase false
Set push.default
git config push.default simple
Set credential cache
For Unix/Linux
Cache for 600 seconds
git config credential.helper ‘cache --timeout=600’
For Mac OSX
git config credential.helper osxkeychain
For Windows
git config credential.helper wincred
Set core.longpaths (for windows)
git config core.longpaths true
Set http and https proxy
git config http.proxy http.proxy http://proxyuser:proxypwd@proxy.server.com:8080
git config https.proxy http.proxy http://proxyuser:proxypwd@proxy.server.com:8080
Get http and https proxy
git config --get http.proxy
git config --get https.proxy
Unset http and https proxy
git config --unset http.proxy
git config --unset https.proxy
List all config
git config --list
Global property
Use --global flag to set any property at global level instead of repository level
git config --global user.name "someuser"
git config --global user.email some.user@example.org
Unset a property
git config --unset user.name
Check status
git status
Check remotes
git remote
git remote --verbose
Remove remote
git remote remove origin
Add remote
git remote add origin git@example.com
Change url of existing remote
git remote set-url origin git@new.example.com
Check branch
git branch
Ignore files and directories
Create a .gitignore file in the root directory or any sub directory and add the following common values:
# Maven build directory
target/
# Log files
*.log
# IntelliJ files
.idea/
*.iml
*.ipr
*.iws
# Eclipse files
.settings/
.loadpath
.project
.classpath
.factorypath
Check in empty directory
Create a .gitkeep file in the empty directory to inform git to check in an empty directory
Stage files or directories to commit
Stage specific file or directory
git add .gitignore
git add test-directory/
Stage all files or directories
git add --all
Unstage specific files or directories from commit
Unstage specific file or directory
git reset HEAD .gitignore
git reset HEAD test-directory/
Unstage all files or directories from commit
git reset HEAD
Local commit
Commit with message through vi:
git commit
Commit with inline message (concatenated as paragraph):
git commit -m "JIRA story: some short description" -m "Long description 1" -m "Long description 2"
Revert local uncommitted changes
Revert specific file or directory
Staged file:
git reset HEAD test-directory/SomeFile.txt && git checkout -- test-directory/SomeFile.txt
Unstaged file:
git checkout -- test-directory/SomeFile.txt
Untracked file:
git clean -df test-directory/NewFile.txt
Revert all files or directories
Reverts all changes including staged, unstaged and untracked
git reset --hard HEAD && git clean -df
Revert pushed changes through new commit
With commit message
git revert {commit_id}
With default commit
git revert --no-edit {commit_id}
Without commit and keep changes
git revert --no-commit {commit_id}Delete files or directories
File:
git rm test-directory/NewFile.txt
Directory:
git rm -r test-directory/
Push to remote branch from local branch
git push
Pull from remote branch to local branch (as merge)
git pull
Pull from remote branch to local branch (as rebase)
git pull -r
Fetch changes of remote repo
git fetch origin
List branches
Local branches
git branch
Remote branches
git branch -r
All branches
git branch -a
All non-merged branches
git branch --no-merged
Tracked branches
git branch -vv
Create local branch
From master
git checkout master
git branch my_new_branch
From a local branch
git checkout my_new_branch
git branch another_new_branch
From a remote branch
git fetch origin
git branch my_new_remote_branch origin/my_new_remote_branch
From commit SHA
git branch new_branch_from_commit_id 826410607c5d7a1f485c22850e3262fe86228271
From tag
git fetch origin
git branch new_branch_from_tag tag_name
Switch to a local branch
git checkout my_new_branch
Create a remote branch from local branch
git checkout my_new_branch
git push --set-upstream origin my_new_branch
Merge local source branch to local target branch
Merge changes from my_new_branch to master:
git checkout master
git merge my_new_branch
git merge --squash my_new_branch
Merge changes with squash from my_new_branch to master:
git checkout mastergit merge --squash my_new_branch
Rebase local source branch to local target branch
Merge changes from my_new_branch to master using rebase to maintain linear commit history:
git checkout my_new_branch
git rebase master
git checkout master
git merge my_new_branch
Create a patch from branch and apply it to master
git diff master feature-branch > changes.patch
git apply -v changes.patch
Delete commits
Undo last commit
git reset --hard HEAD~1
Note: use --soft to undo the commit and keep the changes
git reset --soft HEAD~1
Undo last 3 commits
git reset --hard HEAD~3
Undo all local commits not pushed
git reset --hard origin/master
Undo a merge
git reset --hard HEAD~1
Abort a merge with conflict
git reset --hard HEAD && git clean -df
Rebasing (linear commits)
Rebase with a branch
git rebase master
Fix conflict and continue
git add . && git rebase --continue
Abort a rebase with conflict
git rebase --abort
Stash, Apply and Pop
Stash uncommitted changes (staged and unstaged)
git stash
List stashed changes
git stash list
Apply recently stashed change
git stash apply --index
Apply change by name
git stash apply --index stash@{2}
Apply change and clear from stash
git stash pop --index
Drop changes from stash
git stash drop stash@{2}
Cherrypick commit
Cherry pick single commit based on commit id (from branch or tag)
git checkout master
git cherry-pick bf644cbacf313340b975a091f2e786d3242e9404
Cherry pick single commit based on commit id (without creating a commit on current branch)
git checkout master
git cherry-pick -n bf644cbacf313340b975a091f2e786d3242e9404
Cherry pick multiple commits (commit ids separated by space)
git checkout master
git cherry-pick bf644cbacf313340b975a091f2e786d3242e9404 73b05ca6c776361cabe6e1eb22e3993be6e656f7
Cherry pick a merge commit which is squashed
git checkout master
git cherry-pick -m 1 bf644cbacf313340b975a091f2e786d3242e9404
Cherry pick a merge commit which is not squashed
git checkout master
git cherry-pick -m 3 bf644cbacf313340b975a091f2e786d3242e9404
Delete branches
Local branch
Fully merged local branch
git checkout master
git branch -d my_fully_merged_local_branch
Unmerged local branch
git checkout master
git branch -D my_unmerged_local_branch
Remote branch
git fetch origin --prune
git push origin --delete my_remote_branch_on_origin
Clean up local obsolete branches
git fetch origin --prune
git branch -vv
Watch out for the branches with ‘gone’ in the remote reference which means the branch doesn’t exist in origin
my_remote_branch 9f9655d [origin/my_remote_branch: gone] Fourth change on branch
git branch -d my_remote_branch
Create tags
Local tag from commit SHA
git tag -a v1.5.0 d4b25fc268fba0e86cb5ecbb5c51faae1846ad61 -m "Short description" -m "Long description"
Local tag from local branch
git tag -a v1.5.2 my_local_branch -m "Tag from a branch" -m "Creating a tag from a branch"
Push local tag to remote repository
Push a single tag
git push origin v1.5.0
Push all tags
git push origin --tags
List tags
Local tags
Show only tag names
git tag --sort=creatordate
Show tag names with created date
git for-each-ref --sort=creatordate --format '%(refname:short) %(creatordate)' refs/tags
Show tags along with dereference (commit id referenced by the tag)
git show-ref --tags -d
Remote tags
git ls-remote --tags --sort=creatordate origin
Delete tags
Local tag
git tag -d v1.5.0
Remote tag
git push origin --delete v1.5.0
Synchronize local tags with remote tags
git fetch --prune origin +refs/tags/*:refs/tags/*
Display commit details
By tag
git show v1.5.0
By commit id
git show d4b25fc268fba0e86cb5ecbb5c51faae1846ad61
Display history
For a branch
Full history
git checkout master
git log
Use keyboard pointer to scroll up and down, and press ‘q’ to exit
Last 10 commits
git log -n 10
Between two dates
git log --before=2016-01-24 --after=2016-01-19
For all branches with tags
git log --all --decorate
As a network / graph
git log --all --graph --decorate
Between two commits
Commit ids to be separated by three dots (...)
git log --all --graph --decorate c7cc3ef69857610f5bbf92a21fd5bc765ee047f5...d4fb3cc5647ebbf240d55856d6ee49db8a486a76
View differences and commits
View differences
git diff "old_commit".."new_commit"
View commits
git log "old_commit".."new_commit"
Create and Apply patch
Create a patch
- Go to the root directory of git
- Add all changes to staged (note . at the end) by git add -A .
- Create a patch from diff by git diff --cached > some_directory/some_file_name.patch
Apply a patch
- Go to root folder of git
- Apply a patch by git apply some_directory/some_file_name.patch
Update all repos in a given directory
ls | xargs -I{} echo "echo {} && git -C {} pull" | bash
Checkout main branch in all repos in a given directory
ls | xargs -I{} echo "echo {} && git -C {} checkout main" | bash
Print the current branch of all repositories
ls | xargs -I{} echo "printf \"{} \" && git -C {} branch --show-current" | bash
Print the locally modified files in all repositories
ls | xargs -I{} echo "printf \"{} \" && git -C {} status -s | wc -l" | bash
Delete all local branches in a repository whose remote branch is deleted
git fetch --prune && git branch -vv | grep 'gone]' | cut -d ' ' -f3 | xargs git branch -D
Comments
Post a Comment