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

后端 未结 10 1002
挽巷
挽巷 2020-12-13 00:13

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 g

相关标签:
10条回答
  • 2020-12-13 00:40

    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.

    0 讨论(0)
  • 2020-12-13 00:41

    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
    
    0 讨论(0)
  • 2020-12-13 00:46

    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.
    0 讨论(0)
  • 2020-12-13 00:46

    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>
    
    0 讨论(0)
  • 2020-12-13 00:55

    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 讨论(0)
  • 2020-12-13 00:57

    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"

    0 讨论(0)
提交回复
热议问题