码迷,mamicode.com
首页 > 其他好文 > 详细

Git 协作:Fetch Pull Push Branch Remote相关

时间:2019-09-19 19:38:57      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:cts   mit   常用   diff   情况   合作开发   信息   git clone   撤销   

前言

学习git的时候,我们首先学习的是最常用的,自己独立开发Software时用的命令:

git init                                                                 #初始化git仓库
git add <file_name>                                              #将文件添加到暂存区
git rm <file_name>                                              #将暂存区的该文件删除
git commit -m "<commit info>"                             #将暂存区的修改提交到当前分支
git status                                                             #查看当前状态
git reset --hard <commit_id>                                #切换到commit_id对应的版本
git checkout -- <file_name>                                  #撤销file_name文件的工作区修改
git reset HEAD <file_name>                                  #撤销暂存区的修改,将其放回工作区
git diff HEAD -- <file_name>                                 #查看工作区和版本库里面file_name对应文件最新版本的区别
git checkout <branch_name>                                 #切换到该branch
git checkout -b <branch_name>                             #创建名为<branch_name>的branch并切换到该branch
git clone <remote_git_address>                              #克隆远端仓库到本地

 

而在实际工作中,我们往往要与他人一起合作开发/Debug。这个时候,就得学习在合作开发/Debug的情况下遇到的各种情况该怎么办,什么样的Git命令能解决我们的问题。

场景1: 需要拉取陌生远端分支到本地。我们本地有一个自己的repo, 此时同事遇到一个比较难的Bug想请你帮忙一起看看,但是这个Bug在你自己的repo的branch上没有,于是同事将其branch推送到了他自己的Github远端仓库里,然后把远端仓库的git address和branch name发给你了。此时我们该怎么办呢?

我们应该:

1. 添加一个远端源 git remote add <created_remote_name> <github_repo_git_address>
2. 将远端源的信息拉过来 git fetch <created_remote_name>
3. 拉取远端源repo的一个分支到本地分支 git checkout -b <local_branch_name> <created_remote_name>/<branch_name>

场景2: 本地与远端同一分支提交历史不一致。本地与远端同一分支提交历史不一致。多个人在同一个分支上协作时,出现冲突是很正常的,比如现在有一个项目由我和A一同开发。我在修复了一个bug以后准备提交,现在准备推送到远端。push失败了,发现A在我之前已经提交了,我本地master分支的提交历史已经落后远端了,需要先pull一下,与远端同步后才能push。

直接执行 git pull --rebase

场景3:其他场景

将目标分支的代码Merge到本地分支 git merge <object_branch>
 (if has conflicts and want to just cover the current code with object branch code, add "--strategy-option theirs" parameter)
将本地分支代码推送到远端源指定分支 git push <local_branch_name> <created_remote_name>:<branch_name>
 (if want to cover the remote code with local code, add "-f" parameter)

 

Git 协作:Fetch Pull Push Branch Remote相关

标签:cts   mit   常用   diff   情况   合作开发   信息   git clone   撤销   

原文地址:https://www.cnblogs.com/ArsenalfanInECNU/p/11551803.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!