Below I will be listing git commands that might be useful for everyday programming workflow.
Show helpful guides that come with Git
git help -g
Search change by content
git log -S'<a term in the source>'
Show changes over time for specific file
git log -p <file_name>
Remove sensitive data from history, after a push
git 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 --all
Sync with remote, overwrite local changes
git fetch origin && git reset --hard origin/master && git clean -f -d
List of all files till a commit
git ls-tree --name-only -r <commit-ish>
Git reset first commit
git update-ref -d HEAD
Reset: preserve uncommitted local changes
git reset --keep <commit>
List all the conflicted files
git diff --name-only --diff-filter=U
List of all files changed in a commit
git diff-tree --no-commit-id --name-only -r <commit-ish>
Unstaged changes since last commit
git diff
Changes staged for commit
git diff --cached
Alternatives:
git diff --staged
Show both staged and unstaged changes
git diff HEAD
List all branches that are already merged into master
git branch --merged master
Quickly switch to the previous branch
git checkout -
Alternatives:
git checkout @{-1}
Remove branches that have already been merged with master
git branch --merged master | grep -v '^\*' | xargs -n 1 git branch -d
Alternatives:
git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d # will not delete master if master is not checked out
List all branches and their upstreams, as well as last commit on branch
git branch -vv
Track upstream branch
git branch -u origin/mybranch
Delete local branch
git branch -d <local_branchname>
Delete remote branch
git push origin --delete <remote_branchname>
Alternatives:
git push origin :<remote_branchname>git branch -dr <remote/branch>
Delete local tag
git tag -d <tag-name>
Delete remote tag
git push origin :refs/tags/<tag-name>
Undo local changes with the last content in head
git checkout -- <file_name>
Revert: Undo a commit by creating a new commit
git revert <commit-ish>
Reset: Discard commits, advised for private branch
git reset <commit-ish>
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>'
Reset author, after author has been changed in the global config.
git commit --amend --reset-author --no-edit
Changing a remote’s URL
git remote set-url origin <URL>
Get list of all remote references
git remote
Alternatives:
git remote show
Get list of all local and remote branches
git branch -a
Get only remote branches
git branch -r
Stage parts of a changed file, instead of the entire file
git add -p
Get git bash completion
curl -L http://git.io/vfhol > ~/.git-completion.bash && echo '[ -f ~/.git-completion.bash ] && . ~/.git-completion.bash' >> ~/.bashrc
What changed since two weeks?
git log --no-merges --raw --since='2 weeks ago'
Alternatives:
git whatchanged --since='2 weeks ago'
See all commits made since forking from master
git log --no-merges --stat --reverse master..
Pick commits across branches using cherry-pick
git checkout <branch-name> && git cherry-pick <commit-ish>
Find out branches containing commit-hash
git branch -a --contains <commit-ish>
Alternatives:
git branch --contains <commit-ish>
Git Aliases
git config --global alias.<handle> <command>
git config --global alias.st status
Saving current state of tracked files without commiting
git stash
Alternatives:
git stash save
Saving current state of unstaged changes to tracked files
git stash -k
Alternatives:
git stash --keep-indexgit stash save --keep-index
Saving current state including untracked files
git stash -u
Alternatives:
git stash save -ugit stash save --include-untracked
Saving current state with message
git stash save <message>
Saving current state of all files (ignored, untracked, and tracked)
git stash -a
Alternatives:
git stash --allgit stash save --all
Show list of all saved stashes
git stash list
Apply any stash without deleting from the stashed list
git stash apply <stash@{n}>
Apply last stashed state and delete it from stashed list
git stash pop
Alternatives:
git stash apply stash@{0} && git stash drop stash@{0}
Delete all stored stashes
git stash clear
Alternatives:
git stash drop <stash@{n}>
Grab a single file from a stash
git checkout <stash@{n}> -- <file_path>
Alternatives:
git checkout stash@{0} -- <file_path>
Show all tracked files
git ls-files -t
Show all untracked files
git ls-files --others
Show all ignored files
git ls-files --others -i --exclude-standard
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
Untrack files without deleting
git rm --cached <file_path>
Alternatives:
git rm --cached -r <directory_path>
Before deleting untracked files/directory, do a dry run to get the list of these files/directories
git clean -n
Forcefully remove untracked files
git clean -f
Forcefully remove untracked directory
git clean -f -d
Update all the submodules
git submodule foreach git pull
Alternatives:
git submodule update --init --recursivegit submodule update --remote
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>
Rename a branch
git branch -m <new-branch-name>
Alternatives:
git branch -m [<old-branch-name>] <new-branch-name>
Rebases ‘feature’ to ‘master’ and merges it in to master
git rebase master feature && git checkout master && git merge -
Archive the master
branch
git archive master --format=zip --output=master.zip
Modify previous commit without modifying the commit message
git add --all && git commit --amend --no-edit
Prunes references to remote 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