快捷搜索:  汽车  科技

怎么熟练使用git进行版本控制?程序员必须掌握版本控制的工具

怎么熟练使用git进行版本控制?程序员必须掌握版本控制的工具· Git仓库:这个是git用来保存项目数据 meta数据和对象数据库的地方。是git中的核心。我们使用git clone命令克隆仓库时就是复制的这里的数据。· 暂存区:Git官方术语叫“索引”。平常大家都叫暂存区。暂存区本质是一个文件,用来保存下次commit到git仓库的文件列表信息。· 已暂存(staged) 表示对一个已修改文件的当前版本做了标记,使之包含在下次提交的快照中。对应于三个状态 git就有了三个工作阶段,如下图所示:· 工作区:就是你checkout出来的内容,你的原始工作都是在工作区进行的。

Git是一个分布式代码和文件版本管理和控制系统。git的诞生源于linux内核的版本控制。2005 年之前linux内核源码使用BitKeeper来进行版本控制,到了2005年,BitKeeper的开发公司终止了其合作,无奈Linus Torvalds开发了git。如果git已经成了绝大多数公司进行版本控制的首选。下面我们就来来学习一下Git的基本知识吧。

在git 中有三种状态,已提交(committed) 已修改(modified) 和已暂存(staged)。

· 已提交(committed) 已提交表示数据已经安全地保存在本地数据库中。

· 已修改(modified) 已修改表示修改了文件,但还没保存到数据库中。

· 已暂存(staged) 表示对一个已修改文件的当前版本做了标记,使之包含在下次提交的快照中。

对应于三个状态 git就有了三个工作阶段,如下图所示:

怎么熟练使用git进行版本控制?程序员必须掌握版本控制的工具(1)

· 工作区:就是你checkout出来的内容,你的原始工作都是在工作区进行的。

· 暂存区:Git官方术语叫“索引”。平常大家都叫暂存区。暂存区本质是一个文件,用来保存下次commit到git仓库的文件列表信息。

· Git仓库:这个是git用来保存项目数据 meta数据和对象数据库的地方。是git中的核心。我们使用git clone命令克隆仓库时就是复制的这里的数据。

使用git工作的基本流程:

1. 首先你是在工作区完成对文件或者代码的修改

2. 将你下次想要提交的文件,选择性地暂存

3. 提交更新到git仓库

假设你首先执行了git checkout命令,然后你在工作区修改了a.c文件,那么这时候 a.c就处于“已修改“状态”。如果你又执行了git add命令将a.c添加到了暂存区,那么这时候a.c就处于“已暂存”状态。如果你继续运行git commit命令将a.c提交到了本地git仓库那么a.c就处于“已提交”状态。

我们下面一起学习一下git的具体使用方法:

1. 创建本地仓库

首先cd 到自己的project目录,然后运行下面的命令:

$ git init

执行完上述命令后,会输出下面的语句:

Initialized empty Git repository in D:/gitExample/.git/

红色部分会有不同,就是是自己的项目目录。这时你会发现当前项目文件夹内多了一个.git子目录:

$ ls -la total 28 drwxr-xr-x 1 coder 1049089 0 Mar 27 11:42 ./ drwxr-xr-x 1 coder 1049089 0 Mar 27 11:39 ../ drwxr-xr-x 1 coder 1049089 0 Mar 27 11:42 .git/

这个git子目录里就是你初始化git仓库所有必须的文件 请不要修改和删除它们。刚刚我们仅仅做了一个初始化动作 git还没有追踪你的项目文件。我们可以使用git status查看当前文件的状态,由于我们的项目还没有任何文件被追踪,所以当我们敲入git status后会得到如下输出:

$ git status On branch master No commits yet nothing to commit (create/copy files and use "git add" to track)

我现在添加一些如下文件:

$ touch a.h a.c readme

添加完成后,(注意,此时三个文件都是空的。)我们再次使用git status来查看当前项目文件的状态。可以得到如下输出:

$ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) a.c a.h readme nothing added to commit but untracked files present (use "git add" to track)

我们可以看到git告诉我们有三个文件当前的状态是Untracked的状态,Untracked状态的文件表明git之前的提交当中没有这些文件。同时git也提示我们用git add的命令来将untracked的文件加入tracking。

我们运行:

$ git add .

