Git的安装和详细使用示例

git的安装和使用示例

git简单使用

参考教程:https://www.yiibai.com/git/git_life_cycle.html

常用的命令

yitiandeMacBook-Pro:~ yitian$ git
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone      Clone a repository into a new directory
   init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add        Add file contents to the index
   mv         Move or rename a file, a directory, or a symlink
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect     Use binary search to find the commit that introduced a bug
   grep       Print lines matching a pattern
   log        Show commit logs
   show       Show various types of objects
   status     Show the working tree status

grow, mark and tweak your common history
   branch     List, create, or delete branches
   checkout   Switch branches or restore working tree files
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   merge      Join two or more development histories together
   rebase     Reapply commits on top of another base tip
   tag        Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch      Download objects and refs from another repository
   pull       Fetch from and integrate with another repository or a local branch
   push       Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.

git工作流程

【参考】:https://www.yiibai.com/git/git_life_cycle.html
image.png

git基础和原理

【参考】:https://www.yiibai.com/git/getting-started-git-basics.html
远程操作和本地操作图示:
image.png

使用示例

gitlab中创建项目

在gitlab上创建一个项目:https://gitlab.com/Yitian.me/project-demo

本地安装git

安装git:https://git-scm.com/downloads

配置gitlab ssh公钥

查看本机是否有ssh key:

$ cd ~/.ssh
$ ls
id_rsa  id_rsa.pub

如果电脑中没有~/.ssh目录文件,则说明该机器中还未配置ssh,运行下面的命令初始化配置ssh:

yitiandeMacBook-Pro:~ yitian$ ssh-keygen -t rsa -C yitian.z@foxmail.com
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/yitian/.ssh/id_rsa): 
Created directory '/Users/yitian/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/yitian/.ssh/id_rsa.
Your public key has been saved in /Users/yitian/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:itWUkhOo+GOL9fDo/xvcfa4Uq8SCKOYa3rDyfC40Cuc yitian.z@foxmail.com
The key's randomart image is:
+---[RSA 2048]----+
|     ..          |
|    .  o .       |
| . .  + o        |
|. .    =         |
| .    . S .      |
|. X. = + . o     |
|+@.Oo = + + .    |
|B+E +  + o o     |
|+*+*o.o.. ...    |
+----[SHA256]-----+

运行完成后,将~/.ssh目录中的id_ras.pub文件中的key,复制到gitlab中的keys中:
20190606110114.jpg    
添加完毕后,可以使用如下命令进行测试:

yitiandeMacBook-Pro:.ssh yitian$ ssh -T git@gitlab.com
The authenticity of host 'gitlab.com (35.231.145.151)' can't be established.
ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitlab.com,35.231.145.151' (ECDSA) to the list of known hosts.
Welcome to GitLab, @Yitian.me!

如上则说明,key设置成功。

项目配置

  1. 在本地创建一个目录并初始化,作为git的本地仓库
yitiandeMacBook-Pro:Documents yitian$ mkdir git-project
yitiandeMacBook-Pro:Documents yitian$ cd git-project/
yitiandeMacBook-Pro:git-project yitian$ ls
yitiandeMacBook-Pro:git-project yitian$ git init
Initialized empty Git repository in /Users/yitian/Documents/git-project/.git/
  1. 克隆上述在gitlab中创建的项目到本地:
yitiandeMacBook-Pro:git-project yitian$ git clone git@gitlab.com:Yitian.me/project-demo.git
Cloning into 'project-demo'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
yitiandeMacBook-Pro:git-project yitian$ ls
project-demo
  1. 设置项目中使用的user和email:
yitiandeMacBook-Pro:git-project yitian$ git config user.name "yitian"
yitiandeMacBook-Pro:git-project yitian$ git config user.email "yitian.z@foxmail.com"
  1. 重命名远程项目库
yitiandeMacBook-Pro:git-project yitian$ git remote add origin git@gitlab.com:Yitian.me/project-demo.git
  1. 从远程库中pull项目中是否有新增的文件:
yitiandeMacBook-Pro:git-project yitian$ git pull origin master
warning: no common commits
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From gitlab.com:Yitian.me/project-demo
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
fatal: refusing to merge unrelated histories
  1. 在本地项目中新建一个文件,然后使用add命令加入缓冲区,并提交:
yitiandeMacBook-Pro:git-project yitian$ cd project-demo/
yitiandeMacBook-Pro:project-demo yitian$ ls
README.md
yitiandeMacBook-Pro:project-demo yitian$ touch text.txt
yitiandeMacBook-Pro:project-demo yitian$ git add text.txt
yitiandeMacBook-Pro:project-demo yitian$ git commit -m "this is a commit message"
[master 6a4c0fa] this is a commit message
 Committer: 一天 <yitian@yitiandeMacBook-Pro.local>
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, 0 insertions(+), 0 deletions(-)
 create mode 100644 text.txt
  1. 查看git的状态
git status
  1. 执行push命令,将新增的文件添加到远程的库中:
yitiandeMacBook-Pro:project-demo yitian$ git push origin master
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 300 bytes | 300.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To gitlab.com:Yitian.me/project-demo.git
   f0b6a9a..6a4c0fa  master -> master
yitiandeMacBook-Pro:project-demo yitian$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
  1. 在gitlab中查看刚刚提交的文件:

20190606111916.jpg

新增使用示例

查看当前的用户名

上述命令在使用时可以看到使用的是该项目的创建账户:
user.name: yitian
user.email: yitian.z@foxmail.com
也可以通过如下命令进行查看当前的用户名:

yitiandeMacBook-Pro:project-demo yitian$ git config user.name
yitian
yitiandeMacBook-Pro:project-demo yitian$ git config user.email
yitian.z@foxmail.com

修改当前用户名(全局修改)

由于该项目为自己创建的示例项目,但在公司中使用的是公司分配的用户名和邮箱,如果还使用自己的用户名和邮箱,在提交代码时会出现一些问题。例如这里在公司项目中使用的git用户名和邮箱就是公司的:

yitiandeMacBook-Pro:virginrocks yitian$ git config user.name
yitianzhang.zyt
yitiandeMacBook-Pro:virginrocks yitian$ git config user.email
yitianzhang.zyt@alibaba-inc.com

为了防止以后出现问题,这里将全局的用户名和邮箱都进行设置:

yitiandeMacBook-Pro:xinxuan-assets yitian$ git config --global user.name yitianzhang.zyt
yitiandeMacBook-Pro:xinxuan-assets yitian$ git config --global user.email yitianzhang.zyt@alibaba-inc.com

这样在上述自己的示例项目中,可以查看用户名和邮箱已经改到当前设置的公司账号:

yitiandeMacBook-Pro:project-demo yitian$ git config user.name
yitianzhang.zyt
yitiandeMacBook-Pro:project-demo yitian$ git config user.email
yitianzhang.zyt@alibaba-inc.com

修改文件内容并提交

使用新账号的代码提交与上面的过程一样:
在项目中的text.txt文件新增内容:

this is the first comment!!!

使用git add命令将加入暂存区:

yitiandeMacBook-Pro:project-demo yitian$ git add .
yitiandeMacBook-Pro:project-demo yitian$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   text.txt

使用git commit命令提交变更的内容:

yitiandeMacBook-Pro:project-demo yitian$ git commit -m "this is the first comment in master."
[master a54bdb8] this is the first comment in master.
 1 file changed, 1 insertion(+)
yitiandeMacBook-Pro:project-demo yitian$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

使用push命令进行推送到远程仓库:

nothing to commit, working tree clean
yitiandeMacBook-Pro:project-demo yitian$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 325 bytes | 325.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To gitlab.com:Yitian.me/project-demo.git
   6a4c0fa..a54bdb8  master -> master

可以在gitlab上查看该账号新增的内容:
20190615104841.jpg

分支的使用

查看分支

上述的操作都是在master主分支中进行的,现在查看一下项目分支的情况;

# 不带参数为查看本地分支
yitiandeMacBook-Pro:project-demo yitian$ git branch
* master
# -a查看所有分支(包括本地和远程)
yitiandeMacBook-Pro:project-demo yitian$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
# -r查看远程分支
yitiandeMacBook-Pro:project-demo yitian$ git branch -r
  origin/HEAD -> origin/master
  origin/master

创建本地分支

上面可以看到当前项目中只有一个master分支,下面创建一个分支:

yitiandeMacBook-Pro:project-demo yitian$ git branch 20190615-branch
yitiandeMacBook-Pro:project-demo yitian$ git branch -a
  20190615-branch
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

注意,上述创建的新分支的内容是基于当前分支创建的。

切换分支修改文件然并提交

  1. 切换分支:
yitiandeMacBook-Pro:project-demo yitian$ git checkout 20190615-branch
Switched to branch '20190615-branch'
  1. 查看当前分支:
yitiandeMacBook-Pro:project-demo yitian$ git branch
* 20190615-branch
  master

其中*号标注的就是当前所在分支。

  1. 在新分支中修改并新增文件:
yitiandeMacBook-Pro:project-demo yitian$ vim text.txt
this is the first comment!!!
this is 20190615-branch modified context...
yitiandeMacBook-Pro:project-demo yitian$ touch new-branch.txt
yitiandeMacBook-Pro:project-demo yitian$ vim new-branch.txt 
this file is created by 20190615-branch!
  1. 添加并提交修改的内容
yitiandeMacBook-Pro:project-demo yitian$ git add .
yitiandeMacBook-Pro:project-demo yitian$ git status
On branch 20190615-branch
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   new-branch.txt
	modified:   text.txt

yitiandeMacBook-Pro:project-demo yitian$ git commit -m "this commit in 20190615-branch"
[20190615-branch f58b8f8] this commit in 20190615-branch
 2 files changed, 3 insertions(+)
 create mode 100644 new-branch.txt
yitiandeMacBook-Pro:project-demo yitian$ git push
fatal: The current branch 20190615-branch has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin 20190615-branch

通过上面的命令可以看到git push命令出现了问题,使用提示的命令重新push:

yitiandeMacBook-Pro:project-demo yitian$ git push --set-upstream origin 20190615-branch
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 442 bytes | 442.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0)
remote: 
remote: To create a merge request for 20190615-branch, visit:
remote:   https://gitlab.com/Yitian.me/project-demo/merge_requests/new?merge_request%5Bsource_branch%5D=20190615-branch
remote: 
To gitlab.com:Yitian.me/project-demo.git
 * [new branch]      20190615-branch -> 20190615-branch
Branch '20190615-branch' set up to track remote branch '20190615-branch' from 'origin'.
yitiandeMacBook-Pro:project-demo yitian$ git status
On branch 20190615-branch
Your branch is up to date with 'origin/20190615-branch'.

nothing to commit, working tree clean

push成功。

  1. 查看gitlab:
    20190615111910.jpg

可以看到,之前在本地创建的分支可以自动的push到远程中。现在该项目有了两个分支,在新分支中修改和添加的内容不会影响master分支。
也可以通过git branch命令进行查看:

yitiandeMacBook-Pro:project-demo yitian$ git branch -a
* 20190615-branch
  master
  remotes/origin/20190615-branch
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

可以看到当前所在的分支为本地创建的20190615-branch分支,由于刚才把当前分支的代码提交到了远程库中,在远程也存在了一个对应的remotes/origin/20190615-branch分支。也就是每个本地分支会对应一个提交到的远程分支,根据目前的情况对应是这样的:

20190615-branch -> remotes/origin/20190615-branch
master -> remotes/origin/master

远程创建分支

上述使用命令行创建分支是创建的本地分支,然后通过push命令将本地分支提交到了git中,git会根据提交的分支自动创建一个对应名字的远程分支。
但在项目使用过程中,往往是先在远程创建一个分支,然后在从本地切换到这个分支中。需要进行如下操作:

  1. 远程创建分支:

20190615112745.jpg
20190615112841.jpg
由于这里依然是从master中创建分支,因此这个分支中的内容和master中的一样。

  1. 此时在gitlab中可以看到,远程已经存在三个分支:

20190615113016.jpg

切换到远程分支进行操作

由于新到分支是从远程创建的,因此如果直接在本地进行查看是查看不到远程创建的分支的。因此需要先使用pull命令,然后在进行查看和切换分支:

yitiandeMacBook-Pro:project-demo yitian$ git pull
From gitlab.com:Yitian.me/project-demo
 * [new branch]      new_branch_from_remote -> origin/new_branch_from_remote
Already up to date.
yitiandeMacBook-Pro:project-demo yitian$ git branch -a
* 20190615-branch
  master
  remotes/origin/20190615-branch
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/new_branch_from_remote
yitiandeMacBook-Pro:project-demo yitian$ 

切换到远程分支:

yitiandeMacBook-Pro:project-demo yitian$ git checkout new_branch_from_remote
Branch 'new_branch_from_remote' set up to track remote branch 'new_branch_from_remote' from 'origin'.
Switched to a new branch 'new_branch_from_remote'
yitiandeMacBook-Pro:project-demo yitian$ git branch -a
  20190615-branch
  master
* new_branch_from_remote
  remotes/origin/20190615-branch
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/new_branch_from_remote

此时就可以看到当前已经切换到本地名为new_branch_from_remote的分支,该分支对应的远程分支为:remotes/origin/new_branch_from_remote,在此本地分支中修改提交的内容,都会被push到对应名称的远程分支中。
修改和添加内容:

yitiandeMacBook-Pro:project-demo yitian$ vim text.txt
this is the first comment!!!
here is the context modified by new_branch_from_remote!
yitiandeMacBook-Pro:project-demo yitian$ touch new_branch_file_from_remote.txt
yitiandeMacBook-Pro:project-demo yitian$ vim new_branch_file_from_remote.txt 
this file is created by new_branch_from_remote!

提交修改内容:

yitiandeMacBook-Pro:project-demo yitian$ git add .
yitiandeMacBook-Pro:project-demo yitian$ git commit -m "this commit from new_bran_from_remote"
[new_branch_from_remote 6075643] this commit from new_bran_from_remote
 2 files changed, 2 insertions(+)
 create mode 100644 new_branch_file_from_remote.txt
yitiandeMacBook-Pro:project-demo yitian$ git push
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 476 bytes | 476.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0)
remote: 
remote: To create a merge request for new_branch_from_remote, visit:
remote:   https://gitlab.com/Yitian.me/project-demo/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch_from_remote
remote: 
To gitlab.com:Yitian.me/project-demo.git
   a54bdb8..6075643  new_branch_from_remote -> new_branch_from_remote

在gitlab中可以查看当前分支对应的远程分支中提交的内容:
20190615114046.jpg

合并分支

根据如上的内容,我们在master分支的基础上创建了两个分支:20190615-branch和new_branch_from_remote,并分别提交了修改内容。

合并分支到master

现在首先将20190615-branch分支的内容合并到master分支中:

# 首先需要切换到master分支中
yitiandeMacBook-Pro:project-demo yitian$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
# 查看切换后到分支情况
yitiandeMacBook-Pro:project-demo yitian$ git branch -a
  20190615-branch
* master
  new_branch_from_remote
  remotes/origin/20190615-branch
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/new_branch_from_remote
# 将20190615-branch分支合并到当前分支中(master)
yitiandeMacBook-Pro:project-demo yitian$ git merge 20190615-branch
Updating a54bdb8..f58b8f8
Fast-forward
 new-branch.txt | 1 +
 text.txt       | 2 ++
 2 files changed, 3 insertions(+)
 create mode 100644 new-branch.txt
# 查看合并后的内容
yitiandeMacBook-Pro:project-demo yitian$ ls
README.md	new-branch.txt	text.txt
yitiandeMacBook-Pro:project-demo yitian$ cat text.txt 
this is the first comment!!!

this is 20190615-branch modified context...

使用git status命令可以看到,上述merge命令会自动将合并后的内容进行提交,但没有push:

yitiandeMacBook-Pro:project-demo yitian$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

因此这里还需要执行push操作,合并后但内容在能更新到远程库中:

yitiandeMacBook-Pro:project-demo yitian$ git push
Total 0 (delta 0), reused 0 (delta 0)
To gitlab.com:Yitian.me/project-demo.git
   a54bdb8..f58b8f8  master -> master

在gitlab页面中查看合并后的内容:
20190615115354.jpg

合并master分支到本地

上面展示了将新分支20190615-branch合并到master分支中,没有出现冲突到问题。下面尝试将master分支到内容合并到new_branch_from_remote分支中。

yitiandeMacBook-Pro:project-demo yitian$ git checkout new_branch_from_remote
Switched to branch 'new_branch_from_remote'
Your branch is up to date with 'origin/new_branch_from_remote'.
yitiandeMacBook-Pro:project-demo yitian$ git merge master
Auto-merging text.txt
CONFLICT (content): Merge conflict in text.txt
Automatic merge failed; fix conflicts and then commit the result.
yitiandeMacBook-Pro:project-demo yitian$ git status
On branch new_branch_from_remote
Your branch is up to date with 'origin/new_branch_from_remote'.

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Changes to be committed:

	new file:   new-branch.txt

Unmerged paths:
  (use "git add <file>..." to mark resolution)

	both modified:   text.txt

这里因为master中合并了20190615-branch分支中的内容,其中对text.txt文件进行了修改,而new_branch_from_remote分支中也对text.txt文件进行了修改,因此对text.txt文件在合并中就会出现冲突对问题。需要先手动解决冲突,在进行操作。
打开提示冲突的文件text.txt,内容如下:

this is the first comment!!!
<<<<<<< HEAD
here is the context modified by new_branch_from_remote!
=======

this is 20190615-branch modified context...
>>>>>>> master

Git用<<<<<<<=======>>>>>>>标记出不同分支的内容,我们修改如下后保存:

this is the first comment!!!
here is the context modified by new_branch_from_remote!
this is 20190615-branch modified context...

解决冲突以后,将存在冲突而为add到缓冲区中到文件加入到缓冲区中:

yitiandeMacBook-Pro:project-demo yitian$ git add .
yitiandeMacBook-Pro:project-demo yitian$ git status
On branch new_branch_from_remote
Your branch is up to date with 'origin/new_branch_from_remote'.

All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

	new file:   new-branch.txt
	modified:   text.txt

然后提交合并的内容:

yitiandeMacBook-Pro:project-demo yitian$ git commit -m "merge master to branch_remote"
[new_branch_from_remote 271a46c] merge master to branch_remote
yitiandeMacBook-Pro:project-demo yitian$ git push
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 430 bytes | 430.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: 
remote: To create a merge request for new_branch_from_remote, visit:
remote:   https://gitlab.com/Yitian.me/project-demo/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch_from_remote
remote: 
To gitlab.com:Yitian.me/project-demo.git
   6075643..271a46c  new_branch_from_remote -> new_branch_from_remote

此时,在gitlab中的new_branch_from_remote分支中查看之前的冲突文件内容如下:
20190615141357.jpg

在次合并到master分支

这里仍然使用目前所在的new_branch_from_remote分支,假设该分支合并了master分支之后,有自己添加了一些新的内容:

yitiandeMacBook-Pro:project-demo yitian$ touch new-file.txt
yitiandeMacBook-Pro:project-demo yitian$ vim new-file.txt 
this file is created by yitian in new_branch_from_remote branch.
yitiandeMacBook-Pro:project-demo yitian$ vim text.txt
this is the first comment!!!
here is the context modified by new_branch_from_remote!
this is 20190615-branch modified context...
we added some new features in the new_branch_from_remote branch.
let us do it!

然后提交该分支中的变更:

yitiandeMacBook-Pro:project-demo yitian$ git add .
yitiandeMacBook-Pro:project-demo yitian$ git commit -m "add some new features"
[new_branch_from_remote aad7adf] add some new features
 2 files changed, 3 insertions(+)
 create mode 100644 new-file.txt
yitiandeMacBook-Pro:project-demo yitian$ git push
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 497 bytes | 497.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: 
remote: To create a merge request for new_branch_from_remote, visit:
remote:   https://gitlab.com/Yitian.me/project-demo/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch_from_remote
remote: 
To gitlab.com:Yitian.me/project-demo.git
   271a46c..aad7adf  new_branch_from_remote -> new_branch_from_remote

提交完成后,我们在切换到master分支中,将该分支的内容合并到master分支中:

yitiandeMacBook-Pro:project-demo yitian$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
yitiandeMacBook-Pro:project-demo yitian$ git pull
Already up to date.
yitiandeMacBook-Pro:project-demo yitian$ git merge new_branch_from_remote
Updating f58b8f8..aad7adf
Fast-forward
 new-file.txt                    | 1 +
 new_branch_file_from_remote.txt | 1 +
 text.txt                        | 4 +++-
 3 files changed, 5 insertions(+), 1 deletion(-)
 create mode 100644 new-file.txt
 create mode 100644 new_branch_file_from_remote.txt

此时没有冲突的发生,查看之前冲突过的text.txt文件,内容如下:

yitiandeMacBook-Pro:project-demo yitian$ cat text.txt 
this is the first comment!!!
here is the context modified by new_branch_from_remote!
this is 20190615-branch modified context...
we added some new features in the new_branch_from_remote branch.
let us do it!

接着把合并后的内容push到远程库中:

yitiandeMacBook-Pro:project-demo yitian$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
yitiandeMacBook-Pro:project-demo yitian$ git push
Total 0 (delta 0), reused 0 (delta 0)
To gitlab.com:Yitian.me/project-demo.git
   f58b8f8..aad7adf  master -> master

此次合并分支也结束了,在gitlab中查看主分支的内容:
20190615142820.jpg
为其他两个分支中添加的内容就都合并到了master分支中了。

删除分支

在上面将20190615-branch分支合并到主分支之后,这个分支就不再使用了,这时我们可以将该分支删除:

yitiandeMacBook-Pro:project-demo yitian$ git branch -d 20190615-branch
Deleted branch 20190615-branch (was f58b8f8).
yitiandeMacBook-Pro:project-demo yitian$ git branch -a
* master
  new_branch_from_remote
  remotes/origin/20190615-branch
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/new_branch_from_remote

删除分支之后,可以看到删除的分支为本地的分支,远程对应的分支还在。
如果这时在切换到20190615-branch分支中,git会自动识别到远程分支的存在,在本地重新创建一个和远程分支中一样的分支20190615-branch:

yitiandeMacBook-Pro:project-demo yitian$ git checkout 20190615-branch
Branch '20190615-branch' set up to track remote branch '20190615-branch' from 'origin'.
Switched to a new branch '20190615-branch'
yitiandeMacBook-Pro:project-demo yitian$ ls
README.md	new-branch.txt	text.txt
yitiandeMacBook-Pro:project-demo yitian$ git branch -a
* 20190615-branch
  master
  new_branch_from_remote
  remotes/origin/20190615-branch
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/new_branch_from_remote

分支管理流程图

未命名文件.png

其他常用命令

git log命令

直接使用git log,显示所有历史信息:

yitiandeMacBook-Pro:project-demo yitian$ git log
commit aad7adfc82185b91083d17407291b1f5e6508585 (HEAD -> master, origin/new_branch_from_remote, origin/master, origin/HEAD, new_branch_from_remote)
Author: yitianzhang.zyt <yitianzhang.zyt@alibaba-inc.com>
Date:   Sat Jun 15 14:21:48 2019 +0800

    add some new features

commit 271a46ccfc6dac9c6e33d43875b1366c1b28487b
Merge: 6075643 f58b8f8
Author: yitianzhang.zyt <yitianzhang.zyt@alibaba-inc.com>
Date:   Sat Jun 15 14:12:32 2019 +0800

    merge master to branch_remote

commit 60756430dc316e6414e91ab60cf4250c238967b2
Author: yitianzhang.zyt <yitianzhang.zyt@alibaba-inc.com>
Date:   Sat Jun 15 11:39:16 2019 +0800

    this commit from new_bran_from_remote

commit f58b8f8b14c8a62f9ae076608575372a9d45983a (origin/20190615-branch)
Author: yitianzhang.zyt <yitianzhang.zyt@alibaba-inc.com>
Date:   Sat Jun 15 11:07:43 2019 +0800

    this commit in 20190615-branch

commit a54bdb8afd921718a3382372b6c055a518268c26
Author: yitianzhang.zyt <yitianzhang.zyt@alibaba-inc.com>
Date:   Sat Jun 15 10:16:24 2019 +0800

    this is the first comment in master.

commit 6a4c0fa1a8efe399db6cd18b9a6ba996de4c2107
Author: 一天 <yitian@yitiandeMacBook-Pro.local>
Date:   Thu Jun 6 10:52:20 2019 +0800

    this is a commit message

commit f0b6a9ad0fd36474c215f095cea5f87d06a08918
Author: Yitian Zhang <yitian.z@foxmail.com>
Date:   Thu Jun 6 01:38:49 2019 +0000

    Initial commit

以图形形式显示分支合并历史(master分支log):

yitiandeMacBook-Pro:project-demo yitian$ git log --oneline --graph
* aad7adf (HEAD -> master, origin/new_branch_from_remote, origin/master, origin/HEAD, new_branch_from_remote) add some new features
*   271a46c merge master to branch_remote
|\  
| * f58b8f8 (origin/20190615-branch) this commit in 20190615-branch
* | 6075643 this commit from new_bran_from_remote
|/  
* a54bdb8 this is the first comment in master.
* 6a4c0fa this is a commit message
* f0b6a9a Initial commit

查看历史记录的简洁信息:

yitiandeMacBook-Pro:project-demo yitian$ git log --oneline
aad7adf (HEAD -> master, origin/new_branch_from_remote, origin/master, origin/HEAD, new_branch_from_remote) add some new features
271a46c merge master to branch_remote
6075643 this commit from new_bran_from_remote
f58b8f8 (origin/20190615-branch) this commit in 20190615-branch
a54bdb8 this is the first comment in master.
6a4c0fa this is a commit message
f0b6a9a Initial commit

git diff命令

不加参数的git diff用于show diff of unstaged changes. 此命令比较的是工作目录中当前文件和暂存区域快照之间的差异,也就是修改之后还没有暂存起来的变化内容.
这里我们checkout到之前的new_branch_from_remote分支中,修改一下text.txt文件:

yitiandeMacBook-Pro:project-demo yitian$ cat text.txt 
this is the first comment!!!
here is the context modified by new_branch_from_remote!
this is 20190615-branch modified context...
we added some new features in the new_branch_from_remote branch.
let us do it!
# 如下为新增的内容

show some diff context for me!

运行git diff命令:

yitiandeMacBook-Pro:project-demo yitian$ git diff
diff --git a/text.txt b/text.txt
index cfb86d2..59f54e0 100644
--- a/text.txt
+++ b/text.txt
@@ -3,3 +3,5 @@ here is the context modified by new_branch_from_remote!
 this is 20190615-branch modified context...
 we added some new features in the new_branch_from_remote branch.
 let us do it!
+
+show some diff context for me!

可以看到,这里+表示的为当前工作目录中和暂存区域的差异(因为没有运行add命令)。
然后我们现将新的变更add到暂存区中,在运行diff命令查看:

yitiandeMacBook-Pro:project-demo yitian$ git add .
# add命令执行后,当前工作区和暂存区之间就没有了差异
yitiandeMacBook-Pro:project-demo yitian$ git diff

# --cached参数显示了暂存区和上次提交时快照之间到差异,可以看到为新add到文件内容
yitiandeMacBook-Pro:project-demo yitian$ git diff --cached
diff --git a/text.txt b/text.txt
index cfb86d2..59f54e0 100644
--- a/text.txt
+++ b/text.txt
@@ -3,3 +3,5 @@ here is the context modified by new_branch_from_remote!
 this is 20190615-branch modified context...
 we added some new features in the new_branch_from_remote branch.
 let us do it!
+
+show some diff context for me!

# --staged参数和--cached效果一样
yitiandeMacBook-Pro:project-demo yitian$ git diff --staged
diff --git a/text.txt b/text.txt
index cfb86d2..59f54e0 100644
--- a/text.txt
+++ b/text.txt
@@ -3,3 +3,5 @@ here is the context modified by new_branch_from_remote!
 this is 20190615-branch modified context...
 we added some new features in the new_branch_from_remote branch.
 let us do it!
+
+show some diff context for me!

# HEAD参数比较到是工作区间和上次提交时之间的差异
yitiandeMacBook-Pro:project-demo yitian$ git diff HEAD
diff --git a/text.txt b/text.txt
index cfb86d2..59f54e0 100644
--- a/text.txt
+++ b/text.txt
@@ -3,3 +3,5 @@ here is the context modified by new_branch_from_remote!
 this is 20190615-branch modified context...
 we added some new features in the new_branch_from_remote branch.
 let us do it!
+
+show some diff context for me!

然后,我们将变更进行提交:

yitiandeMacBook-Pro:project-demo yitian$ git commit -m "add new feature"
[new_branch_from_remote dd6c0dc] add new feature
 1 file changed, 2 insertions(+)
 # 提交变更之后,可以看到使用下面的命令都没有差别
yitiandeMacBook-Pro:project-demo yitian$ git diff --cached
yitiandeMacBook-Pro:project-demo yitian$ git diff --staged
yitiandeMacBook-Pro:project-demo yitian$ git diff HEAD

# 使用如下命令,比较两个分支提交时的差异
yitiandeMacBook-Pro:project-demo yitian$ git diff new_branch_from_remote..master
diff --git a/text.txt b/text.txt
index 59f54e0..cfb86d2 100644
--- a/text.txt
+++ b/text.txt
@@ -3,5 +3,3 @@ here is the context modified by new_branch_from_remote!
 this is 20190615-branch modified context...
 we added some new features in the new_branch_from_remote branch.
 let us do it!
-
-show some diff context for me!
yitiandeMacBook-Pro:project-demo yitian$ git diff new_branch_from_remote master
diff --git a/text.txt b/text.txt
index 59f54e0..cfb86d2 100644
--- a/text.txt
+++ b/text.txt
@@ -3,5 +3,3 @@ here is the context modified by new_branch_from_remote!
 this is 20190615-branch modified context...
 we added some new features in the new_branch_from_remote branch.
 let us do it!
-
-show some diff context for me!

如果比较同一分支中,当前提交和上次提交之间的差异,使用如下命令:

yitiandeMacBook-Pro:project-demo yitian$ git diff HEAD^ HEAD
diff --git a/text.txt b/text.txt
index cfb86d2..59f54e0 100644
--- a/text.txt
+++ b/text.txt
@@ -3,3 +3,5 @@ here is the context modified by new_branch_from_remote!
 this is 20190615-branch modified context...
 we added some new features in the new_branch_from_remote branch.
 let us do it!
+
+show some diff context for me!

注:这里的 HEAD 关键字指的是当前分支最末梢最新的一个提交。也就是版本库中该分支上的最新版本。
为了不影响以后的示例操作,这里将该分支中新增的commit push到远程库中。

yitiandeMacBook-Pro:project-demo yitian$ git push

git reset命令(补充)

为了演示reset和revert命令的作用,我们首先新建一个远程分支,然后pull到本地来:
20190619201210.jpg
然后pull到本地来然后checkout到该分支中:

yitiandeMacBook-Pro:project-demo yitian$ git pull
From gitlab.com:Yitian.me/project-demo
 * [new branch]      new-branch-for-redoing -> origin/new-branch-for-redoing
Already up to date.
yitiandeMacBook-Pro:project-demo yitian$ git branch -a
  master
* new_branch_from_remote
  remotes/origin/20190615-branch
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/new-branch-for-redoing
  remotes/origin/new_branch_from_remote
yitiandeMacBook-Pro:project-demo yitian$ git checkout new-branch-for-redoing
Branch 'new-branch-for-redoing' set up to track remote branch 'new-branch-for-redoing' from 'origin'.
Switched to a new branch 'new-branch-for-redoing'
yitiandeMacBook-Pro:project-demo yitian$ git branch -a
  master
* new-branch-for-redoing
  new_branch_from_remote
  remotes/origin/20190615-branch
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/new-branch-for-redoing
  remotes/origin/new_branch_from_remote

以上为准备工作,下面进行具体的操作。

回滚本地add

我们先在该分支中创建一个新的文件,并查看git status:

yitiandeMacBook-Pro:project-demo yitian$ touch new-file-redoing.txt
yitiandeMacBook-Pro:project-demo yitian$ vim new-file-redoing.txt 
yitiandeMacBook-Pro:project-demo yitian$ git status
On branch new-branch-for-redoing
Your branch is up to date with 'origin/new-branch-for-redoing'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	new-file-redoing.txt

nothing added to commit but untracked files present (use "git add" to track)

如果现在执行reset操作,不会有什么作用:

yitiandeMacBook-Pro:project-demo yitian$ git reset HEAD

因为HEAD标志的是当前分支中的最新commit的状态,现在在没有任何add和commit的操作下,reset不会有效果。
现在将新修改的文件进行add操作:

yitiandeMacBook-Pro:project-demo yitian$ git add .
yitiandeMacBook-Pro:project-demo yitian$ git status
On branch new-branch-for-redoing
Your branch is up to date with 'origin/new-branch-for-redoing'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   new-file-redoing.txt

现在在进行reset HEAD:

yitiandeMacBook-Pro:project-demo yitian$ git reset HEAD
yitiandeMacBook-Pro:project-demo yitian$ git status
On branch new-branch-for-redoing
Your branch is up to date with 'origin/new-branch-for-redoing'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	new-file-redoing.txt

nothing added to commit but untracked files present (use "git add" to track)

可以看到,这里会将之前add到缓存区中的新文件回退到工作区中,缓存区中不再存在。

回退commit操作

现在将上述到修改重新add并commit,然后对该文件进行修改然后再次提交(该分支对第二次提交):

yitiandeMacBook-Pro:project-demo yitian$ git add .
yitiandeMacBook-Pro:project-demo yitian$ git commit -m "add new file for redoing"
[new-branch-for-redoing 75406f8] add new file for redoing
 1 file changed, 1 insertion(+)
 create mode 100644 new-file-redoing.txt
yitiandeMacBook-Pro:project-demo yitian$ vim new-file-redoing.txt 
yitiandeMacBook-Pro:project-demo yitian$ git add .
yitiandeMacBook-Pro:project-demo yitian$ git commit -m "modified and commit 2"
[new-branch-for-redoing e19e9ff] modified and commit 2
 1 file changed, 1 insertion(+)

查看git log:

yitiandeMacBook-Pro:project-demo yitian$ git log
commit e19e9ffcab7d447b1416bf10d5b8e142743c1f19 (HEAD -> new-branch-for-redoing)
Author: yitianzhang.zyt <yitianzhang.zyt@alibaba-inc.com>
Date:   Wed Jun 19 20:41:02 2019 +0800

    modified and commit 2

commit 75406f87f911eb5b4c6cbb88670b8e5c32ab8a29
Author: yitianzhang.zyt <yitianzhang.zyt@alibaba-inc.com>
Date:   Wed Jun 19 20:39:59 2019 +0800

    add new file for redoing

现在再次执行git reset HEAD依然不会起作用。使用git reset HEAD才会起作用,因为HEAD为当前分支最新状态,而HEAD为上次提交对状态,上上一个版本就是HEAD^,当然往上100个版本写100个比较容易数不过来,可以写成HEAD~100。

yitiandeMacBook-Pro:project-demo yitian$ git reset HEAD^
Unstaged changes after reset:
M	new-file-redoing.txt
yitiandeMacBook-Pro:project-demo yitian$ git status
On branch new-branch-for-redoing
Your branch is ahead of 'origin/new-branch-for-redoing' by 1 commit.
  (use "git push" to publish your local commits)

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:   new-file-redoing.txt

no changes added to commit (use "git add" and/or "git commit -a")

可以看到,执行完命令之后,这次commit的变更被撤销了,提交的变更回到了本地的工作区中。查看git log,也可以看到第二次的commit已经被撤销了,HEAD指向了第一次的commit:
20190619205220.jpg
注意:这里我们使用的是 git reset HEAD^ ,也就是默认的git reset --mixed HEAD^,此时我们执行git status看到:我们的修改又回到了工作区。

git reset --soft

重新add + commit这次变更,执行git reset --soft HEAD^:

yitiandeMacBook-Pro:project-demo yitian$ git add .
yitiandeMacBook-Pro:project-demo yitian$ git commit -m "commit modified 2"

此时的git log:
20190619205751.jpg
执行reset命令:

yitiandeMacBook-Pro:project-demo yitian$ git reset --soft HEAD^

20190619205908.jpg
可以看到,重新commit之后,执行–soft的话,那么修改就会放回暂存区。执行git log可以看到最新的一次commit同样被撤销了:
20190619210050.jpg

git reset --hard

重新从缓存区commit之后,log如下:
20190619210320.jpg
然后执行如下命令,看一下–hard作用:
20190619210351.jpg
可以看到上次的commit被撤销,同时上次的修改被撤销,工作区间完全回到上一次commit之后的状态。
总结:从上面的操作中可以看到,reset命令可以对add和commit命令进行撤销操作,reset --soft  head^,会撤销上次的commit并将add的变更留在缓存区。reset --mixed head^,会撤销上次的commit,add的变更撤销缓存区只保留在工作区中。reset --hard head^则会直接将上次的commit中的变更在工作区中撤销,也就是本地工作区的修改都被撤销,工作区直接回滚到上次的commit后的状态。

回滚提交版本

由上面的总结可以看出,使用reset --hard commit_id可以将当前的工作区回滚到之前提交的指定commit中。
下面在进行几次修改和提交过程,都是修改上面创建的新增文件,然后在该文件中加入如下内容,分别代表3次提交操作:

yitiandeMacBook-Pro:project-demo yitian$ vim new-file-redoing.txt 
yitiandeMacBook-Pro:project-demo yitian$ git add .
yitiandeMacBook-Pro:project-demo yitian$ git commit -m "modified 1"
[new-branch-for-redoing 8a86fc4] modified 1
 1 file changed, 1 insertion(+)
yitiandeMacBook-Pro:project-demo yitian$ vim new-file-redoing.txt 
yitiandeMacBook-Pro:project-demo yitian$ git add .
yitiandeMacBook-Pro:project-demo yitian$ git commit -m "modified 2"
[new-branch-for-redoing da654a6] modified 2
 1 file changed, 1 insertion(+)
yitiandeMacBook-Pro:project-demo yitian$ vim new-file-redoing.txt 
yitiandeMacBook-Pro:project-demo yitian$ git add .
yitiandeMacBook-Pro:project-demo yitian$ git commit -m "modified 3"
[new-branch-for-redoing ea3aeaa] modified 3
 1 file changed, 1 insertion(+)

20190619213412.jpg
然后进行回滚commit的操作。上图中,可以看到每次提交都对应了一个commit id,根据该id,可以将head回退到指定的commit状态。这里将head回退到第二次提交的状态中,也就是在new-file-redoing.txt只有modified 2:

yitiandeMacBook-Pro:project-demo yitian$ git reset --hard da654a670f9ad1b42359d09da418800a97acb8fe
HEAD is now at da654a6 modified 2

查看git log如下:
20190620093713.jpg
可以看到HEAD已经指向第二次commit的状态,查看new-file-redoding.txt文件的内容:

yitiandeMacBook-Pro:project-demo yitian$ cat new-file-redoing.txt 
this is the file for redoing example!
modified 1
modified 2

工作区间文件中的内容也回退到了第二次commit的状态。
注意:上面所有的操作都没有进行push命令,也就是上述的操作影响的都只是本地的代码库版本内容,远程gitlab中该分支的内容始终没有发生变化。下面进行一次push操作:

yitiandeMacBook-Pro:project-demo yitian$ git push
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 8 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (9/9), 893 bytes | 893.00 KiB/s, done.
Total 9 (delta 3), reused 0 (delta 0)
remote: 
remote: To create a merge request for new-branch-for-redoing, visit:
remote:   https://gitlab.com/Yitian.me/project-demo/merge_requests/new?merge_request%5Bsource_branch%5D=new-branch-for-redoing
remote: 
To gitlab.com:Yitian.me/project-demo.git
   aad7adf..da654a6  new-branch-for-redoing -> new-branch-for-redoing

执行成功,如果push命令不成功,可以使用如下命令强制push:

git push -f

push完成后,在gitlab中可以看到之前的提交都已经push到远程仓库中:
20190620094800.jpg
综上,git reset命令的主要作用:

  • 可以将当前add到缓存区中的更新回滚(清空缓存区)
  • 可以将当前提交的内容回滚到缓冲区,或者直接丢弃
  • 可以将HEAD回滚到之前提交的一次commit上,即将当前工作区回滚到之前的commit状态

【参考】:https://www.yiibai.com/git/git_reset.html#

git revert命令(补充)

为了演示revert的命令,我们在修改一次上面的文件 modified 3,然后提交:

yitiandeMacBook-Pro:project-demo yitian$ vim new-file-redoing.txt 
yitiandeMacBook-Pro:project-demo yitian$ git add .
yitiandeMacBook-Pro:project-demo yitian$ git commit -m "modified 3"

此时的git log如下:
20190620103413.jpg
执行revert命令:

yitiandeMacBook-Pro:project-demo yitian$ git revert HEAD
[new-branch-for-redoing 76938eb] Revert "modified 3"
 1 file changed, 1 deletion(-)

此时会提示编辑此次revert的一个记录,默认的内容如下:
20190620103208.jpg
保存后退出,查看git log:
20190620103725.jpg
可以看到,git revert将会撤销上一次的提交,并重新提交一次新的提交,此次的提交就和modified 2中的内容一致,查看new-file-redoing.txt:

yitiandeMacBook-Pro:project-demo yitian$ cat new-file-redoing.txt 
this is the file for redoing example!
modified 1
modified 2

如果不想在revert某次提交之后自动提交,进行如下操作,这里为了避免上次revert对演示对影响,首先进行一次reset --hard命令撤销上次的提交,将提交状态回滚到modified 3:

yitiandeMacBook-Pro:project-demo yitian$ git reset --hard 0126fa310f95f7fb92716bcaf6ad2b1a8e68deb2
HEAD is now at 0126fa3 modified 3

查看reset后的log状态:
20190620105020.jpg
然后进行revert操作:

yitiandeMacBook-Pro:project-demo yitian$ git revert -n da654a670f9ad1b42359d09da418800a97acb8fe
error: could not revert da654a6... modified 2
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'

发现有冲突的文件:

yitiandeMacBook-Pro:project-demo yitian$ git status
On branch new-branch-for-redoing
Your branch is ahead of 'origin/new-branch-for-redoing' by 1 commit.
  (use "git push" to publish your local commits)

You are currently reverting commit da654a6.
  (fix conflicts and run "git revert --continue")
  (use "git revert --abort" to cancel the revert operation)

Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

	both modified:   new-file-redoing.txt

no changes added to commit (use "git add" and/or "git commit -a")

冲突文件内容:
20190620105145.jpg
处理冲突,保留modified 1,因为只有这个是commit 1中修改的内容,如果需要保留modified 2和modified 3中的修改的内容,那就根据情况修改冲突文件即可。重新add和commit命令,查看git log如下:

yitiandeMacBook-Pro:project-demo yitian$ git add .
yitiandeMacBook-Pro:project-demo yitian$ git commit -m "revert to modified 1"
[new-branch-for-redoing d6d7f27] revert to modified 1
 1 file changed, 2 deletions(-)

20190620105557.jpg
**总结:**revert和reset的不同主要在于,reset的命令会操作head指向reset后的commit,属于撤销操作。而revert可以理解为反转,它将一次指定的commit进行反转,然后在提交一次新的commit,head始终指向新的commit中。
reset命令执行之后,不会保留目标commit之后的commit状态,而revert命令执行后,目标状态之后的变更可以根据需要进行保存。
在进行一次push命令:

yitiandeMacBook-Pro:project-demo yitian$ git push
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 8 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 587 bytes | 587.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0)
remote: 
remote: To create a merge request for new-branch-for-redoing, visit:
remote:   https://gitlab.com/Yitian.me/project-demo/merge_requests/new?merge_request%5Bsource_branch%5D=new-branch-for-redoing
remote: 
To gitlab.com:Yitian.me/project-demo.git
   da654a6..d6d7f27  new-branch-for-redoing -> new-branch-for-redoing

push之后,gitlab上的状态;
20190620110320.jpg
20190620110406.jpg
【参考】:https://www.cnblogs.com/my–sunshine/p/7093412.html
【参考】:https://blog.csdn.net/yxlshk/article/details/79944535

状态记录

最后记录一下该项目的最终操作:

yitiandeMacBook-Pro:project-demo yitian$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
yitiandeMacBook-Pro:project-demo yitian$ git merge new-branch-for-redoing
Updating aad7adf..d6d7f27
Fast-forward
 new-file-redoing.txt | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 new-file-redoing.txt
yitiandeMacBook-Pro:project-demo yitian$ ls
README.md			new-file.txt
new-branch.txt			new_branch_file_from_remote.txt
new-file-redoing.txt		text.txt
yitiandeMacBook-Pro:project-demo yitian$ git push
Total 0 (delta 0), reused 0 (delta 0)
To gitlab.com:Yitian.me/project-demo.git
   aad7adf..d6d7f27  master -> master
yitiandeMacBook-Pro:project-demo yitian$ 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值