Git Review Basics

Git Review Basic

https://learngitbranching.js.org/?locale=en_US

https://github.com/pcottle/learnGitBranching

A way of learning git with visualization.

Since this blog post, I will bring some of my personal life into the site to make it more vivid.

This index page image is one of the famous sceneries in my hometown –Chengdu, Sichuan, China. The mixture of green bamboo, red walls, and emerald tiles depict a beautiful, peaceful Chinese garden painting in a traditional Chinese style. It is reminiscent of my childhood in summer where I played with my friends in a park (called 梨园 Liyuan, removed years ago for the purpose to rebuild that part of the Chengdu city.)

Basic

Commit

  • git commit : simply a snapshot of all the tracked files in the directory

Branch

Branches are simply pointers to specific commit. No extra memory/ storage overhead with making many branches.

A branch can be translated as : want to include the work of this commits and all parent commits.

1
2
3
4
git branch [branchname]
git checkout [branchname]
# Equivalent to the following
git checkout -b [branchname]

Merging

Merge is to combine the work from two different branches together.

Meging creates a special commit that has two unique parents.

A commit with two parents can be translated as : want to include all the work from from these two parents and the set of all their parents.

Note that: git merge [branchname] is to take all the commits from another branch and merge with current branch!

Untitled

Rebasing

Rebasing is to take a set of commits, copy them, and plop them down somewhere else.

If currently we are on C3, and we dogit rebase main

Untitled

C3 will be the next commit of main, and that is essentially what rebase do. main is the ancestor of bugFix.

then we will do the git rebase bugFix on main . Git notice main is the ancestor of bugFix so it simply moves main branch reference forward in history.

Untitled

Rebasing can be translated to: reallocate current branch to be the next branch of some branch so that a sequential commit history is shown.

HEAD: A symbolic name for the currently checked out commit, it’s essentially what commit you’re working on top of.

HEAD can be translate to your current position in the tree

  • HEAD always points to the most recent commit branch.
  • HEAD points to a branch name such as bugFix. When commit, the status of bugFix is changed through looking at HEAD

To detach HEAD means to go back to a specific commit (not specific branch though, a branch name can be seen as the default HEAD of that branch)

1
git checkout [commitHash]

Relative Refs

  • Moving upwards one commit at a time with ^ (Caret)
  • Moving upwards a number of times with ~<num>
1
2
# find the last (parent) commit of main, set HEAD there
git checkout main^

Untitled

1
2
3
4
5
# (find parent and set HEAD there) x3
git checkout C3
git checkout HEAD^
git checkout HEAD^
git checkout HEAD^

Untitled

1
2
# Go 4 levels up the tree!
git checkout HEAD~4

Branch forcing

Notice that this only reassign different branch name onto another commit, but not changing the work tree.

1
2
3
# move a branch to a specific place by force
# git branch -f [branchname] [destination]
git branch -f main HEAD~3

Reversing Changes

git reset

Reset is to reverse changes by moving a branch reference backwards in time to an older commit.

git reset can be translated as rewrite the history as if the commit had never been made in the first place.

use git reset when you have committed in your local machine but wants to discard it.

1
2
# git reset [destination]
git reset HEAD~1

git revert

git reset works well on local machine ,but it cannot share this reset with remote repo

git revert can do this job.

git revert introduces a new changes that happens to reset all changes made by C2.

Untitled

Intermediate

Git Review Intermediate


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!