如何将主分支的新更改合并到我的个人分支上 GitHub?

huangapple go评论131阅读模式
英文:

How to get new changes from main branch to my personal branch on github?

问题

步骤1: 检查当前分支

git branch

步骤2: 切换到 'main' 分支

git checkout main

步骤3: 拉取最新的 'main' 分支代码

git pull origin main

步骤4: 切换回 'dev2' 分支

git checkout dev2

步骤5: 合并 'main' 分支到 'dev2' 分支

git merge main

步骤6: 处理可能的冲突(如果有)

# 如果有冲突,手动解决它们,然后继续

步骤7: 推送 'dev2' 分支的更改到远程仓库

git push origin dev2
英文:

There are three branches named 'dev1', 'dev2' and 'main'.
I am working on 'dev2' branch and my friend is working on 'dev1' branch.
We both are fetching and uploading our code from 'main' branch. ('main' branch is the ultimate branch)

Now, suppose I made some changes locally in my 'dev2' branch. Now, I want to push those changes to 'dev2' branch. But, before that, my friend has pushed some changes from'dev1' to 'main' branch. So what should I do?

Please show me step-wise commands for github

for e.g.

step1: git <---do something-->
step2: git <--do something 2--->

答案1

得分: 2

你好,我很乐意回答你的问题。

首先,你在本地工作的步骤在WindowsLinux上相似,其他步骤如下:

可选步骤:

当你位于你的目录中时,你需要确保已创建分支,可以运行以下命令来确认你是否有分支:

git branch

然后按下 "q" 键退出。

同样重要的是要检查你是否已创建仓库,并将其连接到你的仓库:

git remote -v

如果没有的话,可以添加你的仓库,可以选择使用HTTPS或SSH

git remote add origin https://github.com/user/repositoryexample.git

一旦这些都确认好了,你需要将分支推送到GitHub。

以下是逐步命令,用于将更改推送到dev2分支,同时确保你拥有已合并到主分支的朋友的dev1分支的最新更改。

第一步

确保你在dev2分支上,通过在本地仓库运行以下命令:

git checkout dev2

第二步:

接下来,通过运行以下命令来获取主分支的最新更改:

git fetch origin main

第三步:

通过运行以下命令合并主分支的更改到你的dev2分支:

git merge origin/main

第四步:

现在,你的dev2分支已经更新为来自主分支的最新更改,你可以通过运行以下命令将你的更改推送到远程dev2分支:

git push origin dev2

这将把你的更改推送到远程仓库的dev2分支。

注意:如果你的更改与dev1分支的更改存在冲突,Git将提示你在完成合并之前解决它们。在这种情况下,你需要手动解决冲突,然后才能推送你的更改。

其他选择

此外,你也可以只推送你的dev2分支,然后在GitHub上使用Codespace。

  • 首先,确保你在dev2分支上:
git checkout dev2
  • 通过运行以下命令将dev2分支推送到远程仓库:
git push origin dev2

现在你可以使用Codespace

https://github.dev/github/dev

附加信息

当你完成这些步骤后,你可能会想知道如何从GitHub更新你的分支以及如何管理你的分支

英文:

Hello, I will gladly answer your question.

First, you work in local the steps is more similarity in windows that Linux and other steps in

Optional Step:

When you are located in your directory, you need to have the branches created, to make sure you have the branches you can do a

git branch

and quit with "q" key

It is also important to check that you have your repository created, and connected to your repository

git remote -v

If not, add your repository, either with HTTPS, or SSH.

git remote add origin https://github.com/user/repositoryexample.git

Once this is verified, you have to send the branch to github.

> Here are the step-wise commands you can use to push your changes to the dev2 branch while ensuring that you have the latest changes from your friend's dev1 branch, which have been merged into the main branch.

First Step

Ensure that you are on the dev2 branch by running the command in your local repository:

git checkout dev2

Step 2:

Next, fetch the latest changes from the main branch by running command:

git fetch origin main

Step 3:

Merge the changes from main branch into your dev2 branch, running the next command:

git merge origin/main

Step 4:

Now that your dev2 branch is up to date with the latest changes from main, you can push your changes to the remote dev2 branch by running the following command:

git push origin dev2

This will push your changes to the dev2 branch on the remote repository.

Note: If there are conflicts between your changes and the changes from dev1 branch, Git will prompt you to resolve them before you can complete the merge. In this case, you will need to resolve the conflicts manually before you can push your changes.


Other option

Also, you can just push your dev2 branch and on GitHub you can work with the Codespace.

  • First, ensure that you are on the dev2 branch
git checkout dev2

  • Push the dev2 branch to the remote repository by running the following command:
git push origin dev2

Now you can use the codespace

https://github.dev/github/dev


Additional info.

> When you have these steps, you would be interested to know how to update your branches from GitHub and manage your branches.

答案2

得分: 0

确保你当前的分支(dev1)是最新的,以避免冲突。

你可以将你当前分支上的修改暂存(这将把你的修改保存在一个暂存区,然后你可以在拉取最新的 main 分支之后再恢复这些修改):

git stash

然后切换到 main 分支并拉取最新的变更:

git checkout main
git pull

接着你可以切换回你的分支:

git checkout dev1

然后将 main 分支的最新变更合并到你的分支上:

git merge main

在这个步骤之后,你可以将之前暂存的修改应用到你刚刚更新的分支上:

git stash pop

现在你的分支(dev1)包含了 main 分支的所有最新变更,以及你本地所做的修改。

英文:

You need to make sure your current branch(dev1) is up to date with the main branch to avoid conflicts.

You can stash your changes on your current branch(this will store your changes in a stash while you pull the latest on main):

git stash

Then check out main and pull the latests changes

git checkout main
git pull

From there you can check out your branch:

git checkout dev1

Then merge the latest changes from main into your branch:

git merge main

After that step you can pop your stash into your newly updated branch:

git stash pop

And now your branch(dev1) has all the latest changes from main as well as the changes you've made locally

答案3

得分: 0

Step1: 将你的本地更改推送到dev2分支。

Step2: 从远程获取主分支上由你的朋友所做的更改。

Step3: 合并你的本地dev2分支与主分支上你的朋友所做的更改。

英文:

Step1: git push dev2 your local changes to dev2 branch. (This wont affect the main branch, or changes made by your friend to the main branch)

Step2: git fetch origin main (Will bring changes made by your friend to local)

Step3: git merge origin/main (will merge your friends changes on main to your local dev2)

huangapple
  • 本文由 发表于 2023年3月8日 14:55:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定