git cheat sheet

Share on:

merge PR into fork

1git remote add pr-source https://github.com/<user-providing-pull-request>/<repo-name>
2git fetch pr-source
3git merge pr-source/<pull-request-branch-name>

checkout commit

1git checkout <sha1>

sync fork with remote master

1git clone git@github.com:<your-gh-name>/ezpublish.git ezforksync
2cd ezforksync
3git remote add upstream https://github.com/ezsystems/ezpublish.git
4git pull upstream master
5git push
6

undo latest commit

1 git reset HEAD~

undo two latest commits

1git reset HEAD~2

undo to a commit (warning, hard will delete local changes)

1git reset --hard <sha1_of_where_you_want_to_be>

undo a rebase (must be the last thing you did) (else, use reflog)

1git reset --hard ORIG_HEAD
2git rebase --abort

#####Rebase with squash/pick commits Source: http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History

1$ git rebase -i HEAD~4

In editor that appears, change pick to squash into the above picked commit:

1pick 01d1124 Adding license
2pick 6340aaa Moving license into its own file
3pick ebfd367 Jekyll has become self-aware.
4pick 30e0ccb Changed the tagline in the binary, too.

Next choose commit message, just comment out all lines but one, and write a pretty msg. Done.

#####Cleanup branches Source: https://railsware.com/blog/2014/08/11/git-housekeeping-tutorial-clean-up-outdated-branches-in-local-and-remote-repositories/

 1#what branches are merged into master?
 2git checkout master
 3git branch --merged
 4
 5#remove branch with name
 6git branch -d name-of-branch-to-remove
 7
 8#what branches are not merged?
 9git branch --no-merged
10
11#remove if abandoned
12git branch -D name-of-branch-to-remove
13
14#cleanup stale references
15git branch -r
16git remote prune origin