Git Basics
与其他版本控制系统的区别
直接记录快照,而非差异比较
CVS SVN等记录文件差异
Git记录文件快照
文件三种状态
Main components of a Git repository
Commit-level Operation
Reset
For example, the following command moves the hotfix branch backwards by two commits.
For example, the following command moves the hotfix branch backwards by two commits.
git checkout hotfix
git reset HEAD~2
In addition to moving the current branch, you can also get git reset to alter the staged snapshot and/or the working directory by passing it one of the following flags:
- –soft – The staged snapshot and working directory are not altered in any way.
- –mixed – The staged snapshot is updated to match the specified commit, but the working directory is not affected. This is the default option.
- –hard – The staged snapshot and the working directory are both updated to match the specified commit.
Checkout
git checkout hotfix
git checkout HEAD~2
Revert
Reverting undoes a commit by creating a new commit. This is a safe way to undo changes, as it has no chance of re-writing the commit history. For example, the following command will figure out the changes contained in the 2nd to last commit, create a new commit undoing those changes, and tack the new commit onto the existing project.
git checkout hotfix
git revert HEAD~2
File-level Operations
Reset
git reset HEAD~2 foo.py
Checkout
git checkout HEAD~2 foo.py
Summary
Command | Scope | Common use cases |
---|---|---|
git reset | Commit-level | Discard commits in a private branch or throw away uncommited changes |
git reset | File-level | Unstage a file |
git checkout | Commit-level | Switch between branches or inspect old snapshots |
git checkout | File-level | Discard changes in the working directory |
git revert | Commit-level | Undo commits in a public branch |
git revert | File-level | (N/A) |