开发者

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

I have currently one of the project it conta开发者_如何学Goin more branches. master branch not yet merger long time. So i want switched to master with latest code.

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?


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

git checkout master
git checkout other_branch .

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

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

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

git clean -fd

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


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?

git checkout develop
git merge master


Firstly, I would try just merging it to master and 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 status is clean before you start, and you find that there are too many conflicts to deal with, you can just go back with:

git reset --merge

However, if you just want to make your current master the same as your other branch, you could do that with:

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 master beforehand.)
  2. You're rewriting the history of the master branch, so if you've ever shared this branch with anyone the results will be confusing for both of you.
  3. git reset --hard should always be used with caution, since it will throw away all uncommitted changes you have.


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:

git branch -f master


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


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

  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"


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

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

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

However, if you want to take all commits from branch into master, you won't get around merging and resolve conflicts (unless you git reset --hard other-branch the master branch as described – and discouraged – by Mark).


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

git checkout master
git rebase <working branch>


In IntelliJ at least, you can do the following:

  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

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