如何使用远程主服务器重新建立本地分支

本文详细介绍了如何使用Git重新定位本地分支到远程主分支的过程,包括解决冲突、测试代码、提交更改并将其推送到远程分支的步骤。适用于Git初学者和有经验的用户。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:How to rebase local branch with remote master

I have cloned project from master branch from remote repository remote_repo . 我已经从远程存储库remote_repo master分支中克隆了项目。 I create new branch and I commit to that branch. 我创建一个新分支,然后提交该分支。 Other programmers pushed to remote_repo to master branch. 其他程序员将remote_repo推送到master分支。 I need now to rebase my branch RB onto remote_repo master. 现在,我需要将分支RB重新建立到remote_repo主服务器上。 How to do this ? 这个怎么做 ? What commands to type to terminal ? 在终端输入什么命令?


#1楼

参考:https://stackoom.com/question/XGn3/如何使用远程主服务器重新建立本地分支


#2楼

git pull --rebase origin master
# where --rebase[=(false|true|merges|preserve|interactive)]

#3楼

Note: If you have broad knowledge already about rebase then use below one liner for fast rebase. 注意:如果您已经具备有关变基的广泛知识,那么请使用一根以下的衬纸进行快速变基。 Solution: Assuming you are on your working branch and you are the only person working on it. 解决方案:假设您在工作分支上,并且您是唯一在该分支上工作的人。

git fetch && git rebase origin/master

Resolve any conflicts, test your code, commit and push new changes to remote branch. 解决所有冲突,测试您的代码,提交并将新更改推送到远程分支。

                            ~:   For noobs   :~

The following steps might help anyone who are new to git rebase and wanted to do it without hassle 以下步骤可能会帮助git rebase新手并希望轻松完成此操作

Step 1: Assuming that there are no commits and changes to be made on YourBranch at this point. 步骤1:假设此时没有在YourBranch上进行提交和更改。 We are visiting YourBranch. 我们正在访问YourBranch。

git checkout YourBranch
git pull --rebase

What happened? 发生了什么? Pulls all changes made by other developers working on your branch and rebases your changes on top of it. 提取分支上其他开发人员所做的所有更改,并在此基础上重新进行更改。

Step 2: Resolve any conflicts that presents. 步骤2:解决出现的任何冲突。

Step 3: 第三步:

git checkout master
git pull --rebase

What happened? 发生了什么? Pulls all the latest changes from remote master and rebases local master on remote master. 从远程主机上提取所有最新更改,并在远程主机上重新建立本地主机的基础。 I always keep remote master clean and release ready! 我总是保持远程主机干净并准备发布! And, prefer only to work on master or branches locally. 并且,只希望在本地的master或分支上工作。 I recommend in doing this until you gets a hand on git changes or commits. 我建议您这样做,直到您对git进行更改或提交为止。 Note: This step is not needed if you are not maintaining local master, instead you can do a fetch and rebase remote master directly on local branch directly. 注意:如果您不维护本地主服务器,则不需要此步骤,而是可以直接在本地分支上进行获取和重新设置远程主服务器的基准。 As I mentioned in single step in the start. 正如我在一开始提到的那样。

Step 4: Resolve any conflicts that presents. 步骤4:解决出现的任何冲突。

Step 5: 步骤5:

git checkout YourBranch
git rebase master

What happened? 发生了什么? Rebase on master happens 依靠大师发生

Step 6: Resolve any conflicts, if there are conflicts. 步骤6:解决任何冲突(如果存在冲突)。 Use git rebase --continue to continue rebase after adding the resolved conflicts. 添加已解决的冲突后,请使用git rebase --continue继续进行重新设置。 At any time you can use git rebase --abort to abort the rebase. 您可以随时使用git rebase --abort中止rebase。

Step 7: 步骤7:

git push --force-with-lease 

What happened? 发生了什么? Pushing changes to your remote YourBranch. 将更改推送到您的远程YourBranch。 --force-with-lease will make sure whether there are any other incoming changes for YourBranch from other developers while you rebasing. --force-with-lease将确保您在重新定基础时,YourBranch是否从其他开发人员收到其他任何更改。 This is super useful rather than force push. 这是超级有用的方法,而不是强行推动。 In case any incoming changes then fetch them to update your local YourBranch before pushing changes. 如果有任何传入的更改,则在推送更改之前先获取它们以更新您的本地YourBranch。

