Helpful Git commands
Here is a list of helpful HOWTO Git commands. Please follow/star this github repository for updates.
- How to Everyday Git in twenty commands or so
- How to Show helpful guides that come with Git
- How to Overwrite pull
- How to List of all files till a commit
- How to Git reset first commit
- How to List all the conflicted files
- How to List of all files changed in a commit
- How to Unstaged changes since last commit
- How to Changes staged for commit
- How to Show both staged and unstaged changes
- How to List all branches that are already merged into master
- How to Quickly switch to the previous branch
- How to Remove branches that have already been merged with master
- How to List all branches and their upstreams, as well as last commit on branch
- How to Track upstream branch
- How to Delete local branch
- How to Delete remote branch
- How to Undo local changes with the last content in head
- How to Revert: Undo a commit by creating a new commit
- How to Reset: Discard commits, advised for private branch
- How to Reword the previous commit message
- How to See commit history for just the current branch
- How to Amend author.
- How to Reset author, after author has been changed in the global config.
- How to Changing a remote’s URL
- How to Get list of all remote references
- How to Get list of all local and remote branches
- How to Get only remote branches
- How to Stage parts of a changed file, instead of the entire file
- How to Get git bash completion
- How to What changed since two weeks?
- How to See all commits made since forking from master
- How to Pick commits across branches using cherry-pick
- How to Find out branches containing commit-hash
- How to Git Aliases
- How to Saving current state of tracked files without commiting
- How to Saving current state including untracked files
- How to Show list of all saved stashes
- How to Apply any stash without deleting from the stashed list
- How to Apply last stashed state and delete it from stashed list
- How to Delete all stored stashes
- How to Grab a single file from a stash
- How to Show all tracked files
- How to Show all untracked files
- How to Show all ignored files
- How to Create new working tree from a repository (git 2.5)
- How to Create new working tree from HEAD state
- How to Untrack files without deleting
- How to Before deleting untracked files/directory, do a dry run to get the list of these files/directories
- How to Forcefully remove untracked files
- How to Forcefully remove untracked directory
- How to Update all the submodules
- How to Show all commits in the current branch yet to be merged to master
- How to Rename a branch
- How to rebases ‘feature’ to ‘master’ and merges it in to master
- How to Archive the
master
branch - How to Modify previous commit without modifying the commit message
- How to Prunes references to remote branches that have been deleted in the remote.
- How to Retrieve the commit hash of the initial revision.
- How to Visualize the version tree.
- How to Deploying git tracked subfolder to gh-pages
- How to Adding a project to repo using subtree
- How to Get latest changes in your repo for a linked project using subtree
- How to Export a branch with history to a file.
- How to Import from a bundle
- How to Get the name of current branch.
- How to Ignore one file on commit (e.g. Changelog).
- How to Stash changes before rebasing
- How to Fetch pull request by ID to a local branch
- How to Show the most recent tag on the current branch.
- How to Show inline word diff.
- How to Don’t consider changes for tracked file.
- How to Undo assume-unchanged.
- How to Clean the files from
.gitignore
. - How to Restore deleted file.
- How to Restore file to a specific commit-hash
- How to Always rebase instead of merge on pull.
- How to List all the alias and configs.
- How to Make git case sensitive.
- How to Auto correct typos.
- How to Check if the change was a part of a release.
- How to Dry run. (any command that supports dry-run flag should do.)
- How to Marks your commit as a fix of a previous commit.
- How to squash fixup commits normal commits.
- How to skip staging area during commit.
- How to List ignored files.
- How to Status of ignored files.
- How to Commits in Branch1 that are not in Branch2
- How to reuse recorded resolution, record and reuse previous conflicts resolutions.
- How to Open all conflicted files in an editor.
- How to Count unpacked number of objects and their disk consumption.
- How to Prune all unreachable objects from the object database.
- How to Instantly browse your working repository in gitweb.
- How to View the GPG signatures in the commit log
- How to Remove entry in the global config.
- How to Checkout a new branch without any history
- How to Extract file from another branch.
- How to List only the root and merge commits.
- How to Merge previous two commits into one.
- How to List all branch is WIP
- How to Find guilty with binary search
- How to Bypass pre-commit and commit-msg githooks
- How to List commits and changes to a specific file (even through renaming)
- How to Clone a single branch
- How to Create and switch new branch
- How to Ignore file mode changes on commits
- How to Turn off git colored terminal output
- How to specific color settings
Everyday Git in twenty commands or so
1 |
git <span class="pl-c1">help</span> everyday |
Show helpful guides that come with Git
1 |
git <span class="pl-c1">help</span> -g |
Overwrite pull
1 |
git fetch --all <span class="pl-k">&&</span> git reset --hard origin/master |
List of all files till a commit
1 |
git ls-tree --name-only -r <span class="pl-k"><</span>commit-ish<span class="pl-k">></span> |
Git reset first commit
1 |
git update-ref -d HEAD |
List all the conflicted files
1 |
git diff --name-only --diff-filter=U |
List of all files changed in a commit
1 |
git diff-tree --no-commit-id --name-only -r <span class="pl-k"><</span>commit-ish<span class="pl-k">></span> |
Unstaged changes since last commit
1 |
git diff |
Changes staged for commit
1 |
git diff --cached |
Alternatives:
1 |
git diff --staged |
Show both staged and unstaged changes
1 |
git diff HEAD |
List all branches that are already merged into master
1 |
git branch --merged master |
Quickly switch to the previous branch
1 |
git checkout - |
Remove branches that have already been merged with master
1 |
git branch --merged master <span class="pl-k">|</span> grep -v <span class="pl-s"><span class="pl-pds">'</span>^\*<span class="pl-pds">'</span></span> <span class="pl-k">|</span> xargs -n 1 git branch -d |
Alternatives:
1 |
git branch --merged master <span class="pl-k">|</span> grep -v <span class="pl-s"><span class="pl-pds">'</span>^\*\| master<span class="pl-pds">'</span></span> <span class="pl-k">|</span> xargs -n 1 git branch -d <span class="pl-c"># will not delete master if master is not checked out</span> |
List all branches and their upstreams, as well as last commit on branch
1 |
git branch -vv |
Track upstream branch
1 |
git branch -u origin/mybranch |
Delete local branch
1 |
git branch -d <span class="pl-k"><</span>local_branchname<span class="pl-k">></span> |
Delete remote branch
1 |
git push origin --delete <span class="pl-k"><</span>remote_branchname<span class="pl-k">></span> |
Alternatives:
1 |
git push origin :<span class="pl-k"><</span>remote_branchname<span class="pl-k">></span> |
Undo local changes with the last content in head
1 |
git checkout -- <span class="pl-k"><</span>file_name<span class="pl-k">></span> |
Revert: Undo a commit by creating a new commit
1 |
git revert <span class="pl-k"><</span>commit-ish<span class="pl-k">></span> |
Reset: Discard commits, advised for private branch
1 |
git reset <span class="pl-k"><</span>commit-ish<span class="pl-k">></span> |
Reword the previous commit message
1 |
git commit -v --amend |
See commit history for just the current branch
1 |
git cherry -v master |
Amend author.
1 |
git commit --amend --author=<span class="pl-s"><span class="pl-pds">'</span>Author Name <[email protected]><span class="pl-pds">'</span></span> |
Reset author, after author has been changed in the global config.
1 |
git commit --amend --reset-author --no-edit |
Changing a remote’s URL
1 |
git remote set-url origin <span class="pl-k"><</span>URL<span class="pl-k">></span> |
Get list of all remote references
1 |
git remote |
Alternatives:
1 |
git remote show |
Get list of all local and remote branches
1 |
git branch -a |
Get only remote branches
1 |
git branch -r |
Stage parts of a changed file, instead of the entire file
1 |
git add -p |
Get git bash completion
1 |
curl http://git.io/vfhol <span class="pl-k">></span> <span class="pl-k">~</span>/.git-completion.bash <span class="pl-k">&&</span> <span class="pl-c1">echo</span> <span class="pl-s"><span class="pl-pds">'</span>[ -f ~/.git-completion.bash ] && . ~/.git-completion.bash<span class="pl-pds">'</span></span> <span class="pl-k">>></span> <span class="pl-k">~</span>/.bashrc |
What changed since two weeks?
1 |
git log --no-merges --raw --since=<span class="pl-s"><span class="pl-pds">'</span>2 weeks ago<span class="pl-pds">'</span></span> |
Alternatives:
1 |
git whatchanged --since=<span class="pl-s"><span class="pl-pds">'</span>2 weeks ago<span class="pl-pds">'</span></span> |
See all commits made since forking from master
1 |
git log --no-merges --stat --reverse master.. |
Pick commits across branches using cherry-pick
1 |
git checkout <span class="pl-k"><</span>branch-name<span class="pl-k">></span> <span class="pl-k">&&</span> git cherry-pick <span class="pl-k"><</span>commit-ish<span class="pl-k">></span> |
Find out branches containing commit-hash
1 |
git branch -a --contains <span class="pl-k"><</span>commit-ish<span class="pl-k">></span> |
Alternatives:
1 |
git branch --contains <span class="pl-k"><</span>commit-ish<span class="pl-k">></span> |
Git Aliases
1 2 |
git config --global <span class="pl-c1">alias</span>.<span class="pl-k"><</span>handle<span class="pl-k">></span> <span class="pl-k"><</span><span class="pl-c1">command</span><span class="pl-k">></span> git config --global <span class="pl-c1">alias</span>.st status |
Saving current state of tracked files without commiting
1 |
git stash |
Alternatives:
1 |
git stash save |
Saving current state including untracked files
1 |
git stash save -u |
Alternatives:
1 |
git stash save --include-untracked |
Show list of all saved stashes
1 |
git stash list |
Apply any stash without deleting from the stashed list
1 |
git stash apply <span class="pl-k"><</span>stash@{n}<span class="pl-k">></span> |
Apply last stashed state and delete it from stashed list
1 |
git stash pop |
Alternatives:
1 |
git stash apply stash@{0} <span class="pl-k">&&</span> git stash drop stash@{0} |
Delete all stored stashes
1 |
git stash clear |
Alternatives:
1 |
git stash drop <span class="pl-k"><</span>stash@{n}<span class="pl-k">></span> |
Grab a single file from a stash
1 |
git checkout <span class="pl-k"><</span>stash@{n}<span class="pl-k">></span> -- <span class="pl-k"><</span>file_path<span class="pl-k">></span> |
Alternatives:
1 |
git checkout stash@{0} -- <span class="pl-k"><</span>file_path<span class="pl-k">></span> |
Show all tracked files
1 |
git ls-files -t |
Show all untracked files
1 |
git ls-files --others |
Show all ignored files
1 |
git ls-files --others -i --exclude-standard |
Create new working tree from a repository (git 2.5)
1 |
git worktree add -b <span class="pl-k"><</span>branch-name<span class="pl-k">></span> <span class="pl-k"><</span>path<span class="pl-k">></span> <span class="pl-k"><</span>start-point<span class="pl-k">></span> |
Create new working tree from HEAD state
1 |
git worktree add --detach <span class="pl-k"><</span>path<span class="pl-k">></span> HEAD |
Untrack files without deleting
1 |
git rm --cached <span class="pl-k"><</span>file_path<span class="pl-k">></span> |
Alternatives:
1 |
git rm --cached -r <span class="pl-k"><</span>directory_path<span class="pl-k">></span> |
Before deleting untracked files/directory, do a dry run to get the list of these files/directories
1 |
git clean -n |
Forcefully remove untracked files
1 |
git clean -f |
Forcefully remove untracked directory
1 |
git clean -f -d |
Alternatives:
1 |
git clean -df |
Update all the submodules
1 |
git submodule foreach git pull |
Show all commits in the current branch yet to be merged to master
1 |
git cherry -v master |
Alternatives:
1 |
git cherry -v master <span class="pl-k"><</span>branch-to-be-merged<span class="pl-k">></span> |
Rename a branch
1 |
git branch -m <span class="pl-k"><</span>new-branch-name<span class="pl-k">></span> |
Alternatives:
1 |
git branch -m [<span class="pl-k"><</span>old-branch-name<span class="pl-k">></span>] <span class="pl-k"><</span>new-branch-name<span class="pl-k">></span> |
rebases ‘feature’ to ‘master’ and merges it in to master
1 |
git checkout feature <span class="pl-k">&&</span> git rebase @{-1} <span class="pl-k">&&</span> git checkout @{-2} <span class="pl-k">&&</span> git merge @{-1} |
Archive the master
branch
1 |
git archive master --format=zip --output=master.zip |
Modify previous commit without modifying the commit message
1 |
git add --all <span class="pl-k">&&</span> git commit --amend --no-edit |
Prunes references to remote branches that have been deleted in the remote.
1 |
git fetch -p |
Alternatives:
1 |
git remote prune origin |
Retrieve the commit hash of the initial revision.
1 |
git rev-list --reverse HEAD <span class="pl-k">|</span> head -1 |
Visualize the version tree.
1 |
git log --pretty=oneline --graph --decorate --all |
Alternatives:
1 |
gitk --all |
Deploying git tracked subfolder to gh-pages
1 |
git subtree push --prefix subfolder_name origin gh-pages |
Adding a project to repo using subtree
1 |
git subtree add --prefix=<span class="pl-k"><</span>directory_name<span class="pl-k">></span>/<span class="pl-k"><</span>project_name<span class="pl-k">></span> --squash git@github.com:<span class="pl-k"><</span>username<span class="pl-k">></span>/<span class="pl-k"><</span>project_name<span class="pl-k">></span>.git master |
Get latest changes in your repo for a linked project using subtree
1 |
git subtree pull --prefix=<span class="pl-k"><</span>directory_name<span class="pl-k">></span>/<span class="pl-k"><</span>project_name<span class="pl-k">></span> --squash git@github.com:<span class="pl-k"><</span>username<span class="pl-k">></span>/<span class="pl-k"><</span>project_name<span class="pl-k">></span>.git master |
Export a branch with history to a file.
1 |
git bundle create <span class="pl-k"><</span>file<span class="pl-k">></span> <span class="pl-k"><</span>branch-name<span class="pl-k">></span> |
Import from a bundle
1 |
git clone repo.bundle <span class="pl-k"><</span>repo-dir<span class="pl-k">></span> -b <span class="pl-k"><</span>branch-name<span class="pl-k">></span> |
Get the name of current branch.
1 |
git rev-parse --abbrev-ref HEAD |
Ignore one file on commit (e.g. Changelog).
1 |
git update-index --assume-unchanged Changelog<span class="pl-k">;</span> git commit -a<span class="pl-k">;</span> git update-index --no-assume-unchanged Changelog |
Stash changes before rebasing
1 |
git rebase --autostash |
Fetch pull request by ID to a local branch
1 |
git fetch origin pull/<span class="pl-k"><</span>id<span class="pl-k">></span>/head:<span class="pl-k"><</span>branch-name<span class="pl-k">></span> |
Alternatives:
1 |
git pull origin pull/<span class="pl-k"><</span>id<span class="pl-k">></span>/head:<span class="pl-k"><</span>branch-name<span class="pl-k">></span> |
Show the most recent tag on the current branch.
1 |
git describe --tags --abbrev=0 |
Show inline word diff.
1 |
git diff --word-diff |
Don’t consider changes for tracked file.
1 |
git update-index --assume-unchanged <span class="pl-k"><</span>file_name<span class="pl-k">></span> |
Undo assume-unchanged.
1 |
git update-index --no-assume-unchanged <span class="pl-k"><</span>file_name<span class="pl-k">></span> |
Clean the files from .gitignore
.
1 |
git clean -X -f |
Restore deleted file.
1 |
git checkout <span class="pl-k"><</span>deleting_commit<span class="pl-k">></span>^ -- <span class="pl-k"><</span>file_path<span class="pl-k">></span> |
Restore file to a specific commit-hash
1 |
git checkout <span class="pl-k"><</span>commit-ish<span class="pl-k">></span> -- <span class="pl-k"><</span>file_path<span class="pl-k">></span> |
Always rebase instead of merge on pull.
1 |
git config --global branch.autosetuprebase always |
List all the alias and configs.
1 |
git config --list |
Make git case sensitive.
1 |
git config --global core.ignorecase <span class="pl-c1">false</span> |
Auto correct typos.
1 |
git config --global <span class="pl-c1">help</span>.autocorrect 1 |
Check if the change was a part of a release.
1 |
git name-rev --name-only <span class="pl-k"><</span>SHA-<span class="pl-k">1></span> |
Dry run. (any command that supports dry-run flag should do.)
1 |
git clean -fd --dry-run |
Marks your commit as a fix of a previous commit.
1 |
git commit --fixup <span class="pl-k"><</span>SHA-<span class="pl-k">1></span> |
squash fixup commits normal commits.
1 |
git rebase -i --autosquash |
skip staging area during commit.
1 |
git commit -am <span class="pl-k"><</span>commit message<span class="pl-k">></span> |
List ignored files.
1 |
git check-ignore <span class="pl-k">*</span> |
Status of ignored files.
1 |
git status --ignored |
Commits in Branch1 that are not in Branch2
1 |
git log Branch1 ^Branch2 |
reuse recorded resolution, record and reuse previous conflicts resolutions.
1 |
git config --global rerere.enabled 1 |
Open all conflicted files in an editor.
1 |
git diff --name-only <span class="pl-k">|</span> uniq <span class="pl-k">|</span> xargs <span class="pl-smi">$EDITOR</span> |
Count unpacked number of objects and their disk consumption.
1 |
git count-objects --human-readable |
Prune all unreachable objects from the object database.
1 |
git gc --prune=now --aggressive |
Instantly browse your working repository in gitweb.
1 |
git instaweb [--local] [--httpd<span class="pl-k">=<</span>httpd<span class="pl-k">></span>] [--port<span class="pl-k">=<</span>port<span class="pl-k">></span>] [--browser<span class="pl-k">=<</span>browser<span class="pl-k">></span>] |
View the GPG signatures in the commit log
1 |
git log --show-signature |
Remove entry in the global config.
1 |
git config --global --unset <span class="pl-k"><</span>entry-name<span class="pl-k">></span> |
Checkout a new branch without any history
1 |
git checkout --orphan <span class="pl-k"><</span>branch_name<span class="pl-k">></span> |
Extract file from another branch.
1 |
git show <span class="pl-k"><</span>branch_name<span class="pl-k">></span>:<span class="pl-k"><</span>file_name<span class="pl-k">></span> |
List only the root and merge commits.
1 |
git log --first-parent |
Merge previous two commits into one.
1 |
git rebase --interactive HEAD~2 |
List all branch is WIP
1 |
git checkout master <span class="pl-k">&&</span> git branch --no-merged |
Find guilty with binary search
1 2 3 4 5 6 |
git bisect start <span class="pl-c"># Search start </span> git bisect bad <span class="pl-c"># Set point to bad commit </span> git bisect good v2.6.13-rc2 <span class="pl-c"># Set point to good commit|tag </span> git bisect bad <span class="pl-c"># Say current state is bad </span> git bisect good <span class="pl-c"># Say current state is good </span> git bisect reset <span class="pl-c"># Finish search </span> |
Bypass pre-commit and commit-msg githooks
1 |
git commit --no-verify |
List commits and changes to a specific file (even through renaming)
1 |
git log --follow -p -- <span class="pl-k"><</span>file_path<span class="pl-k">></span> |
Clone a single branch
1 |
git clone -b <span class="pl-k"><</span>branch-name<span class="pl-k">></span> --single-branch https://github.com/user/repo.git |
Create and switch new branch
1 |
git checkout -b <span class="pl-k"><</span>branch-name<span class="pl-k">></span> |
Alternatives:
1 |
git branch <span class="pl-k"><</span>branch-name<span class="pl-k">></span> <span class="pl-k">&&</span> git checkout <span class="pl-k"><</span>branch-name<span class="pl-k">></span> |
Ignore file mode changes on commits
1 |
git config core.fileMode <span class="pl-c1">false</span> |
Turn off git colored terminal output
1 |
git config --global color.ui <span class="pl-c1">false</span> |
specific color settings
1 |
git config --global <span class="pl-k"><</span>specific <span class="pl-c1">command</span> e.g branch, diff<span class="pl-k">></span> <span class="pl-k"><</span><span class="pl-c1">true</span>, <span class="pl-c1">false</span> or always<span class="pl-k">></span> |