主要是供自己看 学习 git 请参考:https://www.liaoxuefeng.com/wiki/896043488029600
首先创建仓库 (版本库),仓库可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改、删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻可以“还原”。
步骤:
$ mkdir git_repository
$ cd git_repository
$ pwd # 此步骤是为了查看当前路径,和下面做比较
/d/git_repository
$ git init
Initialized empty Git repository in D:/git_repository/.git/
# 观察上述输出。此时git已经把仓库建好了,并且是一个空仓库。
# 同时,还多出了一个 .git 目录,它用来跟踪管理版本库。.git默认是隐藏
然后就可以添加文件了,如 readme.txt
。
Git is a version control system.
Git is free software.
一定要放到 git_repository
目录下(子目录也行),因为这是一个Git仓库,放到其他地方Git再厉害也找不到这个文件。
把文件添加到仓库:
$ git add readme.txt
提交到仓库:
$ git commit -m "wrote a readme file"
[master (root-commit) 8bf6109] wrote a readme file
1 file changed, 2 insertions(+)
create mode 100644 readme.txt
git commit
中,-m
参数是本次提交的说明。它成功后会告诉你:1 file changed
:一个文件被改动;2 insertions
:插入了两行内容。
将添加文件分为两步是因为,commit
可以一次提交很多文件,所以你可以多次add
不同的而文件,如:
$ git add file1.txt
$ git add file2.txt file3.txt
$ git commit -m "add 3 files."
git status
命令可以知道仓库的当前状态。如更改 readme.txt 内容如下:
Git is a distributed version control system.
Git is free software.
然后:
$ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
它输出说明,readme.txt 被修改了,但是还没有准备提交的修改。
通过 git diff
可以查看具体修改了什么内容:
$ git diff
diff --git a/readme.txt b/readme.txt
index d8036c1..013b5bc 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a distributed version control system.
Git is free software.
\ No newline at end of file
可以以相同的方式 (add,然后 commit) 将文件提交仓库
$ git add readme.txt
$ git commit -m "add distributed"
[master 15bdeba] add distributed
1 file changed, 1 insertion(+), 1 deletion(-)
$ git status # 提交后再次查看
On branch master
nothing to commit, working tree clean
# 上述输出git告诉我们当前没有需要提交的修改,而且,工作目录是干净(working tree clean)的。
再次修改 readme.txt 如下:
Git is a distributed version system.
Git is free software distributed under the GPL;
然后重复之前的步骤:
$ git add readme.txt
$ git commit -m "append GPL"
[master 0acb05e] append GPL
1 file changed, 2 insertions(+), 2 deletions(-)
此时,readme.txt 共有三种版本:wrote a readme file、add distributed、append GPL。(准确的说是,commit 有三种版本)
git log
供我们查看历史记录
commit 0acb05e2307a0a42affac1a7436a6dc4b62d95ac (HEAD -> master)
Author: hjm <[email protected]>
Date: Thu Jun 25 11:08:48 2020 +0800
append GPL
commit 15bdeba5f5af7a407d83c49a92f9c2aa4da134f5
Author: hjm <[email protected]>
Date: Thu Jun 25 11:02:05 2020 +0800
add distributed
commit 8bf6109512877d3fce6e257e417ccf0522b4721f
Author: hjm <[email protected]>
Date: Thu Jun 25 11:00:17 2020 +0800
wrote a readme file
它显示的是从近到远的提交日志。
如果嫌输出信息太多,可以加上 --pretty=oneline
参数:
$ git log --pretty=oneline
0acb05e2307a0a42affac1a7436a6dc4b62d95ac (HEAD -> master) append GPL
15bdeba5f5af7a407d83c49a92f9c2aa4da134f5 add distributed
8bf6109512877d3fce6e257e417ccf0522b4721f wrote a readme file
一大串类似
0acb05e...
的是commit id
(版本号)
在 git 中,用 HEAD
表示当前版本,上一个版本是 HEAD^
,上上个版本是 HEAD^^
,当回退的版本较多时,可以写成 HEAD~100
(表示回退100个版本)
可以使用 git reset
回退版本:
$ git reset --hard HEAD^
HEAD is now at 15bdeba add distributed
--hard
参数后叙。此时 readme.txt
的内容已经回退。同时,git log
也看不见 append GPL
版本了:
$ git log
commit 15bdeba5f5af7a407d83c49a92f9c2aa4da134f5 (HEAD -> master)
Author: hjm <[email protected]>
Date: Thu Jun 25 11:02:05 2020 +0800
add distributed
commit 8bf6109512877d3fce6e257e417ccf0522b4721f
Author: hjm <[email protected]>
Date: Thu Jun 25 11:00:17 2020 +0800
wrote a readme file
只要当前的命令行窗口还没有关掉,仍然可以通过 commit id
来回到未来(当前是 appen distributed) 的版本。
$ git reset --hard 0acb05
HEAD is now at 0acb05e append GPL
此时,可以发现 readme.txt 内容回到了 applen GPL 版本。指定 commit id
时,不用全部写,前几位就可以了,git 会自动寻找。
其实,你已经可以感觉到,版本的更改速度很快,因为它是更改的
HEAD
指针。
而且,git 中还提供了 git reflog
用来记录你的每一次命令。即使你的文件回退到了某个版本后,并且关闭了电脑,也仍然可以通过此命令来获得各版本的 commit id
$ git reflog # 命令的顺序是按时间从近到远
0acb05e (HEAD -> master) HEAD@{0}: reset: moving to 0acb05
15bdeba HEAD@{1}: reset: moving to HEAD^
0acb05e (HEAD -> master) HEAD@{2}: commit: append GPL
15bdeba HEAD@{3}: commit: add distributed
8bf6109 HEAD@{4}: commit (initial): wrote a readme file
工作区可以理解电脑里的目录,如 git_repository。而工作区的一个隐藏目录 .git
,这是 git 的版本库,其中最重要的就是称为 stage (或 index) 的暂存区,和 git 自动创建的第一个分支 master
,以及一个指向 master
的指针 HEAD
。(这里先不说 分支和 HEAD
的概念)
命令 git add
实际上是把文件添加到暂存区;而 git commit
提交更改,实际是把暂存区的所有内容提交到当前分支。
假设现在修改 readme.txt
文件,其内容添加一行 Git has a mutable index called stage.
,同时再新增一个文件 LICENSE.txt
,内容随便写。
然后 git status
:
$ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: readme.txt
Untracked files:
(use "git add ..." to include in what will be committed)
LICENSE.txt
no changes added to commit (use "git add" and/or "git commit -a")
它会提示 readme.txt
已经修改,同时 LICENSE.txt
的状态为 Untracked
,因为它从来没有被添加过。(此时的 readme.txt
和 LICENSE.txt
都处于工作区)
当执行 git add
,便把所有提交的修改放到暂存区
$ git add readme.txt LICENSE.txt
$ git status
On branch master
Changes to be committed:
(use "git restore --staged ..." to unstage)
new file: LICENSE.txt
modified: readme.txt
使用 git commit
一次性将暂存区的所有修改提交到分支:
$ git commit -m "understand how stage works"
[master 36d9585] understand how stage works
2 files changed, 3 insertions(+), 1 deletion(-)
create mode 100644 LICENSE.txt
# 如果没有对工作区做任何修改,那么工作区就是“干净”的
$ git status
On branch master
nothing to commit, working tree clean
git 跟踪和管理的是修改,而不是文件。
在 readme.txt
中添加一行 Git tracks changes.
,然后 git add
;将 readme.txt
最后一行改为 Git tracks changes of files
,接着执行 git commit
,查看状态:
$ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
因为第二次修改并没有通过 git add
提交到暂存区,所以 git commit
就只提交了第一次修改。
git diff
查看工作区与版本库里最新版本的区别:
$ git diff HEAD -- readme.txt
diff --git a/readme.txt b/readme.txt
index 79a5990..7f94e60 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,4 +1,4 @@
Git is a distributed version system.
Git is free software distributed under the GPL;
Git has a mutable index called stage.
-Git tracks changes. # 版本库中
\ No newline at end of file
+Git tracks changes of files. # 工作区
\ No newline at end of file
再次 git add
,然后 git commit
我们知道,当工作区文件更改后,git status
可以获得状态。那么如何撤销修改呢?通过git checkout -- filename
,该命令有两种情况:
filename
自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;filename
已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。此时再 git status
,git 将提示working tree clean
注意,命令中的
--
再有和没有时,是两种不同的命令。
在 git 中,删除也是一个修改操作。当我们在文件管理器中删除了文件,然后 git status
,git 会告诉你哪些文件删除了。此时工作区和版本库不一致了,此时的操作可以有两种:
git rm
从版本库中删掉,并且使用 git commit
git checkout -- filename
进行恢复。git checkout
其实是版本库的版本替换工作区的版本,无论是修改还是删除。
github 网站提供远程仓库托管服务。由于 github 与本地 git 仓库之间的传输通过 SSH 加密,所以还需要做以下操作:
$ ssh-keygen -t rsa -C "[email protected]"
然后一直回车即可。此时,会有一个 .ssh
目录,里面包含 id_rsa
和 id_rsa.pub
两个文件,分别对应私钥和公钥。然后将公钥添加到 github 上的 ssh keys 中。
ssh key 是让 github 识别出推送的提交是你本人,当有多台电脑需要提交到这个仓库时,只需要都做上述操作,然后把公钥添加进去即可。
推送
通过以下命令将本地仓库与远程 git 仓库关联:
$ git remote add origin [email protected]:YourGithubName/YourRepositoryName.git
# 如果之后出现 类似 远程仓库不存在的错误,那么先删除 origin 然后重新 执行上述语句即可
# git remote rm origin
添加后,远程库的名字就是 origin
,一般都使用这个。
然后通过 git push
将内容推送到远程库上:
$ git push -u origin master
git push
实际是将当前分支 master
推送到远程。第一次推送 master
时,需要加上 -u,这样 git 会推送内容之外,同时把本地的 master
分支与远程的 master
关联起来。之后的推送或拉去时就可以简化命令:git push origin master
。
注意,是将当前的分支进行推送。所以当你在本地仓库删除文件后,如果不是误删,记得使用 git rm
,然后再推送!!!见上一节
克隆
git clone [email protected]:GithubName/RepositoryName.git