Why do I need to push changes? 为什么我需要推动变更? To rewrite the commit message in remote YourBranch after proper rebase or If there are any conflicts resolved? 要在正确调整基准后在远程YourBranch中重写提交消息,或者是否解决了任何冲突? Then you need to push the changes you resolved in local repo to the remote repo of YourBranch 然后,您需要将在本地存储库中解决的更改推送到YourBranch的远程存储库中

Yahoooo...! Yahoooo ...! You are succesfully done with rebasing. 您已经成功完成了基础调整。

You might also be looking into doing: 您可能还会考虑这样做:

git checkout master
git merge YourBranch

When and Why? 什么时候以及为什么? Merge your branch into master if done with changes by you and other co-developers. 如果您和其他共同开发人员进行了更改,则将分支合并到master。 Which makes YourBranch up-to-date with master when you wanted to work on same branch later. 当您以后想要在同一个分支上工作时,这可以使YourBranch与master保持最新。

                            ~:   (๑ơ ₃ ơ)♥ rebase   :~

#4楼

git fetch origin master:master pulls the latest version of master without needing to check it out. git fetch origin master:master不需要检出最新版本的master。

So all you need is: 因此,您需要做的是:

git fetch origin master:master && git rebase master 👌 git fetch origin master:master && git rebase master master👌


#5楼

1.Update Master first... 1.先更新Master ...

git checkout [master branch]
git pull [master branch]

2.Now rebase source-branch with master branch 2.现在使用master分支对源分支进行重新设置

git checkout [source branch]
git rebase [master branch]
git pull [source branch] (remote/source branch)
git push [source branch]

IF source branch does not yet exist on remote then do: 如果远程上尚不存在源分支,请执行以下操作:

git push -u origin [source branch]

"et voila..." “等等!”


#6楼

Step 1: 第1步:

git fetch origin

Step 2: 第2步:

git rebase origin/master

Step 3:(Fix if any conflicts) 第3步:(解决任何冲突)

git add .

Step 4: 第四步:

git rebase --continue

Step 5: 步骤5:

git push --force
### 如何创建 Git 本地分支Git 中,可以通过 `git branch` 或 `git checkout -b` 命令来创建一个新的本地分支。以下是关于如何创建本地分支的具体说明: #### 使用 `git branch` 创建本地分支 可以使用以下命令单独创建一个新分支而不立即切换到该分支: ```bash git branch分支名称 ``` 此命令会基于当前所在的分支创建一个新的分支[^1]。 #### 使用 `git checkout -b` 创建并切换到新分支 如果希望在创建新分支的同时也切换到这个新分支上工作,则可以使用如下命令: ```bash git checkout -b 新分支名称 ``` 这条命令实际上是两条命令的组合形式:先执行 `git branch分支名称` 来创建分支,再通过 `git checkout 新分支名称` 切换至新建的分支。 完成上述任一操作之后,可通过下面的命令验证是否成功创建了新的分支以及查看当前所在分支的状态: ```bash git branch ``` 这将列出所有的本地分支,并以星号 (`*`) 标记当前所处的活动分支[^3]。 为了进一步确认本地分支远程分支的关系或者检查所有分支(包括远程分支),可运行以下命令: ```bash git branch -a # 显示所有本地远程分支列表 git branch -vv # 更详细的显示每个分支及其追踪信息 ``` 当需要让刚刚创建好的本地分支关联到某个特定的远程分支时,可以按照这种方式设置上游分支: ```bash git push --set-upstream origin 新分支名称 ``` 这样不仅推送了本地的新分支数据给远端服务器上的同名分支,还建立了两者之间的联系以便后续提交可以直接简化成简单的 `git push` 而无需额外参数。 对于已经存在但是尚未配置跟踪关系的老分支来说,也可以手动指定其对应哪个远程分支作为它的upstream: ```bash git branch --set-upstream-to=origin/已有远程分支名 当前本地分支名 ``` 这里需要注意的是两个名字应该完全匹配才能正常生效[^2]。 以上就是有关于Git环境下怎样去构建属于自己的开发线程即所谓“branch”的基本流程介绍。 ```python # 示例 Python 代码仅用于展示格式化效果,实际不涉及逻辑实现 def create_git_branch(branch_name): import os command = f"git checkout -b {branch_name}" result = os.system(command) return result == 0 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值