如何将当前工作分支移动到 git 中的 master 分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7161860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:40:33  来源:igfitidea点击:

How to move the current working branch to master branch in git

git

提问by user857574

I have currently one of the project it contain more branches. master branch not yet merger long time. So i want switched to master with latest code.

我目前有一个包含更多分支的项目。master 分支还没有合并很长时间。所以我想用最新的代码切换到master。

Can you please give me necessary commands and steps.

你能给我必要的命令和步骤吗?

Let me re-frame my question.

让我重新定义我的问题。

I am working in a project with different branches. The latest changes were in some branch other than master. Now my requirement is: is there a way to move the current working branch to master without merging it with master, to avoid conflicts?

我正在一个具有不同分支的项目中工作。最新的更改发生在 master 以外的某个分支中。现在我的要求是:有没有办法将当前工作分支移动到 master 而不与 master 合并,以避免冲突?

回答by radistao

If you want to have in masterexactly the same files state as in other_branchand save history - do the following (and note the period at the end):

如果您想在master拥有other_branch 中完全相同的文件状态并保存历史记录 - 执行以下操作(并注意末尾的句点):

git checkout master
git checkout other_branch .

Now you will have a full copy of other_branchin current master(it is softer than reset), not yet committed. Then make a regular commit:

现在,您将在当前master 中拥有other_branch的完整副本(它比 软),尚未提交。然后进行常规提交:reset

git add --all
git commit -m "* copy other_branch to master working tree"

Note:untracked (unindexed) files (directories) from other_branchwill remain, if they are not tracked by master. To remove those untracked files (directories):

注意:如果 master 没有跟踪,other_branch 中未跟踪(未索引)的文件(目录)将保留。要删除那些未跟踪的文件(目录):

git clean -fd

See How to remove local (untracked) files from the current Git working tree?for more details.

请参阅如何从当前 Git 工作树中删除本地(未跟踪)文件?更多细节。

回答by Grad van Horck

Do you want to merge a 'develop' branch into master?

您想将“开发”分支合并到主分支吗?

git checkout master
git merge develop

Or do you want to merge the changes from master into your development branch?

或者您想将 master 的更改合并到您的开发分支中?

git checkout develop
git merge master

回答by Mark Longair

Firstly, I would try just merging it to masterand seeing if there really are lots of conflicts - git's merging is great at only marking genuine conflicts. If you make sure that your git statusis clean before you start, and you find that there are too many conflicts to deal with, you can just go back with:

首先,我会尝试将其合并master并查看是否真的有很多冲突 - git 的合并非常适合仅标记真正的冲突。如果您git status在开始之前确保自己是干净的,并且发现有太多冲突需要处理,则可以返回:

git reset --merge

However, if you just want to make your current masterthe same as your other branch, you coulddo that with:

但是,如果您只想使当前master分支与其他分支相同,则可以使用以下方法:

git checkout master
git reset --hard other-branch

However, that's generally a bad idea since:

但是,这通常是一个坏主意,因为:

  1. You're throwing away all the work on your master branch. Perhaps you might want that history later? (You could always "back up" your master branch with git branch old-master masterbeforehand.)
  2. You're rewriting the history of the masterbranch, so if you've ever shared this branch with anyone the results will be confusing for both of you.
  3. git reset --hardshould always be used with caution, since it will throw away all uncommitted changes you have.
  1. 您正在丢弃主分支上的所有工作。也许你以后可能想要这段历史?(你总是可以git branch old-master master事先“备份”你的主分支。)
  2. 你正在重写master分支的历史,所以如果你曾经与任何人共享过这个分支,结果会让你们俩都感到困惑。
  3. git reset --hard应始终谨慎使用,因为它会丢弃您拥有的所有未提交的更改。

回答by Karl Bielefeldt

If I understand your English correctly, you don't want to merge your changes back into master, but just reset master to point to the latest commit in your currently checked out branch? To do this, use:

如果我正确理解您的英语,您不想将更改合并回 master,而只是将 master 重置为指向当前签出分支中的最新提交?为此,请使用:

git branch -f master

回答by hvgotcodes

you need to merge your current branch into the master branch. the way i do it is:

您需要将当前分支合并到主分支中。我这样做的方式是:

1) git fetch origin  # get all branches from server  
2) git rebase master # update your local master to the origin master, in case master has changed upstream
3) git checkout <branch>  # go to your branch
4) git rebase master # rebase your branch to master; applies your branch's changes ontop of the master, where it diverged
5) git checkout master # go back to master
6) git merge <branch> # merge your branch into master.  since you rebased, this should be trivial
7) git push # push your master upstream

回答by Chitra Lekha

If you want to copy the files from the branch to master do execute following commands.

如果要将文件从分支复制到 master,请执行以下命令。

  1. git checkout master
  2. git checkout branch_from_which_you_have_to_copy_the_files_to_master .

    (with period)

  3. git add --all

  4. git push -u origin master

  5. git commit -m "copy from branch to master"

  1. git结帐大师
  2. git checkout branch_from_which_you_have_to_copy_the_files_to_master 。

    (带句号)

  3. git add --all

  4. git push -u origin master

  5. git commit -m "从分支复制到主节点"

回答by eckes

If you want to take some commits from branchto master, you could use cherry-picking.

如果你想利用一些提交branchmaster,你可以使用挑肥拣瘦

This takes only certain commits from branchto master, but is – of course – not guaranteed that no conflicts occur.

这仅需要从branchto 进行某些提交master,但是——当然——不能保证不会发生冲突。

See my answerto Managing hotfixes when develop branch is very different from master?

请参阅在开发分支与主分支非常不同时管理修补程序的回答

However, if you want to take allcommits from branchinto master, you won't get around merging and resolve conflicts (unless you git reset --hard other-branchthe masterbranch as described – and discouraged – by Mark).

但是,如果您想将所有提交从branchinto 中取出master,您将无法绕过合并和解决冲突(除非您git reset --hard other-branch使用Markmaster所描述的并且不鼓励的分支)。

回答by Sammy S.

If you want to merge the working branch into the master, you can also use rebase which is a bit cleaner.

如果要将工作分支合并到主分支,还可以使用更简洁的 rebase。

git checkout master
git rebase <working branch>

回答by B. Alvey

In IntelliJ at least, you can do the following:

至少在 IntelliJ 中,您可以执行以下操作:

  1. Checkout the branch to merge into master.
  2. VCS->Git->Merge Changes
  3. Select master
  4. Change the strategy to "ours"
  5. Select "No fast forward."
  6. Click "merge"
  7. Commit (may not be needed)
  8. Push
  1. 签出要合并到 master 的分支。
  2. VCS->Git->合并更改
  3. 选择大师
  4. 将策略更改为“我们的”
  5. 选择“不快进”。
  6. 点击“合并”
  7. 提交(可能不需要)

This turns master into your current branch. The history for master is preserved. I'm sure there is a git command line equivalent.

这会将 master 变成您当前的分支。保留 master 的历史记录。我确定有一个等效的 git 命令行。

回答by DantaliaN

Maybe it's not so clean, but it works for me

也许它不是那么干净,但它对我有用

  • git pull #to get all changes
  • switch to my_branch
  • copy all files tmp dir
  • switch to master
  • remove all files in master
  • copy files from tmp dir
  • commit
  • git pull #获取所有更改
  • 切换到 my_branch
  • 复制所有文件 tmp 目录
  • 切换到主
  • 删除 master 中的所有文件
  • 从 tmp 目录复制文件
  • 犯罪