Git Cheat Sheet

Caution: I’m not sure how accurate all these are. I’ll update as I spot errors.

see log of commits

git log origin/master..HEAD

see diff of commits

git diff origin/master..HEAD

clear out commits & start over (preserves local changes) git reset –soft HEAD^

git add .

git commit –amend

Undo the last commit or merge (loses changes) !! git reset –hard HEAD~1
commit changes, updating the last commit rather than starting a new one

(!) Don’t do this after push!

git commit –amend <file>
squash several commits together git rebase –interactive HEAD~4

+ change 1 line to “pick” and the others to “squash”

show all current branches git branch
show last commit for each branch

git branch -v

show which branches are not merged git branch –no-merged
get master caught up after gerritt merge git reset –hard
selectively apply a change to the current branch git cherry-pick <change Id>
set up nice git history viewer

git config –global alias.hist ‘log –pretty=format:”%h %ad | %s%d [%an]” –graph –date=short’

rebase from a clone branch back to a main branch git checkout <to> # start in the destination branch

git rebase <from> # apply changes from the source branch

See a list of files changed that aren’t staged or committed

git status

See a diff of files that have changed but aren’t staged or committed

git diff

See a diff of files staged for commit

git diff –cached

See status of local repository compared to remote

git diff --stat origin/master

See diff of local repository compared to remote

git diff –stat origin/master

Commit current changes without starting a new change set

git status  # see what’s out there

git add .  # add changes to git staging

git commit –amend

^^^ You’ll have the chance to edit (or re-edit) the commit message

(warning) This just commits the changes to your local repository, but does not send them to the origin repository, or Gerritt for code review.

(warning) It’s safe to do this over and over.

(warning) Once you push your commit, you should not ammend it! If more than one person is working on the branch, you can lose commit history.

(warning) If you push a commit, ammend it, and try to push again, git will complain about your branch behind being behind. If you are the only one working on the branch, you can use -f to force the push.

git push -f

(warning) So don’t amend a commit after you’ve pushed it. Use multiple commits, and rebase them into one.

Push commits to the origin repository

If you are on a branch, you can push any commits you’ve made to the origin repository without submitting to gerritt for review. That way, if your local box dies, your changes will still be available in the origin repositiory.

git push

You may be behind, and have to catch up:

get fetch

git rebase orgin/master

(warning) That this will push commits to the remote repository, but will not push changes on disk or in git staging.

(warning) Don’t –amend after you push (wink)



Delete old branch after merge

After Gerritt does the merge, the “master” branch will be up to date with your commit. If you want to delete the branch and start fresh, use:

git checkout master

 git branch -d <branch>

git pull –rebase

git checkout -b <new branch>

^^^ if you want to spin up a new branch to work in



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s