git add 命令后面可以跟文件或者跟目录,跟目录时表示添加该目录下的所有文件。如上面命令中的”.”表示当前目录。

运行完上面的git add命令后,我们再来看一下当前文件的状态:

$ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: a.c new file: a.h new file: readme

我们看到a.c a.h 和readme文件已经被track了。同时我们看到了一句话” Changes to be committed” 这句话就说明了当前的三个文件已经是暂存状态了。此时我们可以运行git commit命令进行提交了。下面我们提交文件到git 仓库。

$ git commit

运行上面的git commit命令之后就会启动一个文本编辑器让你来输入一些comments。在文本编辑器里会出现下面所示的内容:

# Please enter the commit message for your changes. Lines starting # with '#' will be ignored and an empty message aborts the commit. # # Committer: Coder # # On branch master # # Initial commit # # Changes to be committed: # new file: a.c # new file: a.h # new file: readme #

我输入了”commit test”,然后输入wq(我这里启动的是vim编辑器,这个启动的编辑器是可以配置的。使用git config –global core.editor命令来配置你选择的文本编辑器)保存退出后,得到下面的信息:

[master (root-commit) 1659a0d] commit test Committer: coder Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly. Run the following command and follow the instructions in your editor to edit your configuration file: git config --global --edit After doing this you may fix the identity used for this commit with: git commit --amend --reset-author 3 files changed 0 insertions( ) 0 deletions(-) create mode 100644 a.c create mode 100644 a.h create mode 100644 readme

表明提交成功。好了一个基本的git使用流程就演示结束了。

假设我们现在想修改文件了,我们来修改一个文件试试看。修改前先查看一下状态:

$ git status On branch master nothing to commit working tree clean

我们可以看到,当前我们在master分支上。现在假设我向readme文件中添加了文字”Just a test readme file”。那么我们再次查看文件状态:

$ 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: readme no changes added to commit (use "git add" and/or "git commit -a")

我们看到readme文件的状态变为modified状态。同时我们看到一句话” Changes not staged for commit” 表明readme文件已经被更改,内容发生变化但是还没有放入暂存区。我们依然可以使用git add将更改后的readme暂存。注意git add功能很强大,不仅可以将处于untracked的文件直接放入暂存区,也可以将modified状态的文件加入暂存区,它还可以把合并分支时有冲突的文件标记为已解决。我们运行下面的git add 命令来将readme文件暂存。

$ git add readme

运行完成后,我们再次查看文件状态:

$ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: readme

我们可以看到一句话“Changes to be committed”,也就是说readme已经在暂存区了。我们可以使用git commit提交它到git仓库了。

上面我们每次使用git status命令来查看文件状态时总会输出很多东西,现在我来给它-s参数让它输出的简化一些:

$ git status -s M readme

我们看到,其输出简化了很多。

我也可以查看当前暂存的文件与上一次提交的文件的差异,可以使用git diff --staged命令。如下所示:

$ git diff --staged diff --git a/readme b/readme index e69de29..62cdd93 100644 --- a/readme b/readme @@ -0 0 1 @@ Just a test readme file

我们可以看到,git diff给出了a.h的改动内容。git diff不加任何参数只显示已经修改,但是尚未暂存的改动。如果已经暂存 git diff不加参数的情况下你看不到任何改动的信息。

我们再次使用git status查看一下当前的状态。

$ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: readme 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: a.h

我们可以看到 reame文件已经在暂存区,而a.h文件还没有提交到暂存区。有时候我们比较有把握,那么我们其实可跳过暂存区,直接commit到git 仓库。使用git commit -a 命令即可。如下演示:

$ git commit -a -m "Commit a.h file to git Repo directly" [master 9a8261a] commit a.h file to git Repo directly Committer: Coder Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly. Run the following command and follow the instructions in your editor to edit your configuration file: git config --global --edit After doing this you may fix the identity used for this commit with: git commit --amend --reset-author 2 files changed 2 insertions( )

上面的git commit 命令中我们除了使用了-a参数来跳过暂存区,我们也使用了-m参数可以让我们直接输入comments而不用打开文本编辑器。

假设我们现在不想要readme文件了, 但是它已经在git仓库里了 怎么将其删除呢?

Git中如果要删除一个文件,那么必须将文件从tracking清单中移除,也就是从暂存区移除,然后再进行commit提交。我们可以使用git rm命令完成这项工作。现在我们来删除readme文件如下:

