Git Cheat-sheet

Image for post
Image for post
From Georgia Tech Course Software Development (https://www.youtube.com/watch?v=3a2x1iJFJWc)
  • reset is a limited version of checkout or branch -f

1. Log

alias graph="git log --all --decorate --oneline --graph"

git reflog

2. Change history in one branch:

(1) add new file without creating new commit:

git commit --amend --no-edit

(2) modify comment:

git commit --amend -m "this is the right message"

(3) get rid of untracked files:

git clean -df (d for directory, f for force)

(4) unstage:

git reset --staged

reset has 2 types: --soft(changed work in staging area) , --mix(changed work in working area) , --hard(lose all tracked files)

(5) revert:

git revert undo the effect of past commits by creating a new commit. This can keep the history intact and avoid conflict when other people already pulled your repo.

(6) delete a commit:

git rebase -i HEAD~3

drop af12345 some message I put

(7)reorder commits

git rebase -i HEAD~3

(cut and past)

(8) fixup/squash commits

git rebase -i HEAD~3

pick af78001 some message I put

fixup afac232 some message A I put

fixup afbcc01 some message B I put

(9) split a commit

git rebase -i HEAD~3

edit af78001 some very complicated update message

git whatever you need to edit here

git rebase --continue

3. Change history between branches

  • move commit from branch1 to branch2:

git log (copy the commit hash, e.g af1234)

git checkout branch2

git cherry-pick af1234

git checkout branch1

git reset

4. clean history after merge

git branch branch_debug

git checkout branch_debug

git add .

git commit -m "done"

git checkout master

git merge branch_debug

(git branch --merged)

git branch -d branch_debug

5. resolve 3-way/recursive merge conflict

rule 1: changed lines overwrites the unchanged

rule 2: deleted and changed are conflicting

6. command shortcut

git checkout -b bname= branch+ checkout

git commit -a = add + commit

git pull = fetch + merge

7. get out of a detached HEAD

( detached HEAD means pointing to a commit directly without branch)

git checkout 965fb2 (detached HEAD)

Option 1: checking out an existing branch

git checkout master

option 2: checking out a new branch

git branch tmp_branch (HEAD, tmp_branch)

git checkout tmp_branch (HEAD -> tmp_branch)

8. stash

e.g.: when you want to checkout without a clean working area and don’t want to commit

git stash save “some message”

git stash list -p

git stash apply stash@{1}

9. sync with upstream

git remote add upstream git@github.com:someone/reponame.git

git remote fetch upstream

git merge upstream/master

git push origin master

10. clean request after pull request

git checkout -b 'feature-1'

git branch -a

(branch feature-1 has no remote tracking because it is not on GitHub yet)

11. change history

In one branch:

add new file without creating new commit:

git commit

modify comment:

git commit --amend -m "this is the right message"

get rid of untracked files:

git clean -df (d for directory, f for file)

Between branches, move commit from branch1 to branch2:

git log (copy the commit hash, e.g af1234)

git checkout branch2

git cherry-pick af1234

git checkout branch1

git reset

12. Compare two branches

git diff branch1..branch2

Written by

I am a Machine Learning Engineer with special interest in mental health and finance.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store