A collection of frequently used git commands
The book Pro Git, a comprehensive reference and tutorial, is available at https://git-scm.com/book/en/v2
Configuration
git config --global user.name "Darth Vader"
git config --global user.email "dvader@deathstar.com"
Set your name and email address for commit messages
git config --list
Show current config settings
git config -e
Edit config file with $EDITOR
Repository setup and cloning
git init
Initialize new local repo
git clone git@gitlab.mycompany.com:groupproject.git
Clone a copy of repo to groupproject subdirectory
Make changes
git add
Add individual files to the next commit
git add --all
git add -A
Add all changed files to the next commit
git status
Show status of uncommitted changes
git diff
Show changes not yet staged
git diff --staged
git diff HEAD~1
Show changes ready to be committed
git reset
Remove file from the next commit (no content change)
git checkout -- <file>
Check out a file from the last commit, overwrite current contents
git reset HEAD~1
Undo most recent commit, preserve changes
git reset --hard HEAD~1
Undo most recent commit, discard changes
Commit changes
git commit
Commit all staged changes, enter $EDITOR to create commit message
git commit -m "commmit message"
Commit staged changes and specify commit message without entering $EDITOR
git commit --amend
Edit the commit message of the most recent commit
git commit --amend <file1> <file2> ...
Amend previous commit, including changes made to listed files
View history
git log
List commits, most recent first
git log --grep=string
List commits having "string" in commit message
git log --author=name
List commits by "name" author
git log --follow <file>
List commits affecting <file>
(git whatchanged is depreciated)
git show <commitID>
Show changes in a specific commit
git show --name-only <commitID>
Show only names of files changed by a commit
git blame <file>
Show the author and commit ID of each line of <file>
git diff <rev1> <rev2>
Show differences between commits or branches
Find a deleted file in the repo, if the full path is known
git log --all --full-history -- **/filename.*
Find a deleted file in the repo, if the full path is not known
Branches
git branch
List local branches
git branch -r
List remote branches
git branch -a
List local and remote branches
git branch -av
List local and remote branches with last commit ID and message
git branch <name>
Create a new branch
git checkout <name>
Switches to the specified branch and updates working directory
git checkout -b <name>
Creates new branch then checks out
git branch -d <name>
Delete branch, but only if already merged
git branch -D <name>
Delete branch unconditionally
Working with Remotes
git remote -v
Show URL of remote repository
git fetch
Updates history from remote
git merge
Update current branch with changes from remote
git pull
Download changes and update current branch,
equivalent to git fetch followed by git merge
git push origin <name>
Push local branch to remote repository
Create remote branch if it doesn't already exist
If remote branch exists, pushes local changes
git push origin --delete <name>
Delete remote branch
Rebasing
git rebase <branch>
Rebase the current branch to <branch>
git rebase -i HEAD~5
Interactively rebase the last 5 commits, usually used to "squash" multiple commits into one.
Working with Forks
git remote add upstream git\@github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
Configure original repo as an upstream to your local copy of forked repo
git remote set-url --push upstream no_push
Disable pushing to upstream to prevent accidental pushes
git merge --ff-only upstream/master
Perform a fast-forward merge to update your local master to match upstream repo master.
git reset --hard upstream/master
If fast-forward merge not possible, force your local master to match upstream repo master
Stashing
git stash save
git stash save <name>
Save staged changes to a new stash and restore current branch to last committed state.
(unstaged changes are not saved)
git stash apply
git stash apply <name>
Apply the changes from the most recent stash (or named stash) to the current working tree
git stash pop
Apply the changes from the most recent stash to the current working tree, and delete the stash
git stash list
List current stashes
git stash show <name> -p
Show the contents of a stash - accepts all diff args
git stash drop
Delete a stash
git stash clear
Delete all stashes
Housekeeping
git branch --merged
Show local branches that have been merged (candidates for deletion)
git branch -r --merged
Show remote branches that have been merged (candidates for deletion)
git branch --no-merged
Show local branches that have not been merged
git remote prune origin
Delete local copies of remote branches that have been deleted
git remote prune --dry-run origin
Show local branches that would be deleted but don't delete them
git fetch -p
Combine fetch and prune in one command
for branch in `git branch -r --merged | grep -v HEAD`; do echo -e `git show --format="%ci %cr %an" $branch | head -n 1` \\t$branch; done | sort -r
List remote merged branches with last commit date and author