Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Moving to a new branch</h2> <p><strong>WARNING:</strong> This method works because you are creating a new branch with the first command: <code>git branch newbranch</code>. If you want to move commits to an <strong>existing branch</strong> you need to merge your changes into the existing branch before executing <code>git reset --hard HEAD~3</code> (see <strong><em>Moving to an existing branch</em></strong> below). <strong>If you don't merge your changes first, they will be lost.</strong></p> <p>Unless there are other circumstances involved, this can be easily done by branching and rolling back.</p> <pre class="lang-sh prettyprint-override"><code># 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 (GONE from master) git checkout newbranch # Go to the new branch that still has the desired commits </code></pre> <p>But do make sure how many commits to go back. Alternatively, you can instead of <code>HEAD~3</code>, simply provide the hash of the commit (or the reference like <em>origin/master</em>) you want to "revert back to" on the <em>master</em> (/current) branch, e.g:</p> <pre class="lang-sh prettyprint-override"><code>git reset --hard a1b2c3d4 </code></pre> <p>*1 You will <strong>only</strong> be "losing" commits from the master branch, but don't worry, you'll have those commits in newbranch!</p> <p><strong>WARNING:</strong> With Git version 2.0 and later, if you later <code>git rebase</code> the new branch upon the original (<code>master</code>) branch, you may need an explicit <code>--no-fork-point</code> option during the rebase to avoid losing the carried-over commits. Having <code>branch.autosetuprebase always</code> set makes this more likely. See <a href="https://stackoverflow.com/a/36463546/1256452">John Mellor's answer</a> for details.</p> <h2>Moving to an existing branch</h2> <p>If you want to move your commits to an <strong>existing branch</strong>, it will look like this:</p> <pre class="lang-sh prettyprint-override"><code>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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload