Basic Operations
List of all files till a commit
git ls-tree --name-only -r <commit-ish>
Quickly switch to the previous branch
git checkout -
Alternatives:
git checkout @{-1}
Delete remote branch
git push origin --delete <remote_branchname>
Alternatives:
git push origin :<remote_branchname>
git branch -dr <remote/branch>
Delete remote tag
git push origin :refs/tags/<tag-name>
Undo local changes with the content in index(staging)
git checkout -- <file_name>
Reword the previous commit message
git commit -v --amend
See commit history for just the current branch
git cherry -v master
Amend author.
git commit --amend --author='Author Name <email@address.com>'
Stage parts of a changed file, instead of the entire file
git add -p
Pick commits across branches using cherry-pick
git checkout <branch-name> && git cherry-pick <commit-ish>
Grab a single file from a stash
git checkout <stash@{n}> -- <file_path>
Alternatives:
git checkout stash@{0} -- <file_path>
Create new working tree from a repository (git 2.5)
git worktree add -b <branch-name> <path> <start-point>
Create new working tree from HEAD state
git worktree add --detach <path> HEAD
Show all commits in the current branch yet to be merged to master
git cherry -v master
Alternatives:
git cherry -v master <branch-to-be-merged>
Modify previous commit without modifying the commit message
git add --all && git commit --amend --no-edit
Prunes references to remove branches that have been deleted in the remote.
git fetch -p
Alternatives:
git remote prune origin
Retrieve the commit hash of the initial revision.
git rev-list --reverse HEAD | head -1
Alternatives:
git rev-list --max-parents=0 HEAD
git log --pretty=oneline | tail -1 | cut -c 1-40
git log --pretty=oneline --reverse | head -1 | cut -c 1-40
Import from a bundle
git clone repo.bundle <repo-dir> -b <branch-name>
Ignore one file on commit (e.g. Changelog).
git update-index --assume-unchanged Changelog; git commit -a; git update-index --no-assume-unchanged Changelog
Fetch pull request by ID to a local branch
git fetch origin pull/<id>/head:<branch-name>
Alternatives:
git pull origin pull/<id>/head:<branch-name>
Restore deleted file.
git checkout <deleting_commit> -- <file_path>
Restore file to a specific commit-hash
git checkout <commit-ish> -- <file_path>
Marks your commit as a fix of a previous commit.
git commit --fixup <SHA-1>
Skip staging area during commit.
git commit --only <file_path>
Interactive staging.
git add -i
Status of ignored files.
git status --ignored
Checkout a new branch without any history
git checkout --orphan <branch_name>
Find guilty with binary search
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
Bypass pre-commit and commit-msg githooks
git commit --no-verify
Clone a single branch
git clone -b <branch-name> --single-branch https://github.com/user/repo.git
Create and switch new branch
git checkout -b <branch-name>
Alternatives:
git branch <branch-name> && git checkout <branch-name>
git switch -c <branch-name>
Show all local branches ordered by recent commits
git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/
Clone a shallow copy of a repository
git clone https://github.com/user/repo.git --depth 1
Force push to Remote Repository
git push -f <remote-name> <branch-name>
Group commits by authors and title
git shortlog
Forced push but still ensure you don't overwrite other's work
git push --force-with-lease <remote-name> <branch-name>
Number of commits in a branch
git rev-list --count <branch-name>
Add object notes
git notes add -m 'Note on the previous commit....'
Apply commit from another repository
git --git-dir=<source-dir>/.git format-patch -k -1 --stdout <SHA1> | git am -3 -k
Specific fetch reference
git fetch origin master:refs/remotes/origin/mymaster
Generates a summary of pending changes
git request-pull v1.0 https://git.ko.xz/project master:for-linus
Show git status short
git status --short --branch
Checkout a commit prior to a day ago
git checkout master@{yesterday}
Push the current branch to the same name on the remote repository
git push origin HEAD
Push a new local branch to remote repository and track
git push -u origin <branch_name>
Update a submodule to the latest commit
cd <path-to-submodule>
git pull origin <branch>
cd <root-of-your-main-project>
git add <path-to-submodule>
git commit -m "submodule updated"
Duplicating a repository
git clone --bare https://github.com/exampleuser/old-repository.git
git push --mirror https://github.com/exampleuser/new-repository.git