管理修改
说明
Git跟踪并管理的是修改, 而不是文件. 这是比其他版本控制系统优秀的地方
修改
创建一个文件
新增一行
删除一行
更改某些字符
删除一个文件
示例
一
流程
1. 增加一行
2. git add
3. 再新增一行
4. git commit
操作
$ git status
On branch master
nothing to commit, working directory clean
$ cat hello.txt
the first line: I like git!
the second line: Git is powerful~
$ echo 'add a new line, 111' >> hello.txt
$ cat hello.txt
the first line: I like git!
the second line: Git is powerful~
add a new line, 111
$ git add hello.txt
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: hello.txt
$ echo 'add the second line, 222' >> hello.txt
$ cat hello.txt
the first line: I like git!
the second line: Git is powerful~
add a new line, 111
add the second line, 222
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: hello.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: hello.txt
$ git commit -m "add a new line"
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git diff HEAD -- hello.txt
diff --git a/hello.txt b/hello.txt
index 94a4e36..15ee095 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1,3 +1,4 @@
the first line: I like git!
the second line: Git is powerful~
add a new line, 111
+add the second line, 222
说明
第一次修改 add 到 stage, 第二次修改 没有 add到stage,
最后 commit 到 master分支的是第一次修改的
再次 add+commit 可把修改合并到master分支
修改后, 如果不add到stage, 就不会加入到commit
二
流程
1. 增加一行
2. git add
3. 再新增一行
4. git add
5. git commit
说明
两次修改会合并后, 再提交到 分支
撤销修改
命令
git checkout -- <file>...
作用
use "git checkout -- <file>..." to discard changes in working directory
说明
流程
修改
git checkout -- hello.txt
还原到修改前
流程
第一次修改
git add
第二次修改
git checkout -- hello.txt
还原到第一次修改后
补充
git checkout -- <file>... 是用版本库最新版本替代工作区的版本
撤销暂存
命令
git reset HEAD <file>...
作用
use "git reset HEAD <file>..." to unstage
注意
$ git reset --hard HEAD^
删除
命令
git rm <file>...
示例
git rm hello.txt
git commit hello.txt -m "delete hello.txt"
总结
git checkout -- <file>...
git reset HEAD <file>...
$ git reset --hard f69a305be8d203da3