Git Commands

Sellen Wei
4 min readApr 8, 2018

--

Part I: local git

part 1: git commit + add

  1. git add . add all modified file
  2. git add -u add all file that have been track before
  3. git commit -m “message” commit with message
  4. git commit -a -m “commit message” all all and commit in one command

Part2: git checkout

Git checkout -b [yourbranchname] make new brach

Git checkout <commit name> head will be move directly point to the commit name

git checkout <branch name> switch to certain branch

Git checkout HEAD^

Git checkout HEAD~<num>

Part 3: merge

Git checkout branch1; git merge branch2

Part 4: rebasing

  1. Git checkout bugFix; Git rebase master

Move the head of bugfix to the end of master branch

2. Git rebase -i HEAD~4 (rebase i could reorganize all the previous commit by change the order, squash or more, very good and efficient commit to squash commits to one commit)

Part 5 : HEAD

  1. HEAD^ current commit
  2. HEAD~[num] back num of commits

Git head movement :

  1. Git checkout C1
  2. Git checkout master
  3. Git checkcout HEAD^

Branch head movement:

  1. Git branch -f master HEAD~3 move the master branch back to 3 commit backward

Part 6 : git reset

Git reset HEAD^ go back to the previous commit

Part 7: git reverse

Git reverse HEAD^ generate a new commit that get the previous changes back

Part 8: Cherry- pick

  1. Git cherry- pick C1 C2 C3 C4…. (all commit could come from different branches)

Part 9 git commit — amend

*The git commit — amend command is a convenient way to modify the most recent commit. It lets you combine staged changes with the previous commit instead of creating an entirely new commit.amending does not just alter the most recent commit, it replaces it entirely, meaning the amended commit will be a new entity with its own ref.

*git commit — amend -m “an updated commit message”

Dont amend pubic commits

Part 10: git tag

Git tag <tagName> <commit name/hash>

Always run git clone the first time you deal with a remote branch

Part 11: git describe

Git describe <branchName>

Git describe HEAD

Git describe + any branch head you want to show tag

$git describe

V1_2_gc6 (there is one tag called v1 which is 2 commit away from current commit, and current commit is C6)

Part 12: choose checkout which parent when parent is more than one

Git checkout master ^ 2

Git checkout HEAD ^ 2

Git checkout HEAD~ ^ 2~2 (the first ~² mean 2 commit back step to second parent)

Part 13: git stash

A way to save your work when you want to do switch

Git stash to save work

Git stash pop to reapply work

Untrack fille will not be stash, tracked file no matter stage or not will be stash

could also stash several file more detail see the attached link

Part II: remote git

Part1: git pull

  1. Git pull = git fetch + git merge (git fetch, download the changes but keep those in somewhere and wait for other actions)

2. Git pull — — rebase : use this when you have some local change and remote have changes as well, this will be better as pul because it will keep your commit come after remote commit

3. Git pull origin <remote branch name>

4.Git pull origin bar~1:bugfix

Part 2 : tracking target

Git branch -u <remote branch> <local branch>

Git branch -u <remote branch> if local already been checked out

Part 3: git push

  1. Git push origin <remote branch name> (source and destination are same)
  2. Git push origin <source>:<destination>
  3. Git push origin foo^:master (only one commit push to master branch)
  4. Git push origin master:newbranch (push to a target never exist before, will not update origin/master at all)

3. Git push origin :side delete the branch called side

Part 4: git fetch

  1. Git fetch download all the changes in remote
  2. Git fetch origin <local branch name>
  3. Git fetch origin <source>:<destination>
  4. Git fetch origin foo~1:bar

Part 5: git delete

  1. Local: git branch -d the_local_branch
  2. Remote :git push origin — delete the_remote_branch
  3. Git push origin

Quiz:

  1. When I commit but do not want this commit any more, how to redo it?

Return to the previous commit without keep changes: hard resetUndo commit but save changes: soft reset

2. Before I commit, I realize the file I staged no longer valuable, how to undo staging?

Git reset

3. What if I wrongly run hard reset to my current branch?

If you still have all of the commits name, simply git checkout <lost commit name>; git checkout -b <newbranch>; git checkout <yourbranch>; git rebase newbranch

Other git turorial

https://www.atlassian.com/git/tutorials/learn-git-with-bitbucket-cloud

--

--