Some Git cheatsheet

Xianbo QIAN
2 min readOct 25, 2019

--

I have been using Git from now and then for a while. Every time that I start using Github for some tasks, I feel like learning Git again. I’m thinking that it might be a good time to start putting things on my blog — you know what just happened!

A naïve Git development flow with co-workers (assuming everyone has the edit permission) is like the following:

  • Git clone the entire project
  • Make some local changes but don’t commit
  • Git stash then git pull to get the latest version and avoid conflicts
  • Then git stash pop to continue your work
  • Git commit then immediately git push to submit your code
  • If you run into race condition with your peer — they pushed the code before you then you might be in trouble.

To solve this issue, a better workflow which is more typical is the following:

  • Git clone
  • Create two local branches, one for working and one for keeping in sync
  • Make sure only work on the working branch, and make commits as you want.
  • Switch to sync branch, and pull latest code using git pull rebase
  • Rebase sync to work, and resolve all the commits, which can take some time.
  • Now you should have a stream line work branch, you can push it to remote!

And if anything happens, here are some comments coming to rescue:

Create empty branch:

git checkout --orphan empty-branch

Revert all local changes that are not submitted, including staged files

git reset --hard

Revert a file

# From head
git checkout FILENAME
# From a local branch
git checkout BRANCH FILENAME

Pull remote branch to a local branch

# This can be used to create a new branch or update local version
git pull ORIGIN REMOTE_BRANCH:LOCAL_BRANCH --rebase

--

--

No responses yet