50

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.

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?

Gabriele
  • 420
  • 4
  • 15
user857574
  • 821
  • 1
  • 8
  • 7
  • 1
    You might have to clarify your question, I'm afraid, in order for anyone to be able to help. Would I be right in understanding that you're working on a repository with many branches, but those have not been merged back to the `master` branch for some time, and now you want to merge the work from one or more of those branches into `master`? – Mark Longair Aug 23 '11 at 13:40
  • 1
    I don't understand what you mean with "moving" the branch to master? Do you want to work on the master branch? Than "git checkout master" should do the trick. Also, this seem to be a pure git question, you should probably remove the ruby-on-rails tag. – Droggl Aug 25 '11 at 08:46
  • If your code creates conflicts with master, then any code movement must fix these conflicts. This *branch code move to another* in GIT is called *merge*. – Justinas Jul 16 '19 at 08:35

10 Answers10

80

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.

mahemoff
  • 44,526
  • 36
  • 160
  • 222
radistao
  • 14,889
  • 11
  • 66
  • 92
28

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
Grad van Horck
  • 4,488
  • 1
  • 21
  • 22
16

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.
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
11

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
Karl Bielefeldt
  • 47,314
  • 10
  • 60
  • 94
  • 1
    I'm not the original requestor, but this is exactly what I needed, and I think it speaks to the intent of the original requestor – Happyblue Apr 26 '22 at 03:36
5

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
hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
4

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

  1. git checkout master

  2. git checkout x_branch .

    (with period)

  3. git add --all

  4. git commit -m "copy all from x_branch to master"

  5. git push -u origin master

mirekphd
  • 4,799
  • 3
  • 38
  • 59
Chitra Lekha
  • 281
  • 3
  • 4
1

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).

Community
  • 1
  • 1
eckes
  • 64,417
  • 29
  • 168
  • 201
0

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.

B. Alvey
  • 11
  • 2
0

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>
Sammy S.
  • 498
  • 5
  • 12
  • But not recommended for branches, that are already pushed to another remote repository, or branches, frim which other branches were started. – dunni Aug 23 '11 at 13:45
  • You're right. But I assumed, he wanted to do a fast forward for the master branch – Sammy S. Aug 23 '11 at 13:49
-5

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
DantaliaN
  • 155
  • 10