Git Learning Notes

Version Control System

Posted by Fan Gong on Sep 23, 2018

I have been using Git for at least one year. To make it easy, I always used Github Desktop application to help me handle everything in order not to remember all the commands.

But as the project getting larger and larger than before, people from different functionality are joining and revising the scripts for one project, it is very necessary for me to review the git theory under the hood and make sure what I did is correct. (Especially it is said yesterday that someone was killed by his colleagues since he always type git push --force)

The book that I've read to review is Git's official book "Pro Git". This is really good git tutorial book. So this post will not cover detailed information, instead it will summarize Git's theory.

1. Git Basics

Git is a version-control system for tracking changes in computer files and coordinating work on those files among multiple people

I would like to explain how does git work in one graph:

The Upper part gives us a structure view of a git project. We had remotes, branches and different stages while we manage the files within each branch.

From top to bottom:

  • Remote repositories are versions of our project that are hosted on the internet or network somewhere. One repository can have several branches.
  • Branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master.
  • Three main sections of a git local branch:
    • Working Directory: A single checkout of one version of the project.
    • Staging Area: What will go into your next commit.
    • Git Directory: Where git stores the metadata and object database for our project

The most works we do are under our local folder tracked with one remote branch, to zoom in this whole process, let's see the lower part of the graph:

This part tells us the states that our files can reside in:

  • Untracked Files: The files that Git doesn't take it into account. In another word, the files that in the working directory but neither in staging area not .git directory.
  • Tracked Fils: Other files that have been tracked by Git. Within the tracked files, it includes modified or unmodified files.
  • Unmodified: Just as its name implies, it is the original tracked files from .git directory.
  • Modified: The tracked files that has been edited. We can then decide to move it into the staging area or not.
  • Upstaged: The Modified files but not want to be committed for this change.
  • Staged: The modified files that will go into our next commit.

So the whole work flow is:

  1. We add the untracked files into out .git directory to make them trackable.
  2. We modify the files that make them to modified stage.
  3. We choose the modified file that want to commit this time into staged state.
  4. We commit the changes and record it in .git directory; and then transform the staged files into unmodified tracked files.

2. Git Branching

Git branching is very easy to understand. Basically, it means we can use different branch to diverge from main line of development and continue to do work without messing with the main line.

Among all the procedures of Git Branching, one thing needs to be noticed is the process of branching merging.

I will also use one graph to explain it:

Git uses a method called three-way merge, essentially it uses the two snapshots pointed to by the branch tips and the common ancestor of the two, and then creates a new snapshot.

3. Git Commands

I will not list all the basic commands here, instead I will list some command that is very useful but may not that common.

  1. Restore from previous version: There are two ways to restore from a specific version.

    • Use git reset: git reset --hard <commit hash> One thing needs to be noticed that after using this command, all commits that come after this version are effectively undone...So, in most cases I recommend to use the next method.
    • Use git checkout: git checkout -b <new branch name> <commit hash>. We know git checkout normally used to change branch but it can also create new branch in certain version.
  2. Useful Extension of git log: we know git log will show all the logging information for git commits. But sometimes it looks very redundant, so here I provides several extensions for clear versions of logging information.

    • Use git log --stat. It will tell us how many files and lines did we change
    • Use git log --pretty=oneline It will only show each commit in one line with commits comment.
    • Use git log --since(or after)="yyyy-mm-dd". Logging information before or after some specific date.
  3. Difference between git pull and git fetch

    • git fetch will only fetch down all the changes on the server, but it doesn't modify the working directory.
    • git pull will fetch the data and then merge that remote branch into your current branch.