Collection of
git-tips, want to add your tips? Checkout contributing.md
English | 中文 | Русский | 한국어 | Tiếng Việt | 日本語 | नेपाली | Polski | فارسی
- git-tip - A handy CLI to make optimum use of these tips. (Here in Docker container)
📖 Read the interactive GitBook documentation here!
P.S: All these commands are tested on git version 2.7.4 (Apple Git-66).
- Basic Operations
- List of all files till a commit
- Quickly switch to the previous branch
- Delete remote branch
- Delete remote tag
- Undo local changes with the content in index(staging)
- Reword the previous commit message
- See commit history for just the current branch
- Amend author.
- Stage parts of a changed file, instead of the entire file
- Pick commits across branches using cherry-pick
- Grab a single file from a stash
- Create new working tree from a repository (git 2.5)
- Create new working tree from HEAD state
- Show all commits in the current branch yet to be merged to master
- Modify previous commit without modifying the commit message
- Prunes references to remove branches that have been deleted in the remote.
- Retrieve the commit hash of the initial revision.
- Import from a bundle
- Ignore one file on commit (e.g. Changelog).
- Fetch pull request by ID to a local branch
- Restore deleted file.
- Restore file to a specific commit-hash
- Marks your commit as a fix of a previous commit.
- Skip staging area during commit.
- Interactive staging.
- Status of ignored files.
- Checkout a new branch without any history
- Find guilty with binary search
- Bypass pre-commit and commit-msg githooks
- Clone a single branch
- Create and switch new branch
- Show all local branches ordered by recent commits
- Clone a shallow copy of a repository
- Force push to Remote Repository
- Group commits by authors and title
- Forced push but still ensure you don't overwrite other's work
- Number of commits in a branch
- Add object notes
- Apply commit from another repository
- Specific fetch reference
- Generates a summary of pending changes
- Show git status short
- Checkout a commit prior to a day ago
- Push the current branch to the same name on the remote repository
- Push a new local branch to remote repository and track
- Update a submodule to the latest commit
- Duplicating a repository
- Branching
- List all branches that are already merged into master
- Remove branches that have already been merged with master
- List all branches and their upstreams, as well as last commit on branch
- Track upstream branch
- Delete local branch
- Get list of all local and remote branches
- Get only remote branches
- Find out branches containing commit-hash
- Rename a branch
- Archive the
masterbranch - Delete local branches that has been squash and merged in the remote.
- Export a branch with history to a file.
- Get the name of current branch.
- Show the most recent tag on the current branch.
- List all branch is WIP
- Preformatted patch file.
- Switch to a branch (modern alternative to checkout)
- Log and History
- Show helpful guides that come with Git
- Search change by content
- Show changes over time for specific file
- List all the conflicted files
- List of all files changed in a commit
- Unstaged changes since last commit
- Changes staged for commit
- Show both staged and unstaged changes
- What changed since two weeks?
- See all commits made since forking from master
- Show all tracked files
- Show all untracked files
- Show all ignored files
- Visualize the version tree.
- Visualize the tree including commits that are only referenced from reflogs
- Show inline word diff.
- Show changes using common diff tools.
- Commits in Branch1 that are not in Branch2
- List n last commits
- Open all conflicted files in an editor.
- View the GPG signatures in the commit log
- Extract file from another branch.
- List only the root and merge commits.
- List commits and changes to a specific file (even through renaming)
- Search Commit log across all branches for given text
- Get first commit in a branch (from master)
- Show the author, time and last revision made to each line of a given file
- Show how many lines does an author contribute
- Show all the git-notes
- List unpushed git commits
- Add everything, but whitespace changes
- blame on certain range
- Show a Git logical variable.
- Get the repo name.
- logs between date range
- Exclude author from logs
- View expanded details of changes in last commit
- Visualize each position of HEAD in the last 30 days
- Merging and Rebasing
- Miscellaneous
- Everyday Git in twenty commands or so
- Untrack files without deleting
- Don’t consider changes for tracked file.
- Check if the change was a part of a release.
- List ignored files.
- Count unpacked number of objects and their disk consumption.
- Prune all unreachable objects from the object database.
- Instantly browse your working repository in gitweb.
- Find lines matching the pattern (regex or string) in tracked files
- Backup untracked files.
- Send a collection of patches as emails
- Remotes
- Setup and Config
- Remove sensitive data from history, after a push
- Reset author, after author has been changed in the global config.
- Get git bash completion
- Git Aliases
- Always rebase instead of merge on pull.
- List all the alias and configs.
- Make git case sensitive.
- Add custom editors.
- Auto correct typos.
- Reuse recorded resolution, record and reuse previous conflicts resolutions.
- Remove entry in the global config.
- Ignore file mode changes on commits
- Turn off git colored terminal output
- Specific color settings
- Alias: git undo
- Edit [local/global] git config
- List all git aliases
- Use SSH instead of HTTPs for remotes
- Prevent auto replacing LF with CRLF
- Edit config for each level
- Stashing
- Saving current state of tracked files without committing
- Saving current state of unstaged changes to tracked files
- Saving current state including untracked files
- Saving current state with message
- Saving current state of all files (ignored, untracked, and tracked)
- Show list of all saved stashes
- Show the contents of any stash in patch form
- Apply any stash without deleting from the stashed list
- Apply last stashed state and delete it from stashed list
- Delete all stored stashes
- Submodules and Subtrees
- Tagging
- Undoing Changes
- Sync with remote, overwrite local changes
- Git reset first commit
- Reset: preserve uncommitted local changes
- Revert: Undo a commit by creating a new commit
- Reset: Discard commits, advised for private branch
- Before deleting untracked files/directory, do a dry run to get the list of these files/directories
- Forcefully remove untracked files
- Forcefully remove untracked directory
- Undo assume-unchanged.
- Clean the files from
.gitignore. - Dry run. (any command that supports dry-run flag should do.)
- Unstaging Staged file
- Revert: Reverting an entire merge
- Restore file (modern alternative to reset/checkout --)
git ls-tree --name-only -r <commit-ish>git checkout -Alternatives:
git checkout @{-1}git push origin --delete <remote_branchname>Alternatives:
git push origin :<remote_branchname>git branch -dr <remote/branch>git push origin :refs/tags/<tag-name>git checkout -- <file_name>git commit -v --amendgit cherry -v mastergit commit --amend --author='Author Name <email@address.com>'git add -pgit checkout <branch-name> && git cherry-pick <commit-ish>git checkout <stash@{n}> -- <file_path>Alternatives:
git checkout stash@{0} -- <file_path>git worktree add -b <branch-name> <path> <start-point>git worktree add --detach <path> HEADgit cherry -v masterAlternatives:
git cherry -v master <branch-to-be-merged>git add --all && git commit --amend --no-editgit fetch -pAlternatives:
git remote prune origin git rev-list --reverse HEAD | head -1Alternatives:
git rev-list --max-parents=0 HEADgit log --pretty=oneline | tail -1 | cut -c 1-40git log --pretty=oneline --reverse | head -1 | cut -c 1-40git clone repo.bundle <repo-dir> -b <branch-name>git update-index --assume-unchanged Changelog; git commit -a; git update-index --no-assume-unchanged Changeloggit fetch origin pull/<id>/head:<branch-name>Alternatives:
git pull origin pull/<id>/head:<branch-name>git checkout <deleting_commit> -- <file_path>git checkout <commit-ish> -- <file_path>git commit --fixup <SHA-1>git commit --only <file_path>git add -igit status --ignoredgit checkout --orphan <branch_name>git bisect start # Search start
git bisect bad # Set point to bad commit
git bisect good v2.6.13-rc2 # Set point to good commit|tag
git bisect bad # Say current state is bad
git bisect good # Say current state is good
git bisect reset # Finish search
git commit --no-verifygit clone -b <branch-name> --single-branch https://github.com/user/repo.gitgit checkout -b <branch-name>Alternatives:
git branch <branch-name> && git checkout <branch-name>git switch -c <branch-name>git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/git clone https://github.com/user/repo.git --depth 1git push -f <remote-name> <branch-name>git shortloggit push --force-with-lease <remote-name> <branch-name>git rev-list --count <branch-name>git notes add -m 'Note on the previous commit....'git --git-dir=<source-dir>/.git format-patch -k -1 --stdout <SHA1> | git am -3 -kgit fetch origin master:refs/remotes/origin/mymastergit request-pull v1.0 https://git.ko.xz/project master:for-linusgit status --short --branchgit checkout master@{yesterday}git push origin HEADgit push -u origin <branch_name>cd <path-to-submodule>
git pull origin <branch>
cd <root-of-your-main-project>
git add <path-to-submodule>
git commit -m "submodule updated"git clone --bare https://github.com/exampleuser/old-repository.git
git push --mirror https://github.com/exampleuser/new-repository.gitgit branch --merged mastergit branch --merged master | grep -v '^\*' | xargs -n 1 git branch -dAlternatives:
git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d # will not delete master if master is not checked outgit branch -vvgit branch -u origin/mybranchgit branch -d <local_branchname>git branch -agit branch -rgit branch -a --contains <commit-ish>Alternatives:
git branch --contains <commit-ish>git branch -m <new-branch-name>Alternatives:
git branch -m [<old-branch-name>] <new-branch-name>git archive master --format=zip --output=master.zipgit branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -Dgit bundle create <file> <branch-name>git rev-parse --abbrev-ref HEADgit describe --tags --abbrev=0git checkout master && git branch --no-mergedgit format-patch -M upstream..topicgit switch <branch-name>Alternatives:
git switch -c <new-branch-name>git help -ggit log -S'<a term in the source>'git log -p <file_name>git diff --name-only --diff-filter=Ugit diff-tree --no-commit-id --name-only -r <commit-ish>git diffgit diff --cachedAlternatives:
git diff --stagedgit diff HEADgit log --no-merges --raw --since='2 weeks ago'Alternatives:
git whatchanged --since='2 weeks ago'git log --no-merges --stat --reverse master..git ls-files -tgit ls-files --othersgit ls-files --others -i --exclude-standardgit log --pretty=oneline --graph --decorate --allAlternatives:
gitk --allgit log --graph --pretty=format:'%C(auto) %h | %s | %an | %ar%d'git log --graph --decorate --oneline $(git rev-list --walk-reflogs --all)git diff --word-diffgit difftool [-t <tool>] <commit1> <commit2> <path>git log Branch1 ^Branch2git log -<n>Alternatives:
git log -n <n>git diff --name-only | uniq | xargs $EDITORgit log --show-signaturegit show <branch_name>:<file_name>git log --first-parentgit log --follow -p -- <file_path>git log --all --grep='<given-text>'git log --oneline master..<branch-name> | tail -1Alternatives:
git log --reverse master..<branch-name> | head -6git blame <file-name>git log --author='_Your_Name_Here_' --pretty=tformat: --numstat | gawk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s removed lines: %s total lines: %s
", add, subs, loc }' -Alternatives:
git log --author='_Your_Name_Here_' --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s
", add, subs, loc }' - # on Mac OSXgit log --show-notes='*'git log --branches --not --remotesAlternatives:
git log @{u}..git cherry -vgit diff --ignore-all-space | git apply --cachedgit blame -L <start>,<end>git var -l | <variable>git rev-parse --show-toplevelgit log --since='FEB 1 2017' --until='FEB 14 2017'git log --perl-regexp --author='^((?!excluded-author-regex).*)$'git showgit refloggit rebase master feature && git checkout master && git merge -git rebase --autostashgit rebase -i --autosquashgit rebase --interactive HEAD~2git merge-base <branch-name> <other-branch-name>git rebase --onto <new_base> <old_base>git help everydaygit rm --cached <file_path>Alternatives:
git rm --cached -r <directory_path>git update-index --assume-unchanged <file_name>git name-rev --name-only <SHA-1>git check-ignore *git count-objects --human-readablegit gc --prune=now --aggressivegit instaweb [--local] [--httpd=<httpd>] [--port=<port>] [--browser=<browser>]git grep --heading --line-number 'foo bar'git ls-files --others -i --exclude-standard | xargs zip untracked.zipgit send-email [<options>] <file|directory>…
git send-email [<options>] <format-patch options>git remote set-url origin <URL>git remoteAlternatives:
git remote showgit remote add <remote-nickname> <remote-url>git remote -vgit ls-remote git://git.kernel.org/pub/scm/git/git.gitgit remote update origin --prunegit filter-branch --force --index-filter 'git rm --cached --ignore-unmatch <path-to-your-file>' --prune-empty --tag-name-filter cat -- --all && git push origin --force --allgit commit --amend --reset-author --no-editcurl -L http://git.io/vfhol > ~/.git-completion.bash && echo '[ -f ~/.git-completion.bash ] && . ~/.git-completion.bash' >> ~/.bashrcgit config --global alias.<handle> <command>
git config --global alias.st statusgit config --global pull.rebase trueAlternatives:
#git < 1.7.9
git config --global branch.autosetuprebase alwaysgit config --listgit config --global core.ignorecase falsegit config --global core.editor '$EDITOR'git config --global help.autocorrect 1git config --global rerere.enabled 1git config --global --unset <entry-name>git config core.fileMode falsegit config --global color.ui falsegit config --global <specific command e.g branch, diff> <true, false or always>git config --global alias.undo '!f() { git reset --hard $(git rev-parse --abbrev-ref HEAD)@{${1-1}}; }; f'git config [--global] --editgit config -l | grep alias | sed 's/^alias\.//g'Alternatives:
git config -l | grep alias | cut -d '.' -f 2git config --global url.'git@github.com:'.insteadOf 'https://github.com/'git config --global core.autocrlf falsegit config --edit --system
git config --edit --global
git config --edit --localgit stashAlternatives:
git stash pushgit stash -kAlternatives:
git stash --keep-indexgit stash push --keep-indexgit stash -uAlternatives:
git stash push -ugit stash push --include-untrackedgit stash push -m <message>Alternatives:
git stash push --message <message>git stash -aAlternatives:
git stash --allgit stash push --allgit stash listgit stash show -p <stash@{n}>git stash apply <stash@{n}>git stash popAlternatives:
git stash apply stash@{0} && git stash drop stash@{0}git stash clearAlternatives:
git stash drop <stash@{n}>git submodule foreach git pullAlternatives:
git submodule update --init --recursivegit submodule update --remotegit subtree push --prefix subfolder_name origin gh-pagesAlternatives:
git subtree push --prefix subfolder_name origin branch_namegit subtree add --prefix=<directory_name>/<project_name> --squash git@github.com:<username>/<project_name>.git mastergit subtree pull --prefix=<directory_name>/<project_name> --squash git@github.com:<username>/<project_name>.git mastergit tag <tag-name>git tag -d <tag-name>git fetch origin && git reset --hard origin/master && git clean -f -dgit update-ref -d HEADgit reset --keep <commit>git revert <commit-ish>git reset <commit-ish>git clean -ngit clean -fgit clean -f -dgit update-index --no-assume-unchanged <file_name>git clean -X -fgit clean -fd --dry-rungit reset HEAD <file-name>git revert -m 1 <commit-ish>git restore <file-name>Alternatives:
git restore --staged <file-name>