Git的git fetch, git merge和git pull, git rebase操作

Git push,merge,pull,fetch,rebase各自在什么场景下使用

基本上顺序是这样的:

你修改好了代码,准备要提交到远程仓库,但是此时有可能其他人已经在此分支上已经进行了修改,那么该如何操作呢?这里我们就使用之前模拟多用户的案例来进行试验。

1. 首先提交本地仓库 git commit -am "提交信息"

git commit -am 是将 git add . 和 git commit -m "commit" 操作合并 

2. 将远程仓库中的代码拉取下来

有两种方法来把你的代码和远程仓库中的代码合并

1. git pull 这样就直接把你本地仓库中的代码进行更新但问题是可能会有冲突(conflicts),个人不推荐。2. 先 git fetch origin(把远程仓库中origin最新代码取回),再 git merge origin/master(把本地代码和已取得的远程仓库最新代码合并),如果你的改动和远程仓库中最新代码有冲突,会提示,再去一个一个解决冲突,然后再次提交本地仓库,最后提交远程仓库即可,如果没有冲突,git push origin master,把你的改动推送到远程仓库中。

git pull 操作实战

1. 假设现在你已经在本地进行了修改,并且已经提交到本地仓库,此时推送代码到远程仓库

# bob修改了文字,并提交到本地仓库,然后推送到远程仓库,结果发生冲突
zfz:gitlearn_bob zhangfengzhou$ git branch
* master
zfz:gitlearn_bob zhangfengzhou$ vim ceshi.txt
zfz:gitlearn_bob zhangfengzhou$ git add ceshi.txt
zfz:gitlearn_bob zhangfengzhou$ git commit -m "bob添加了第二段文字"
[master 9e6f504] bob添加了第二段文字
 1 file changed, 3 insertions(+)
zfz:gitlearn_bob zhangfengzhou$ git push origin master
Enter passphrase for key '/Users/zhangfengzhou/.ssh/id_ed25519':
To gitee.com:happiness365/gitlearn.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@gitee.com:happiness365/gitlearn.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

2. 以上提示我们如果想整合代码,需要在推送之前使用 git pull

# git pull 下拉代码,发生冲突,准备解决冲突
zfz:gitlearn_bob zhangfengzhou$ git pull
Enter passphrase for key '/Users/zhangfengzhou/.ssh/id_ed25519':
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From gitee.com:happiness365/gitlearn
   a67b558..b257b17  master     -> origin/master
Auto-merging ceshi.txt
CONFLICT (content): Merge conflict in ceshi.txt
Automatic merge failed; fix conflicts and then commit the result.

3. 上面的提示告诉我们,在自动合并的时候,合并失败,需要我们自己修复冲突,然后提交

# 上面的提示告诉我们,冲突是发生在 ceshi.txt 文件
# vim ceshi.txt 之后,对冲突的地方进行修改
# 通过观察发现,<<<<<< HEAD 到 ======= 之间的代码是我们自己写的代码
# 而 ======= 到 >>>>>>>>>>> b255....a48 之间的代码是别人写的
# 我们需要考虑是否移除别人的代码,还是同时保留自己和别人的代码

bob 写了一段文字
<<<<<<< HEAD
bob 写了第二段文字


=======
lily 也写了一段文字
>>>>>>> b257b175a80c241e4610700d8e51548330a2ea48
~
# 我们选择同时保留自己和别人的代码,然后保存退出
bob 写了一段文字
bob 写了第二段文字
lily 也写了一段文字

4. 在解决好冲突之后,我们就可以提交代码到远程仓库

# 提交代码到本地仓库,然后提交到远程仓库
$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

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

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

	both modified:   ceshi.txt

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

$ git commit -am "修改冲突,且bob提交了第二段文字"
[master 70fe0b2] 修改冲突,且bob提交了第二段文字

$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

$ git push origin master
Enter passphrase for key '/Users/zhangfengzhou/.ssh/id_ed25519':
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 4 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 653 bytes | 653.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.1]
To gitee.com:happiness365/gitlearn.git
   b257b17..70fe0b2  master -> master

git fetch 操作

1. 首先学习和补充下相关的基础知识

git merge origin master 和 git merge origin/master 的区别?

git merge origin master 是将远程分支master在本地的副本和本地的master分支合并到当前分支
git merge origin/master 是合并远程分支master在本地的副本

# git merge 后面跟的都是要合并到当前分支的分支名称,不存在要合并到其他分支上的意思
git merge branchA branchB branchC .... 

git pull origin master 的操作就相当于 
git fetch origin master 和 git merge origin/master 这两步操作

# git fetch origin master 的意思就是更新远程分支master,
# origin master 和 master不是一个意思,master 是本地分支,origin master是远程分支在本地的副本

# git fetch origin master
# git merge origin branchA 相当于 git merge origin/master branchA 
# 因为origin是远程仓库,没有写具体的名字,默认指向的就是master分支

2. 假设bob已经将修改提交到远程仓库,现在lily也准备基于bob的代码开发新的功能,如何操作?

2.1 首先先切换到lily的工作目录中

$ cd gitlearn_lily/

2.2 更新远程分支master,然后进行合并操作

$ git fetch origin master
Enter passphrase for key '/Users/zhangfengzhou/.ssh/id_ed25519':
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
From gitee.com:happiness365/gitlearn
 * branch            master     -> FETCH_HEAD
   b257b17..70fe0b2  master     -> origin/master

$ git branch
* master

$ git merge origin/master
Updating b257b17..70fe0b2
Fast-forward
 ceshi.txt | 1 +
 1 file changed, 1 insertion(+)

2.3 查看更新的代码

$ git status
On branch master
Your branch is up to date with 'origin/master'.  

nothing to commit, working tree clean

# is up to date with xx 是和xx保持最新的意思

$ cat ceshi.txt
bob 写了一段文字
bob 写了第二段文字
lily 也写了一段文字

2.4 git fetch 操作在merge时遇到冲突,那么就解决冲突,然后重新提交即可,并推送远程仓库。

  • 5
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值