git concepts
git concepts
Reference: Welcome to Learn Git Branching
Git Commits
A commit in a git repository records a snapshot of all the (tracked) files in your directory. It's like a giant copy and paste, but even better!
It's a lot to take in, but for now you can think of commits as snapshots of the project. Commits are very lightweight and switching between them is wicked fast!
Git Branches
Branches in Git are incredibly lightweight as well. They are simply pointers to a specific commit -- nothing more.
When we start mixing branches and commits, we will see how these two features combine. For now though, just remember that a branch essentially says "I want to include the work of this commit and all parent commits.
Git Merging
Merging is a kind of way of combining the work from two different branches together.
The first method to combine work that we will examine is git merge. Merging in Git creates a special commit that has two unique parents. A commit with two parents essentially means "I want to include all the work from this parent over here and this one over here, and the set of all their parents."
Git Rebase
Rebasing is another way of combining work between branches. Rebasing essentially takes a set of commits, "copies" them, and plops them down somewhere else.
While this sounds confusing, the advantage of rebasing is that it can be used to make a nice linear sequence of commits. The commit log / history of the repository will be a lot cleaner if only rebasing is allowed.
HEAD
First we have to talk about "HEAD". HEAD is the symbolic name for the currently checked out commit -- it's essentially what commit you're working on top of.
HEAD always points to the most recent commit which is reflected in the working tree. Most git commands which make changes to the working tree will start by changing HEAD.
Normally HEAD points to a branch name (like bugFix). When you commit, the status of bugFix is altered and this change is visible through HEAD.
- HEAD总是指向working tree上的最新commit
- 绝大多数修改working tree的命令都要先更改HEAD
- 通常HEAD总是指向一个branch,当你在某个分支上并且commit时,branch的状态会跟着改变(HEAD同时会跟着变化,因为上面提及HEAD总是指向working tree的最新commit)
- Detaching HEAD
Detaching HEAD just means attaching it to a commit instead of a branch. You can use the command like this: git checkout <commit>
- Relative refs
Specifying commits by their hash isn't the most convenient thing ever, which is why Git has relative refs.
Relative commits are powerful, but we will introduce two simple ones here:
-
Moving upwards one commit at a time with
^
-
Moving upwards a number of times with
~<num>
-
Branch forcing
One of the most common ways I use relative refs is to move branches around. You can directly reassign a branch to a commit with the -f option. So something like:
git branch -f main HEAD~3
moves (by force) the main branch to three parents behind HEAD.
Note: In a real git environment git branch -f command is not allowed for your current branch.
-
Reversing Changes in Git
There are two primary ways to undo changes in Git -- one is using git reset and the other is using git revert
- cherry-pick
- iteractive rebasing