Git


Statement


Some of the content and pictures in this article are excerpted from the CS-Notes. If there is infringement, please inform to delete


Common Commands


1. See more from the cheat sheet

CommandObjectDescription
git addfilenameAdd files to the staging area
git commit -mfilenameSubmit files in staging area to the repository
git commit –amend Modify the last submission (use “shift+zz” to exit; use “q!” to exit without saving)
git mvoriginal newModify the name of a file
git rmfilenameDelete files in both working directory and staging area (use “-f” to force delete)
git rm –cachedfilenameDelete files in the staging area only
   
git pull Fetch from and integrate with another repository or a local branch
git push Update remote refs along with associated objects
   
git log View submission logs
git reflog Show referentiable history versions
   
git resetversionRoll back the version to the staging area
git resetHEAD~nMove the HEAD pointer to the first “n” objects and roll back the objects to staging area
git reset –softHEAD~nMove the HEAD pointer to the first “n” objects only
git reset –hardHAED~nFurther restore the staging area files to working directory (overwrite the source code)
   
git diff Compare files in working directory and staging area
git diffversionCompare files in working directory and repository
git diffversion1 version2Compare 2 files in a repository
git diff –cachedversionCompare files in staging area and repository (only files in repository have “version”)
   
git branchbranch nameCreate a branch
git checkoutbranch nameSwitch to a branch
git mergebranch nameMerge changes from another branch
git branch -dbranch nameDelete a branch


Centralized & Distributed


1. Git is a distributed version control system, while SVN is centralized


2. Comparison between centralized and distributed:

 CentralizedDistributed
The copy of codeOnly the central serverEveryone’s computer
Work without central server×
Networking required×
Create and mergeSlow (copy a complete code)Fast


Central Server


1. The central server is used to exchange the changes of each user. Git can work without it, but the central server can run continuously which makes exchanges easier

  • For example, the Github is a central server


Workflow


1. After creating a new repository, the current directory becomes the workspace

2. There is a hidden directory “.git” in the workspace, which belongs to the Git version library

  • The Git version library includes a staging area called “Stage” and the latest “History” version library. “History” stores all branches’ information and uses a HEAD pointer to point to the current branch



Branch


1. Implement

  • Use the pointer to connect each submission into a timeline, and the HEAD pointer points to the current branch pointer


2. New

  • Create a new pointer to the last node of the timeline, and make the HEAD pointer point to the new branch, indicating that the new branch becomes the current branch


3. Commit

  • Each commit will only move the current branch pointer forward, while other pointers stay fixed


4. Merge

  • Simply change the pointer



Conflict


1. When two branches modify the same line of the same file, conflicts will occur during merging:


2. Git will use “<<<<<<<, =======, >>>>>>>” to identify contents of different branches. We only need to modify the conflicting parts to be the same to resolve the conflict

<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> feature1


Stashing


1. After operating on a branch, switch to another branch before the changes are committed, the changes can also be seen on the other branch. This is because all branches share a same workspace

2. We can use git stash to store the changes of the current branch

  • At this time, all the changes of the current workspace will be stored in the stack to keep the current workspace clean. As there are no uncommitted changes, we can safely switch to other branches
$ git stash
Saved working directory and index state \ "WIP on master: 049d078 added the index file"
HEAD is now at 049d078 added the index file (To restore them type "git stash apply")

3. Stashing can be used to implement the “bug branch”

  • If you are currently developing on the “dev branch”, but there is a bug on the “master branch”, and the development on the “dev branch” is incomplete to be submitted immediately. Before creating and switching to a new bug branch, you need to use git stash to store the uncommitted changes of “dev branch”