$ git rm readme rm 'readme' $ git commit -m "delete readme file" [master 2107c54] delete readme file Committer: Coder Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly. Run the following command and follow the instructions in your editor to edit your configuration file: git config --global --edit After doing this you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed 1 deletion(-) delete mode 100644 readme

我们可以看到readme文件被删除了。

我们已经提交了几次更改了,如果我们想看看我们的提交历史记录 我们可以使用git log命令,如下:

$ git log commit 2107c5441a0c281c2c0b9aabaad6407e808d0328 (HEAD -> master) Author: Coder Date: Sun Mar 27 13:47:44 2022 0800 delete readme file commit 9a8261a35333573466f01a5a23067a69291528f9 Author: Coder Date: Sun Mar 27 13:39:25 2022 0800 Commit a.h file to git Repo directly commit 1659a0dbe5671f6bd08a4b033ac22f215cdb6b74 Author: Coder Date: Sun Mar 27 12:54:09 2022 0800 commit test

我们看到git列出了我们的提交历史记录。

我们继续探索git的基本用法。我们难免有时希望撤销我们的操作。但是在git中有些操作是不可逆的。我们来看看几个撤销操作的命令。

1. git commit --amend

这个命令会将暂存区中的文件提交,如果暂存区有改动的内容的话这个命令的提交结果就会替代上一次提交的结果。比如,我刚刚提交完,但是发现a.c改错了,我要重新改一下。下面演示一下:

$ git commit -m "Wrong modifed" [master ebd3a3b] Wrong modifed Committer: Coder Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly. Run the following command and follow the instructions in your editor to edit your configuration file: git config --global --edit After doing this you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed 1 insertion( )

上面提交了a.c的一次错误修改。现在我们改正a.c后使用git commit –amend命令来覆盖上一次提交的信息。

$ git commit --amend -m "Right modified" [master 4a20982] Right modified Date: Sun Mar 27 14:01:18 2022 0800 Committer: Coder Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly. Run the following command and follow the instructions in your editor to edit your configuration file: git config --global --edit After doing this you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed 1 insertion( )

我们现在使用git log来看一下:

$ git log --stat commit 4a2098213a2e6174d746cd618c7aa56c4215835f (HEAD -> master) Author: Coder Date: Sun Mar 27 14:01:18 2022 0800 Right modified a.c | 1 1 file changed 1 insertion( ) commit 2107c5441a0c281c2c0b9aabaad6407e808d0328 Author: Coder Date: Sun Mar 27 13:47:44 2022 0800 delete readme file readme | 1 - 1 file changed 1 deletion(-) commit 9a8261a35333573466f01a5a23067a69291528f9 Author: Coder Date: Sun Mar 27 13:39:25 2022 0800 Commit a.h file to git Repo directly a.h | 1 readme | 1 2 files changed 2 insertions( ) commit 1659a0dbe5671f6bd08a4b033ac22f215cdb6b74 Author: Coder Date: Sun Mar 27 12:54:09 2022 0800 commit test

从上面的输出我们可以看到我对a.c的提交只有一次记录comments 是” Right modified” 而上一次的提交的comments : “Wrong modifed” 并没有出现。

2. git reset HEAD <file>

git reset HEAD <file> 这个命令可以用来取消暂存。比如我现在又修改a.c,并且已经将其暂存,那么我就可以通过git reset HEAD <file>来取消其暂存状态。

$ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: a.c

我们看大a.c已经处于暂存状态。我们假设我们修改有误,现在我们来取消其暂存状态来继续修正a.c。如下:

$ git reset HEAD a.c Unstaged changes after reset: M a.c

运行上面的命令后a.c就不再是暂存状态了,而是modified 状态了。

好了,我们现在如果想撤销对a.c的内容的修改,怎么办呢?

当然可以怎么改过来的,再怎么改回去。我们也可以用git checkout命令来实现,但是这个命令有风险,因为你对该文件在上次提交之后的本地的任何修改都会被撤销,所以请谨慎使用。如下演示如何使用:

$ git checkout -- a.c

运行上面的命令后之前对a.c的修改就会被撤销。

上面介绍了git在本地创建仓库的一些git操作的基本命令。关于git如何配置远程仓库,以及分支管理的部分,我们放在下一篇文章介绍。

猜您喜欢: