git的基础使用方法有哪些

发布时间:2022-02-18 16:27:22 作者:iii
来源:亿速云 阅读:133

本文小编为大家详细介绍“git的基础使用方法有哪些”,内容详细,步骤清晰,细节处理妥当,希望这篇“git的基础使用方法有哪些”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

git的基础使用方法有哪些

git全局配置

一般在新的系统上,我们都需要先配置下自己的Git工作环境。配置工作只需进行一次,以后升级时还会沿用现在的配置。如果需要,你随时可以用相同的命令修改已有的配置:

git config --global user.name "Breeze Yan"    #配置全局用户名git config --global user.email "yanw02@mysoft.com.cn"  #配置全局用户邮箱git config --unset --global user.name "Breeze Yan" #取消全局用户名配置git config --unset --global user.email "breeze.yan@newbiiz.com"git config --list    #查看git配置git config user.name
git config user.email

创建一个版本库

#创建一个目录:

mkdir git_test

#在项目目录下执行如下操作完成初始化操作:

git init

初始化完成以后,在项目目录下会出现一个.git的目录,所有git需要的数据和资源都存放在这个目录中

git的常用操作

在工作目录下面的所有文件都无外乎这两种状态:已跟踪或未跟踪。已跟踪的文件指的是已经被纳入版本控制管理的文件。已跟踪的文件,它们的状态可能是已修改,已暂存,或者未更新(未修改)。而未跟踪的文件,它们既没有上次更新的快照,也不在当前的暂存区域,通常情况下它们就是那些在工作目录下新创建的文件。

在对某些文件进行编辑之后,git将这些文件标记为已修改。我们会逐步把这些修改过的文件保存到暂存区域,直到最后一次一次性的提交所有这些位于暂存区域的文件,如此重复。所以使用Git时的文件状态变化周期如图所示

版本提交与回退

版本提交 在git_test目录下创建一个文件code.txt,内容如下:

this is the first line

通过如下命令提交一个版本:

yanwei@ubuntu:~/git_test$ git add code.txt
yanwei@ubuntu:~/git_test$ git commit -m 'first commit'[master (根提交) d66bdc0] first commit
1 file changed, 1 insertion(+)
create mode 100644 code.txt

使用如下命令可以查看版本记录:

yanwei@ubuntu:~/git_test$ git logcommit d66bdc0189d3663db2feed6193c00751b277e80d (HEAD -> master)
Author: yanwei
Date:   Sun Jul 15 22:35:33 2018 +0800

版本回退

再次提交一个版本: # 在code.txt中再添加一行之后,内容如下:

yanwei@ubuntu:~/git_test$ cat code.txt
this is the first line
this is the second line

# 再次提交:

yanwei@ubuntu:~/git_test$ git add code.txt
yanwei@ubuntu:~/git_test$ git commit -m 'second commit'[master 227ecaa] second commit
1 file changed, 1 insertion(+)

yanwei@ubuntu:~/git_test$ git logcommit 227ecaa7a5aeca38d392662263f2704c66e1e64a (HEAD -> master)
Author: yanwei
Date:   Sun Jul 15 22:43:49 2018 +0800

second commit

commit d66bdc0189d3663db2feed6193c00751b277e80d
Author: yanwei
Date:   Sun Jul 15 22:35:33 2018 +0800

first commit

现在若想回退到上一个版本,可以使用如下命令:

yanwei@ubuntu:~/git_test$ git reset --hard HEAD^
HEAD 现在位于 d66bdc0 first commit
yanwei@ubuntu:~/git_test$ git logcommit d66bdc0189d3663db2feed6193c00751b277e80d (HEAD -> master)
Author: yanwei
Date:   Sun Jul 15 22:35:33 2018 +0800

first commit
   
yanwei@ubuntu:~/git_test$ cat code.txt
this is the first line

其中HEAD表示当前最新版本,HEAD^表示当前版本的上一个版本,HEAD^^表示当前版本的上上个版本,也可以使用HEAD~1表示当前版本的前一个版本,HEAD~100表示当前版本的前100版本。

假如这个时候,又需要回到second commit版本,可以使用如下命令:

# 通过如下命令找到操作记录:

