Push commits to another branch

前端 未结 9 1239
走了就别回头了
走了就别回头了 2020-11-29 14:38

Is it possible to commit and push changes from one branch to another.

Assume I commited changes in BRANCH1 and want to push them to BRANCH2<

相关标签:
9条回答
  • 2020-11-29 14:51

    when you pushing code to another branch just follow the below git command. Remember demo is my other branch name you can replace with your branch name.

    git push origin master:demo
    
    0 讨论(0)
  • 2020-11-29 14:55

    git init 
    #git remote remove origin
    git remote add origin  <http://...git>
    echo "This is for demo" >> README.md 
    git add README.md
    git commit -m "Initail Commit" 
    git checkout -b branch1 
    git branch --list
    ****add files***
    git add -A
    git status
    git commit -m "Initial - branch1"
    git push --set-upstream origin branch1
    #git push origin --delete  branch1
    #git branch --unset-upstream  

    0 讨论(0)
  • 2020-11-29 15:01

    you can do this easily

    git status
    git add .
    git commit -m "any commit"
    git pull origin (branch name, master in my case)
    git push origin current branch(master):branch 2(development)(in which you want to push changes)
    
    0 讨论(0)
  • 2020-11-29 15:04

    You have committed to BRANCH1 and want to get rid of this commit without losing the changes? git reset is what you need. Do:

    git branch BRANCH2
    

    if you want BRANCH2 to be a new branch. You can also merge this at the end with another branch if you want. If BRANCH2 already exists, then leave this step out.

    Then do:

    git reset --hard HEAD~3
    

    if you want to reset the commit on the branch you have committed. This takes the changes of the last three commits.

    Then do the following to bring the resetted commits to BRANCH2

    git checkout BRANCH2
    

    This source was helpful: https://git-scm.com/docs/git-reset#git-reset-Undoacommitmakingitatopicbranch

    0 讨论(0)
  • 2020-11-29 15:05

    In my case I had one local commit, which wasn't pushed to origin\master, but commited to my local master branch. This local commit should be now pushed to another branch.

    With Git Extensions you can do something like this:

    • (Create if not existing and) checkout new branch, where you want to push your commit.
    • Select the commit from the history, which should get commited & pushed to this branch.
    • Right click and select Cherry pick commit.
    • Press Cherry pick button afterwards.
    • The selected commit get's applied to your checked out branch. Now commit and push it.
    • Check out your old branch, with the faulty commit.
    • Hard reset this branch to the second last commit, where everything was ok (be aware what are you doing here!). You can do that via right click on the second last commit and select Reset current branch to here. Confirm the opperation, if you know what you are doing.

    You could also do that on the GIT command line. Example copied from David Christensen:

    I think you'll find git cherry-pick + git reset to be a much quicker workflow:

    Using your same scenario, with "feature" being the branch with the top-most commit being incorrect, it'd be much easier to do this:

    git checkout master
    git cherry-pick feature
    git checkout feature
    git reset --hard HEAD^

    Saves quite a bit of work, and is the scenario that git cherry-pick was designed to handle.

    I'll also note that this will work as well if it's not the topmost commit; you just need a commitish for the argument to cherry-pick, via:

    git checkout master
    git cherry-pick $sha1
    git checkout feature
    git rebase -i ... # whack the specific commit from the history

    0 讨论(0)
  • 2020-11-29 15:06

    That will almost work.

    When pushing to a non-default branch, you need to specify the source ref and the target ref:

    git push origin branch1:branch2
    

    Or

    git push <remote> <branch with new changes>:<branch you are pushing to> 
    
    0 讨论(0)
提交回复
热议问题