1. 删除文件
除了用 "rm" 删除工作目录中的文件外,还得用 "git rm <file>" 删除代码仓库中的文件。
$ rm INSTALL $ git status # On branch master # Changed but not updated: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # deleted: INSTALL # no changes added to commit (use "git add" and/or "git commit -a") $ git rm INSTALL rm 'doc/INSTALL' $ git commit -am "rm INSTALL" [master 8600e47] rm INSTALL 0 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 doc/INSTALL
当然,版本管理工具的一个好处就是有后悔药卖。
$ git checkout HEAD^ -- INSTALL $ ls INSTALL README $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: INSTALL #
如果仅此仓库移除,但保留工作目录中的文件,可以直接用 "git rm --cached <file>",遗留的文件会变成未跟踪状态。
2. 移动文件
和删除文件的做法类似。
$ mv HISTORY doc/ $ git status # On branch master # Changed but not updated: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # deleted: HISTORY # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # doc/HISTORY no changes added to commit (use "git add" and/or "git commit -a") $ git add . $ git commit -am "mv HISTORY" [master 716af03] mv HISTORY 1 files changed, 0 insertions(+), 0 deletions(-) rename HISTORY => doc/HISTORY (100%)
3. 重新提交
如果最后一次的提交需要修正什么,那么可以用 "--amend" 参数。
$ touch a.txt
$ git add .
$ git commit -am "b.txt"
[master b66690c] b.txt
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
yuhen@yuhen-desktop:~/myproject$ git log
commit b66690cc4353e95fa52cf6cf2e8a3210c1ace057
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date: Tue Apr 20 19:37:23 2010 +0800
b.txt
commit 716af03e2ebafd8c47bf941e0185b8b67adcd20e
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date: Tue Apr 20 19:35:17 2010 +0800
move HISTORY
很显然,注释 "b.txt" 写错了。重来吧~~~
$ git commit --amend -am "a.txt"
[master a93a7cd] a.txt
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
$ git log
commit a93a7cdea6d9344d975c58332064ae48b29fb68f
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date: Tue Apr 20 19:37:23 2010 +0800
a.txt
commit 716af03e2ebafd8c47bf941e0185b8b67adcd20e
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date: Tue Apr 20 19:35:17 2010 +0800
move HISTORY
最后一条提交日志被替换了。
4. 恢复单个文件
可以用 "git checkout ..." 签出以前的某个 "提交版本" 。
$ cat main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
printf("Hello, World!/n");
return EXIT_SUCCESS;
}
$ git show HEAD^ main.c
commit 65bd689c3a2b97c598ee2e66c453cc526b63b892
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date: Tue Apr 20 19:52:16 2010 +0800
add main.c
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..c50fab4
--- /dev/null
+++ b/main.c
@@ -0,0 +1,9 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int main(int argc, char* argv[])
+{
+ return EXIT_SUCCESS;
+}
+
$ git checkout HEAD^ -- main.c
$ cat main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
return EXIT_SUCCESS;
}
也可以用 "git reset HEAD <file>" 重置已添加到暂存区(stage)但未提交(commit)的文件。
4. 查看文件详细信息
用 "git show" 查看某个提交版本的具体信息,或者 "git diff" 比较差异。
$ git show main.c
commit 017c106d3bb9c423dead6d732db1651119b6423c
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date: Tue Apr 20 20:04:12 2010 +0800
update main.c
diff --git a/main.c b/main.c
index c50fab4..a6bb490 100644
--- a/main.c
+++ b/main.c
@@ -4,6 +4,7 @@
int main(int argc, char* argv[])
{
+ printf("Hello, World!/n");
return EXIT_SUCCESS;
}
$ git diff HEAD main.c
diff --git a/main.c b/main.c
index a6bb490..c50fab4 100644
--- a/main.c
+++ b/main.c
@@ -4,7 +4,6 @@
int main(int argc, char* argv[])
{
- printf("Hello, World!/n");
return EXIT_SUCCESS;
}
5. 查看提交日志
"git log -3" 查看最后 3 条提交信息。
$ git log -3
commit 017c106d3bb9c423dead6d732db1651119b6423c
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date: Tue Apr 20 20:04:12 2010 +0800
update main.c
commit 65bd689c3a2b97c598ee2e66c453cc526b63b892
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date: Tue Apr 20 19:52:16 2010 +0800
add main.c
commit 3725c8d49a61bb6d0bb18d7727fcaafe32f510fc
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date: Tue Apr 20 16:55:50 2010 +0800
init
还可以用 "--stat" 显示简单的提交统计信息。
$ git log -3 --stat
commit 017c106d3bb9c423dead6d732db1651119b6423c
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date: Tue Apr 20 20:04:12 2010 +0800
update main.c
main.c | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
commit 65bd689c3a2b97c598ee2e66c453cc526b63b892
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date: Tue Apr 20 19:52:16 2010 +0800
add main.c
main.c | 9 +++++++++
1 files changed, 9 insertions(+), 0 deletions(-)
commit 3725c8d49a61bb6d0bb18d7727fcaafe32f510fc
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date: Tue Apr 20 16:55:50 2010 +0800
init
.gitignore | 5 +++++
1 files changed, 5 insertions(+), 0 deletions(-)
参数 "-p" 显示修改的详细信息。
$ git log -1 -p
commit 017c106d3bb9c423dead6d732db1651119b6423c
Author: Q.yuhen <yuhen@yuhen-desktop.(none)>
Date: Tue Apr 20 20:04:12 2010 +0800
update main.c
diff --git a/main.c b/main.c
index c50fab4..a6bb490 100644
--- a/main.c
+++ b/main.c
@@ -4,6 +4,7 @@
int main(int argc, char* argv[])
{
+ printf("Hello, World!/n");
return EXIT_SUCCESS;
}
6. 初始化全局设置
用户名、联系方式以及着色显示都很要紧。
$ git config --global user.name "Q.yuhen" $ git config --global user.email qyuhen@abc.com $ git config --global color.ui true
可以用 "--list" 参数查看全局设置。
$ git config --list user.name=Q.yuhen user.email=qyuhen@abc.com color.ui=true core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true
更多设置可参考 《Pro Git: 配置 Git》。
本文详细介绍了使用Git进行文件管理的基本操作,包括删除、移动文件以及如何使用命令进行撤销更改、查看文件历史版本等。同时,阐述了如何通过`git checkout`和`git reset`命令恢复特定版本的文件,以及如何使用`git log`和`git show`命令查看和比较文件历史版本。此外,还介绍了如何使用`git add`和`git commit`进行文件的添加和提交,以及如何利用`--amend`参数进行提交内容的修正。
176

被折叠的 条评论
为什么被折叠?