yanwei@ubuntu:~/git_test$ git reflog
d66bdc0 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
227ecaa HEAD@{1}: commit: second commit
d66bdc0 (HEAD -> master) HEAD@{2}: commit (initial): first commit

# 回退到second commit:

yanwei@ubuntu:~/git_test$ git reset --hard 227ecaa
HEAD 现在位于 227ecaa second commit

yanwei@ubuntu:~/git_test$ git logcommit 227ecaa7a5aeca38d392662263f2704c66e1e64a (HEAD -> master)
Author: yanwei
Date:   Sun Jul 15 22:43:49 2018 +0800

second commit

commit d66bdc0189d3663db2feed6193c00751b277e80d
Author: yanwei
Date:   Sun Jul 15 22:35:33 2018 +0800

first commit

工作区、版本库与暂存区

工作区

我们操作文件的目录,如git_test,就是一个工作区

版本库

工作区有一个隐藏目录.git,这个不是工作区,而是git的版本库。

git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。

因为我们创建git版本库时,git自动为我们创建了唯一一个master分支,所以,现在,git commit就是往master分支上提交更改。

可以简单理解为,需要提交的文件修改全部放到暂存区,然后,一次性提交暂存区的所有修改。

work-stage-repo

前面我们把文件往git版本库里添加的时候,是分两步执行的:

第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;

第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。

下面,我们在git_test目录下再创建一个文件code2.txt,内容如下:

yanwei@ubuntu:~/git_test$ cat code2.txt
the code2 first line

然后再次编辑code.txt,在其中加入一行,编辑后内容如下:

yanwei@ubuntu:~/git_test$ cat code.txt
this is the first line
this is the second line
this is the third line

使用git status查看当前工作树的状态:

yanwei@ubuntu:~/git_test$ git status
位于分支 master
尚未暂存以备提交的变更:
 (使用 "git add ..." 更新要提交的内容)
 (使用 "git checkout -- ..." 丢弃工作区的改动)

修改:     code.txt

未跟踪的文件:
 (使用 "git add ..." 以包含要提交的内容)

code2.txt

修改尚未加入提交(使用 “git add” 和/或 “git commit -a”)

上面提示code.txt被修改,而code2.txt没有被跟踪。

我们将code.txt和code2.txt加入到暂存区,然后再次查看工作树状态:

yanwei@ubuntu:~/git_test$ git add code.txt
yanwei@ubuntu:~/git_test$ git add code2.txt
yanwei@ubuntu:~/git_test$ git status
位于分支 master
要提交的变更:
 (使用 "git reset HEAD ..." 以取消暂存)

修改:     code.txt
新文件:   code2.txt

然后,执行git commit就可以一次性把暂存区的所有修改提交到分支创建一个版本:

yanwei@ubuntu:~/git_test$ git commit -m 'third commit'[master e4fb2aa] third commit
2 files changed, 2 insertions(+)
create mode 100644 code2.txt
yanwei@ubuntu:~/git_test$ git logcommit e4fb2aa04ca8aa3b6a32ef46a69fa5f97ae625fa (HEAD -> master)
Author: yanwei
Date:   Sun Jul 15 23:16:56 2018 +0800

   third commit

commit 227ecaa7a5aeca38d392662263f2704c66e1e64a
Author: yanwei
Date:   Sun Jul 15 22:43:49 2018 +0800

   second commit

commit d66bdc0189d3663db2feed6193c00751b277e80d
Author: yanwei
Date:   Sun Jul 15 22:35:33 2018 +0800

   first commit

一旦提交后,在没有再次对工作区作修改之前,那么工作区就是“干净”的:

yanwei@ubuntu:~/git_test$ git status
位于分支 master
无文件要提交,干净的工作区
现在版本库变成了如下模样:

work-stage-repo

管理文件的修改

修改文件 在code.txt中再次添加一行内容,修改后的内容如下:

yanwei@ubuntu:~/git_test$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth line

然后使用git add命令将其添加到暂存区

yanwei@ubuntu:~/git_test$ git add code.txt
yanwei@ubuntu:~/git_test$ git status
位于分支 master
要提交的变更:
 (使用 "git reset HEAD ..." 以取消暂存)

