Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="https://stackoverflow.com/questions/6397898/create-branch-with-n-last-commits/6397932#6397932">knittl's answer</a> works, but there are other ways to do this.</p> <p>I'm assuming you're on the <code>master</code> when you first cloned this repository the master branch will match the master branch on the repository you've cloned from. This is <code>origin/master</code>. Since your question starts with a <code>clone</code> this is a fair assumption.</p> <p>So, after you've made your commits on the master branch, you are now ahead of the <code>origin/master</code> branch.</p> <p>The first thing you do is create a new branch</p> <pre><code>git branch new_branch </code></pre> <p>Note: this command just creates a new branch but doesn't switch branches. So <code>master</code> and <code>new_branch</code> now point to the same commit, but you are still on the <code>master</code> branch.</p> <p>Next thing to do is to set the current branch (which is <code>master</code>) to the state it was before you added commits. This is the same state as <code>origin/master</code> so you issue this command</p> <pre><code>git reset --hard origin/master </code></pre> <p>This sets the current branch to the same state as <code>origin/master</code>. The <code>--hard</code> sets the index and the working tree to the initial state. There are <a href="http://git-scm.com/docs/git-reset" rel="nofollow noreferrer">other flags</a>, but they don't do what you want here. (<strong>Attention</strong>: If you had uncommitted changes in your working tree, they are now thrown away. Use <code>git stash</code> in this case before the reset.)</p> <p>So now you're on <code>master</code> which points the same state as <code>origin/master</code>, all you need to do is switch to the new branch:</p> <pre><code>git checkout new_branch </code></pre> <p>Yes, this is a bit longer (3 commands instead of 2), but you don't have to count how many commits you've got to go back, and this will work even if you've branched, merged, and rebased; and I get to explain other ways of doing things in Git.</p>
 

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