Git: move changes off of master branch

后端 未结 6 451
南旧
南旧 2021-02-03 20:53

Basic question but this happens to me all the time:

  • Make changes in a working-branch
  • Switch to master
  • git merg
相关标签:
6条回答
  • 2021-02-03 21:16

    Get in the habit of typing $ git status before you actually do a git command that will modify something.

    Given that, you have probably edited your file but not checked it in, because you would run git status before the commit. In this case, git does the right thing if you just switch branches, then commit.

    If you have fired a commit off to master, then just move the file between branches with something like this:

     $ git checkout --patch master <somefile>
    

    You don't really have to reset master if you are just going to merge the same file with it, but since presumably you haven't pushed anything yet you should just reset to your remote tracking branches...

    $ git reset master origin/master
    $ git reset stage origin/stage # whatever
    
    0 讨论(0)
  • 2021-02-03 21:20

    I used for similar cases:

    git branch -f <branch-name>
    git checkout <branch-name>
    

    or

    git checkout -B <branch-name>
    

    .

    Both variants move the branch branch-name to your current commit with out reseting-hard your tree.

    0 讨论(0)
  • 2021-02-03 21:25

    1. prevent direct edits on master (warning perhaps)

    You're not the only one to want this. The best idea I've come across is to put the git branch directly in your shell prompt. My prompt looks like this:

    [user@host directory:git_branch]
    

    I also color the git_branch entry, so it's quite obvious what I'm working on at all times. These two links on Stack Overflow should help with your prompt.

    2. to move all edits over to working-branch and clear master so I can continue editing on working-branch

    or

    3. to spin edits into an entirely new branch new-working-branch and then discard working-branch?

    These are really the same question - how to move changes off of master onto a branch, whether it's an old branch or a new branch. And your own answer is correct. Although at second glance, assuming you're on master, you could more simply run:

    git branch new_branch
    git reset --hard origin/master
    

    I prefer to just reset master to origin/master rather than worry about a specific commit SHA. But your steps were essentially correct. As to why you lost changes, I'd have to think that by mistake there wasn't a branch pointer to Q when you reset master. No other explanation makes sense. Again, having the branch shell prompt will help avoid these mistakes. Further more, I'm a big fan of using gitk or git log --graph to verify where my branches are before I move them around. Since I can't easily use gitk at work, I have an alias in my .gitconfig called "graph," which is essentially a command-line version of it:

    [alias]
        graph = log --graph --all --date=short --pretty=format':%C(yellow)%h%Cblue%d%Creset %s %Cgreen %aN, %ad%Creset'
    

    This will show the graph on the far left, the commit SHA in yellow, the branches in blue, the commit message in white, and the author & date in green. Of course this can be modified to your own liking.

    [edited to make the above commands simpler]

    ==============================

    In response to the comment below:

    Start with

    A-B < origin/master
       \
        C-D < master
    

    Now perform git checkout -b new_branch

    A-B < origin/master
       \
        C-D < master, new_branch
    

    Now checkout master, git checkout master. Note that git checkout -b new_branch && git checkout master is the same as git branch new_branch if you were already on master. I edited the above answer to reflect this.

    Now reset master to origin/master, git reset --hard origin/master

    A-B < master, origin/master
       \
        C-D < new_branch
    

    Because you had a branch (new_branch) pointing at D, no changes are lost. If I've made a mistake, please elaborate where.

    0 讨论(0)
  • 2021-02-03 21:26

    From your description, I assume that you did not commit any changes yet – is that correct?

    If yes, here’s your answers:

    How to prevent direct edits to master

    You would need to set that in your editor, but that will probably be difficult. Displaying your current branch in your prompt and your editor helps a lot.

    How to move the changes into a new branch new-working-branch and then discard working-branch

    git checkout -b new-working-branch
    git add …
    git commit -m "mycommit" 
    

    As you didn’t commit anything to master yet, you don’t need to change anything on master. You can now discard your working-branch if you feel like it.

    How to move the changes over to working-branch

    git checkout -b temp-branch
    git add …
    git commit -m "mycommit" 
    git rebase --onto working-branch master
    git checkout working-branch
    git reset --hard temp-branch
    git branch -d temp-branch
    

    If your changes don’t conflict with any changes that are on master, but not in working-branch, this can be done a lot simpler:

    git stash
    git checkout working-branch
    git stash pop
    
    0 讨论(0)
  • 2021-02-03 21:35

    I generally recommend the following Git setting:

    git config push.default nothing
    

    With this, you will at least have to name the branch when you push. It won't stop you from committing to master locally, but when you realize you have, you can move those commits to a branch without affecting anyone else.

    0 讨论(0)
  • 2021-02-03 21:37

    If you already committed your changes to master but didn't push to anywhere...

    create a new branch for the last changes

    git checkout -b newfeat master
    

    replay all the changes (move the commits) on top of your working-branch branch

    git rebase --onto working-branch origin/master newfeat
    

    change to master branch and reset it to the state of the last push

    git checkout master
    git reset --hard origin/master
    

    At this point you have:

    • master pointing to the last pushed commit (origin/master)
    • working-branch never changed
    • a new newfeat branch that contains all the new commits and is ahead of working-branch.
    0 讨论(0)
提交回复
热议问题