修改:     code.txt

再次修改该文件,添加一行内容,修改后的内容如下:

yanwei@ubuntu:~/git_test$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth line
this is the fifth line

通过git commit提交一个版本,并使用git status查看,发现第二次修改code.txt内容之后,并没有将其添加到暂存区,所以创建新版本的时候,并没有被提交:

yanwei@ubuntu:~/git_test$ git status
位于分支 master
要提交的变更:
 (使用 "git reset HEAD ..." 以取消暂存)

修改:     code.txt

尚未暂存以备提交的变更:
 (使用 "git add ..." 更新要提交的内容)
 (使用 "git checkout -- ..." 丢弃工作区的改动)

修改:     code.txt
yanwei@ubuntu:~/git_test$ git commit -m 'forth commit'[master 0a96a0f] forth commit
1 file changed, 1 insertion(+)
yanwei@ubuntu:~/git_test$ git status
位于分支 master
尚未暂存以备提交的变更:
 (使用 "git add ..." 更新要提交的内容)
 (使用 "git checkout -- ..." 丢弃工作区的改动)

修改:     code.txt

修改尚未加入提交(使用 “git add” 和/或 “git commit -a”)

撤销修改

丢弃工作区的改动 可以使用git checkout — 来丢弃工作区的改动:

# 使用如下指令来撤销我们第二次的修改

yanwei@ubuntu:~/git_test$ git checkout -- code.txt
yanwei@ubuntu:~/git_test$ git status

位于分支 master

无文件要提交,干净的工作

# 再次查看code.txt文件,发现最后一次修改已被丢弃:

yanwei@ubuntu:~/git_test$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth line

撤销暂存区的修改

我们再次编辑code.txt,添加一行内容,并添加到暂存区:

yanwei@ubuntu:~/git_test$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth line
new line
yanwei@ubuntu:~/git_test$ git add code.txt

yanwei@ubuntu:~/git_test$ git status
位于分支 master
要提交的变更:
 (使用 "git reset HEAD ..." 以取消暂存)

修改:     code.txt

通过如下命令,将暂存区的修改撤销,重新放回工作区:

yanwei@ubuntu:~/git_test$ git reset HEAD code.txt
重置后取消暂存的变更:
M code.txt
yanwei@ubuntu:~/git_test$ git status
位于分支 master
尚未暂存以备提交的变更:
 (使用 "git add ..." 更新要提交的内容)
 (使用 "git checkout -- ..." 丢弃工作区的改动)

修改:     code.txt

修改尚未加入提交(使用 “git add” 和/或 “git commit -a”)

最后可以通过上面的丢弃工作区的改动,来彻底放弃对文件的修改。

小结

当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout — file。

当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD file,就回到了上面那种情形,然后按照上面的情形操作已经提交了不合适的修改到版本库时,想要撤销本次提交,可直接回退版本库

文件差异对比

差异对比常用指令

git diff    #查看工作目录中当前文件和暂存区域快照之间的差异git diff --cached    #查看暂存文件与上次提交时的快照之间的差异,与git diff --staged相同git diff HEAD    #查看在最后一次提交后所有变更git diff v1.6 -- filename    #从一个特定点开始文件的修改情况git diff v1.6 v1.7 --stat    #两次提交的差异比对git diff master...branchname    #在合并某分支前查看变更内容git diff --name-only v1.6 HEAD    #列出v1.6版本到当前最新版本的差异文件,只显示文件,不显示差异内容

具体操作

对比当前工作区与HEAD版本的修改差异

# 修改当前工作区的code.txt,内容如下:

yanwei@ubuntu:~/git_test$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth line
new line

# 差异对比

yanwei@ubuntu:~/git_test$ git diff HEAD -- code.txt
diff --git a/code.txt b/code.txt
index 66f9219..9bc8cf3 100644
--- a/code.txt  # ---代表HEAD+++ b/code.txt  # +++代表当前工作区的文件@@ -2,3 +2,4 @@ this is the first line
this is the second line
this is the third line
this is the forth line
+new line       # 代表当前工作区比HEAD多一行

对比两个版本之间的差异

