in Technology by
Move the most recent commit(s) to a new branch with Git?

1 Answer

0 votes
by
 
Best answer

Moving to an existing branch

If you want to move your commits to an existing branch, it will look like this:

git checkout existingbranch
git merge master
git checkout master
git reset --hard HEAD~3 # Go back 3 commits. You *will* lose uncommitted work.
git checkout existingbranch

Moving to a new branch

WARNING: This method works because you are creating a new branch with the first command: git branch newbranch. If you want to move commits to an existing branch you need to merge your changes into the existing branch before executing git reset --hard HEAD~3 (see Moving to an existing branch above). If you don't merge your changes first, they will be lost.

Unless there are other circumstances involved, this can be easily done by branching and rolling back.

# Note: Any changes not committed will be lost.
git branch newbranch      # Create a new branch, saving the desired commits
git reset --hard HEAD~3   # Move master back by 3 commits (Make sure you know how many commits you need to go back)
git checkout newbranch    # Go to the new branch that still has the desired commits

But do make sure how many commits to go back. Alternatively, you can instead of HEAD~3, simply provide the hash of the commit (or the reference like origin/master) you want to "revert back to" on the master (/current) branch, e.g:

git reset --hard a1b2c3d4

*1 You will only be "losing" commits from the master branch, but don't worry, you'll have those commits in newbranch!

WARNING: With Git version 2.0 and later, if you later git rebase the new branch upon the original (master) branch, you may need an explicit --no-fork-point option during the rebase to avoid losing the carried-over commits. Having branch.autosetuprebase always set makes this more likely.

Related questions

0 votes
    What is the Git command to move branch pointer to different commit without checkout? A. Git cherrypick B. Git update-ref C. Git reset head...
asked Dec 24, 2022 in Technology by JackTerrance
0 votes
    I started some work on a new feature and after coding for a bit, I decided this feature should be on its ... on the new feature. Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    What is the command to pick a commit from a specific branch and move it to another branch? A. Git move commit B. Git cherry-pick C. Git move branch D. None of the options...
asked Dec 24, 2022 in Technology by JackTerrance
0 votes
    Which of the following creates new commit when you pull new changes from master to the feature branch? A. git rebase master B. git merge master C. git pull origin master...
asked Dec 20, 2022 in Technology by JackTerrance
0 votes
    I would like to know how to delete a commit. By delete, I mean it is as if I didn't make that commit, ... -hard HEAD. Is this correct? Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    How to push a new local branch to a remote Git repository and track it too?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    I have created a branch named dev. I have done a pull request to send dev code to master, when I do ... to resolve these conflicts. Select the correct answer from above options...
asked Jan 31, 2022 in Education by JackTerrance
0 votes
    I need to move all my work from my branch to another branch, but I'm not sure what is the best solution ... changes on the new branch? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Update the README.md file in the GitHub Branch starfeature and commit the updates. Which channel would receive ... . Stakeholders C. Starprojectteam D. None of the options...
asked Dec 23, 2022 in Education by JackTerrance
0 votes
    Update the README.md file in the GitHub Branch starfeature and commit the updates. Which channel would receive ... . Stakeholders C. Starprojectteam D. None of the options...
asked Dec 16, 2022 in Education by JackTerrance
0 votes
    The most recent version of standard SQL prescribed by the American National Standards Institute is (a) SQL ... Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    Recently, I have been asked to cherry-pick a commit. So what does cherry-picking a commit in git mean? How do you do it? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    How git replace with origin branch?...
asked Dec 10, 2020 in Technology by JackTerrance
0 votes
    My git workspace is dirty, there are some local modifications. When I use the command git pull origin master it ... Ansible git module? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Why there is a need to move the existing directory into a new repository? How this can be done in SVN?...
asked Feb 18, 2021 in Technology by JackTerrance
...