Useful git commands
Over the past few years, I've compiled lists of git commands that I use on a daily basis, as well as ones that I use
less often, but are very powerful. Everything in [brackets]
is a variable value.
Index
Branch commands
Basics
git checkout [branch name]
git add [files]
git commit -m "[commit message]"
git pull
git push
git merge [branch name]
The very basics, checking out a branch, adding files to commit, committing with a message, pulling changes from an upstream branch, pushing changes, and merging a branch into another branch.
Checkout a new branch
git checkout -b [new branch name] --track
Checking out a new branch and tracking the current branch. Usually I use this from the develop
or master
branch.
Rename
git branch -m [new name]
Rename the current branch. Always useful if I'm working on a bugfix without a ticket, then I later need to rename my branch to the ticket number.
Change upstream branch
git branch -u [branch to track]
Sometimes I'll space out and accidentally create a new branch off of the wrong branch, or I'll forget to use
--track
with git checkout -b
. In that case, I can use this command to track changes in the correct branch. (I
always remember this one because -u
stands for "upstream").
List all branches
git branch -vv
Lists all the local branches. I do this whenever I forget a branch name.
Diffs and patches
Find differences
git diff
git diff [other branch]
git diff [other branch] --name-only
Diff is useful for finding differences. In the first case, it will find differences in changes made to the branch since the last commit. In the second example, it will find differences between the current branch and another branch.
The third example will only list the filenames of changed files. I use this a lot when I have changes spanning multiple files.
Save a diff to a text file
git diff [other branch] > [filename].patch
git diff [other branch] > [filename].diff
I've used this command before to send a diff to someone for code review. Helpful if you're not using any sort of
code review software. You can also save to a .txt
file, but patch and diff files can be applied using the patch
utility, which isn't my preferred method. I tend to use the following method to apply a patch file instead.
Applying a patch
git apply [filename].diff
git apply [filename].patch
Applying a patch just means taking the diff from the file and applying it to a different branch. Useful for applying a code change from a coworker, or for updating a really out-of-date branch. Sometimes I'll just create a new branch and apply the changes from the old branch.
If there are any conflicts, you can try --ignore-whitespace
or --ignore-space-change
, but personally I like
--3way
, which does a three-way merge if there are conflicts and then I can go into vim and resolve the conflicts
from there.
Commit log
View all changes
git log
Show all commits.
View changes only for the current branch
git log --first-parent --no-merges
Shows the commit log just for the current branch, and doesn't factor in any merges from other branches. Really nice for checking out your work in a previous commit.
Reflog
git reflog
Shows a temporary log of all commits. This is a local log that is separate from git log
.
Stash
Save current changes
git stash
Copy all changes without committing. I do this when I need to temporarily change to another branch and I'm not in a good place to commit my code. Think of it as a git clipboard.
Add my changes back
git stash apply
This applies the last stashed change to the branch.
List stashed changes
git stash list
In case you want to stash multiple changes. I generally just stash and apply a single change within a short amount of time, so I don't use this as much.
Delete stuff
Remove all changes
git checkout -- .
Want to completely remove all your uncommitted changes from all files? This will do it. Replace "." with filenames to just blow away changes in specific files.
Remove untracked files
git clean -n
git clean -f
git clean -fd
The first command will show what's going to be deleted, but won't perform the delete. The second command will delete files (-f for force), and the third will delete files and directories.
Reset back to the last commit
git reset --hard
This is a destructive command, and will discard all of your uncommitted changes. I've used this to revert a bad git
pull
, but rarely use it. I use the next variant more often.
Keep file changes but remove commits
git reset --soft [upstream branch]
Still somewhat dangerous, but if I have a lot of garbage commits in a branch and want it to look respectable before I push to Bitbucket or Github, I'll use this to remove all the previous commits and re-commit.
Delete a branch
git branch -d [branch name]
Deletes a branch. Using -D
will force delete a branch. You can undelete a branch using data from reflog.