# 先查看历史提交

yanwei@ubuntu:~/git_test$ git reflog
0a96a0f (HEAD -> master) HEAD@{0}: commit: forth commit
e4fb2aa HEAD@{1}: commit: third commit
227ecaa HEAD@{2}: reset: moving to 227ecaa
d66bdc0 HEAD@{3}: reset: moving to HEAD^
227ecaa HEAD@{4}: commit: second commit
d66bdc0 HEAD@{5}: commit (initial): first commit

# 对比second commit与third commit之间的差异

yanwei@ubuntu:~/git_test$ git diff 227ecaa e4fb2aa -- code.txt
diff --git a/code.txt b/code.txt
index 9899a76..01e1274 100644
--- a/code.txt
+++ b/code.txt
@@ -1,2 +1,3 @@
this is the first line
this is the second line
+this is the third line         # third commit比second commit多增加了一行内容

删除文件

简单说明

要从Git中移除某个文件,就必须要从已跟踪文件清单中移除(确切地说,是从暂存区域移除),然后提交。可以用 git rm 命令完成此项工作,并连带从工作目录中删除指定的文件。

如果只是简单地从工作目录中手工删除文件,运行git status时就会在”Changed but not updated”部分。

如果删除之前修改过并且已经放到暂存区域的话,则必须要用强制删除选项 -f(译注:即 force 的首字母),以防误删除文件后丢失修改的内容。

如果只想删除暂存区中的文件,而不删除工作目录中的文件,可以使用git rm –cached

具体操作示例

yanwei@ubuntu:~/git_test$ rm code2.txt

yanwei@ubuntu:~/git_test$ git status
位于分支 master
尚未暂存以备提交的变更:
 (使用 "git add/rm ..." 更新要提交的内容)
 (使用 "git checkout -- ..." 丢弃工作区的改动)

删除:     code2.txt

修改尚未加入提交(使用 “git add” 和/或 “git commit -a”)

yanwei@ubuntu:~/git_test$ git rm code2.txt
rm 'code2.txt'yanwei@ubuntu:~/git_test$ git status
位于分支 master
要提交的变更:
 (使用 "git reset HEAD ..." 以取消暂存)

删除:     code2.txt

移动文件

移动一个文件的命令为:git mv

想对git中的文件改名,可以执行

git mv file1 file2

其实运行git mv相当于运行了下面三条命令:

mv readme.txt readme
git rm readme.txt
git add readme

查看提交历史

git log     #按提交时间列出所有的更新,最近的更新排在最上面   -p -2   #-p展开显示每次提交的内容差异,-2则仅显示最近的两次更新   --stat    #仅显示简要的增改行数统计   --online    # 一行显示一个版本   --pretty='format'    #定制要显示的记录格式   format的常用选项如下:
       %H 提交对象(commit)的完整哈希字串
       %h 提交对象的简短哈希字串
       %T 树对象(tree)的完整哈希字串  
       %t 树对象的简短哈希字串
       %P 父对象(parent)的完整哈希字串
       %p 父对象的简短哈希字串
       %an 作者(author)的名字  
       %ae 作者的电子邮件地址
       %ad 作者修订日期(可以用 -date= 选项定制格式)
       %ar 作者修订日期,按多久以前的方式显示
       %cn 提交者(committer)的名字  
       %ce 提交者的电子邮件地址
       %cd 提交日期
       %cr 提交日期,按多久以前的方式显示
       %s 提交说明

需要说明的是,作者指的是实际作出修改的人,提交者为最后一次将些文件提交到的人。

git log --pretty=format:"%H - %an, %ar : %s"git log --oneline

--shortstat 只显示 --stat 中最后的行数修改添加移除统计。
--name-only 仅在提交信息后显示已修改的文件清单。
--name-status 显示新增、修改、删除的文件清单。
--before="2 weeks ago" --after="2012-10-29" --pretty=oneline    #日期区间

读到这里,这篇“git的基础使用方法有哪些”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Git基础入门(八)Git分支的基本概念
  2. Git基础入门(三)Git基本操作

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

git

上一篇:Scala提取器使用实例分析

下一篇:Web安全学习知识点有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》