Patrick Ward words, code, and music

Git Cheatsheet

I’m always forgetting the various subcommands I need when performing git related tasks on my projects. So, I’m using this post as a way to catalogue some of the more useful commands that I need to remember.

Which files have changed between branches?

There are times when I am in a branch or working on the current stash, but curious as to which files have actually changed. Doing a diff gives you all of the details on the current patch, but sometimes I just want to know which files have changed. For that, git provides the --stat option.

git diff master --stat

Which files are being ignored by git?

git status --ignored

Renaming a branch

Sometimes I’ll create a quick branch to start a new idea. But, unfortunately, I often name it something that has nothing to do with what I was trying to accomplish. So, when I return to that branch, the name gives me little information for what I was trying to accomplish. No problem, git provides a -m option for branch, which allows me to rename it.

git branch -m old_branch new_branch

Renaming a Remote Branch (e.g. on GitHub):

  1. Rename the local branch
git branch -m old_branch new_branch
  1. Delete the remote branch
git push origin :old_branch
  1. Push the newly renamed local branch to remote
git push -u new_branch origin/new_branch

Which branches are not merged in?

I needed to know which branches in my master had not been merged in recently. So, I found this little option which provides a list of the branches not merged with the current one.

git branch --no-merged

Deleting a branch locally and remotely

I often create a remote branch and then have to delete, but each time I seem to forget the specific syntax for doing so. As of Git v1.7.0 you can do the following to remove a remote branch:

git push origin --delete my-branch

Which in previous versions of Git v1.50 and above is the same as doing this:

git push origin :my-branch

From: stackoverflow

Pull a Git Branch from Remote

To pull a remote branch onto a system that doesn’t alreay have the named branch:

# Fetch all info from the branch
git fetch origin
# View all remote branches ( use -a for both local and remote )
git branch -r
# Checkout the remote branch and create a local branch in the process
git checkout -b my-branch origin/my-branch

Change the Current Branch to Master in Git

I had a situation where my branch code was superior to the master branch, so I wanted to make the branch the master.

A quick search led me to this StackOverflow answer.

git checkout my-branch
git merge --strategy=ours --no-commit master # keep the content of this branch, record a merge
git commit # add a more descriptive commit message
git checkout master
git merge my-branch

Reset local branch to remote branch HEAD

Set the local branch to match the remote branch exactly:

git fetch origin
git reset --hard origin/master

You may also save the current branch’s state prior to the above steps (as a sanity check). This will save your work in a new branch in case you want to review or revert back to that data after you run the reset options above.

git commit -a -m "Saving my work, just in case"
git branch my-saved-work

Prune Deleted Remote Branches

Remove local references to deleted remote branches:

git fetch -p

Or, set it automatically (git 1.8.5+)

# Local
git config fetch.prune true

# Global
git config --global fetch.prune true

Cleanup and Prune the Local Repo

Run the garbage collector and prune any loose objects older than now (default –prune is 2 weeks)

git gc --prune=now

Git Stash

Using Git stash is a quick an easy way to stash uncommited files when switching branches or cleaning up a working directory when you’re not ready to do anything with files you want to stash.

Stash with a message:

git stash save <option message>

Stashing untracked files can be done using the -u option, which will stash all untracked files and delete them from your working directory. Use this with caution as will delete any files you have in directories that are marked with directory/* in your .gitignore files.

git stash save -u <optional message>

List all stashes on the stack:

git stash list

Apply a stash and keep it on the stack:

git stash apply stash@{0}
# where 0 is the id of the stash you want

Drop a stash without applying it:

git stash drop stash@{0}
# where 0 is the id of the stash you want

Pop the latest stash (applies then deletes the top stash on the stack):

git stash pop

Clear all stashes on the stack:

git stash clear

From: StackOverflor

Refreshing/Updating the list of remote branches

To update the list of remote branches that the local repository knows about:

git remote update origin --prune


Note that for these examples, the tag name is v1.0.0

My general way to add an annotated tag

git tag -a v1.0.0 -m "Some message about the tag"

Delete/Remove a tag locally

git tag -d v1.0.0

Delete/Remove a tag on remote

git push origin :refs/tags/v1.0.0

Rename a tag

For example, to rename a tag named 1.0.0-old to v1.0.0-new

// Add a new tag based on the old one
git tag v1.0.0-new 1.0.0-old

// Delete the old tag
git tag -d 1.0.0-old

// Remove the old tag from origin
git push origin :refs/tags/1.0.0-old

// Push the tags to origin
git push --tags

Prune tags from remote

git pull –prune –tags