A brief of Git usage
Not so many documentations about Git mention about the remote-tracking references. Here we can revise how Git work with remote.
Git is distributed
We all know that Git works as a distributed model. That means, a repository, regardless it is on your machine or somewhere else, will contain all possible information. Data can be transfer between Git nodes. One Git can play the role of the server and others as clients, but there are no differences internally.
The git local and git remote
The below diagram has several common versions on the Internet. I want to summary these steps and emphasize the remote-tracking reference which is usually overlooked.
A straight route to the remote server
When you have work done, then the most common git commands in action would be the triad:
git add
git commit
git push
You do it daily when you understand well your task and when you work alone in a branch. In simple words, your code goes from your keyboard to the remote server.
The bridge between remote and local
There are two middle areas:
- The
local refs
is a buffer for your local before pushing - The
remote-tracking refs
is a buffer for your remote before pulling
Then, you will see the git commit
and git fetch
are symmetry and serving as two sizes of the bridge between local and remote repository. Right on that bridge, annoying conflicts might happen. If you do git pull
, first git fetch
will be executed, then the merging process and conflict solving will follow. Git pull
will let you fix your conflict a the code level, but git fetch
let you investigate the conflict (on the bridge) before any action.
"git pull" = "git fetch" AND [("git checkout" if no conflict) OR (git merge if there is any conflict)]
- You do
git fetch
to update the remote-tracking references, which means you won’t see any change in your repository yet but only download the remote data.
The backup area
The stash
area is not a common but still a useful feature. When you think your code is crap (yes it is) and you must delete it but hesitate to do so, then do git stash
to save you changes and reversed your unstaged work.
The best documentation of Git
The Internet created a large number of documents about it. I prefer using the official site, which is short and complete. Sometimes, Stackoverflow can work well to find a quick solution. Other resources, I think, only re-cook the material from the official site to make it more engageable to the reader. So this blog is, too.