Enable diffing of untracked files
Get the history of specific lines from a file
git blame -L <lines_range> <commit> -- <path>
Change the remote a branch is tracking
git branch <branch> -u <remote>/<branch>
Start tracking a remote branch
git branch --set-upstream-to=<remote>/<branch>
Stop tracking a remote branch
git branch --unset-upstream
Get specific files from another branch
git checkout <branch> -- <paths>
Check out any of the previous branches
git checkout @{-<number>}
Remove local (untracked) files
Edit the previous commit message
git commit --amend -m "New message"
git config --global alias.<name> <command>
git config --get-regexp alias
Show only the staged changes
Compare two branches by their tips
git diff <branch-1>..<branch-2>
See the files affected by a commit
git diff-tree -r --name-only --no-commit-id <commit>
List remote branches with last committer sorted by date
git for-each-ref --format='%(committerdate)%09%(authorname)%09%(refname)' | \
sort -k5n -k2M -k3n -k4n | grep remotes | \
awk -F "\t" '{ printf "%-32s %-27s %s\n", $1, $2, $3 }'
Log all commits not reachable from master
git log <branch> --not master
Log commits with file changes stats
Search for changes containing given string
git log -S '<search string>'
Log commits with changed files
git log --graph --oneline --decorate
Squash all branch commits into one and merge them into current branch
git merge --squash <branch>
git push origin --delete <branch>
git push origin :<branch>
Push a new local branch to a remote
git push -u origin <branch>
Push local branch to a different remote branch and track it
git push -u <remote> production:master
Rebase from the common ancestor
git rebase --onto master <ancestor_branch> <branch>
Show the history of the local branch
git reflog --date=local <branch>
Use new remote repository
git remote add <remote> git://path/to/repo.git
git fetch <remote>
git checkout --track <remote>/<branch>
Show remote
information (including URL)
git remote set-url origin [email protected] /USERNAME/OTHERREPOSITORY.git
Update the remote branches list
Undo a merge inside a dirty working tree
Force pull a rebased branch ignoring the local state
git fetch
git reset --hard origin/<branch>
Show the last commit matching query string
Show branches containing given SHA
git branch --contains <SHA>
Temporarily ignore file changes
git update-index --assume-unchanged <file>
One-commit feature branch workflow
git fetch --all -p
git checkout origin/master
git commit -m’changes’
git push origin HEAD:refs/heads/the-feature
Changing author info in the repository history
git filter-branch --env-filter '
OLD_EMAIL="[email protected] "
CORRECT_NAME="Marek Grzybek"
CORRECT_EMAIL="[email protected] "
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
git push --force --tags origin 'refs/heads/*'