Git cheat sheet

For my first blog post, I’m sharing the Git commands I use most often when setting up a new project. This is mostly here for my own benefit in forgetful moments, but others might find it useful too.

Initial setup

git clone

If NameOfRepo.git was a fork, set the upstream:

git remote add upstream

pull down all remote branches

git fetch upstream  # the upstream
git fetch origin  # my fork of the upstream

changing remote url

git remote set-url origin

(So far I’ve only ever used this to switch from https to ssh, or to correct a mistake in a prior git remote add ... step.)

testing other peoples’ pull requests before merging

git fetch SomeoneElsesUsername/branchname  

(exclude /branchname to get all of their branches)

git checkout -b somename SomeoneElsesUsername/branchname 

now run nosetests, etc.


git fetch upstream
git checkout theBranchYouWantToRebase
git rebase upstream/master

You can rebase against any branch you want, but upstream/master is most common. If there are conflicts, repeat the following two lines as necessary:

git mergetool
git rebase --continue

After all conflicts have been resolved:

git push --force origin theBranchThatYouJustRebased

deleting remote branches

git branch -rd origin/nameOfBranchToDelete
git push origin --delete nameOfBranchToDelete

(The first line usually works; sometimes when it didn’t I’ve had luck with the second.)

untracking a file

# remove a file from local, and from remote on next push:
git rm nameOfFileToDelete
# keep local file but stop tracking it; delete it from remote on next push
git rm --cached nameOfFileToStopTrackingAndDeleteRemoteCopyButKeepLocal
# keep local file and remote file, but ignore any local changes to it
git update-index --assume-unchanged nameOfFileToStopTrackingWithoutDeleting