âšď¸ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.flother.is/til/git-move-commit-to-branch/ |
| Last Crawled | 2026-04-11 22:22:51 (9 hours ago) |
| First Indexed | 2023-03-03 01:37:40 (3 years ago) |
| HTTP Status Code | 200 |
| Meta Title | How to move your most recent Git commit to a new branch â Flother |
| Meta Description | Flother is the online home of Matt Riggott, a programmer and map-maker living in ReykjavĂk, Iceland. |
| Meta Canonical | null |
| Boilerpipe Text | If youâre anything like me youâll have found yourself writing new code and accidentally committing it to the default branch (usually
master
or
main
). This might be alright for your personal repos, but when youâre working in a larger team and collaborating on a project using a branch-based workflow (like
GitHub flow
), then you need to move that commit to a new branch before you push the code.
One way to do this would be to âundoâ the commit: rewind your repository history so itâs as if that commit never happened, but with your local files still modified. However, if you do that, you lose the detailed commit message you spent ten minutes writing (you did write a good commit message, didnât you?). No, what you want to do is create a new branch that includes your latest commit, and then remove the commit from your default branch.
So what youâve done so far is commit your new code to the
master
branch by accident:
git
commit
#
Oops
To fix that, you should branch off from the tip of
master
and then move
master
back one commit:
git
switch
-
C
new-branch
git
switch master
git
reset
--
soft
HEAD^
That last line,
git reset --soft HEAD^
, will remove the last commit from
master
and the files you changed will still be modified but staged. (If you have more than one commit to remove, you can use
HEAD~3
instead of
HEAD^
, where
3
is the number of commits you want to remove. You actually have
loads of options available
.)
Now youâve cleaned that up, you can unstage the modified files and then push the new branch:
git
reset .
git
switch new-branch
git
push
-
u
origin new-branch
And now no-oneâs any the wiser. You have a clean
master
branch, a new branch with your new code, and youâre free to carry on
adding new commits to the branch
. |
| Markdown | [Flother](https://www.flother.is/)
# How to move your most recent Git commit to a new branch
Today I learnt âŚ
2 March 2023
If youâre anything like me youâll have found yourself writing new code and accidentally committing it to the default branch (usually `master` or `main`). This might be alright for your personal repos, but when youâre working in a larger team and collaborating on a project using a branch-based workflow (like [GitHub flow](https://docs.github.com/en/get-started/quickstart/github-flow)), then you need to move that commit to a new branch before you push the code.
One way to do this would be to âundoâ the commit: rewind your repository history so itâs as if that commit never happened, but with your local files still modified. However, if you do that, you lose the detailed commit message you spent ten minutes writing (you did write a good commit message, didnât you?). No, what you want to do is create a new branch that includes your latest commit, and then remove the commit from your default branch.
So what youâve done so far is commit your new code to the `master` branch by accident:
```
git commit # Oops
```
To fix that, you should branch off from the tip of `master` and then move `master` back one commit:
```
git switch -C new-branch
git switch master
git reset --soft HEAD^
```
That last line, `git reset --soft HEAD^`, will remove the last commit from `master` and the files you changed will still be modified but staged. (If you have more than one commit to remove, you can use `HEAD~3` instead of `HEAD^`, where `3` is the number of commits you want to remove. You actually have [loads of options available](https://git-scm.com/docs/git-rev-parse#_specifying_revisions).)
Now youâve cleaned that up, you can unstage the modified files and then push the new branch:
```
git reset .
git switch new-branch
git push -u origin new-branch
```
And now no-oneâs any the wiser. You have a clean `master` branch, a new branch with your new code, and youâre free to carry on [adding new commits to the branch](https://xkcd.com/1296/).
- [Git](https://www.flother.is/tags/git/ "See all pages tagged Git")
- [Home](https://www.flother.is/)
- [Blog](https://www.flother.is/blog/)
- [Today I Learnt](https://www.flother.is/til/)
- [Travelogue](https://www.flother.is/travelogue/)
- [Podcast](https://www.flother.is/podcast/)
- [Categories](https://www.flother.is/categories/)
- [Tags](https://www.flother.is/tags/)
- [GitHub](https://github.com/flother)
- [OpenStreetMap](https://www.openstreetmap.org/user/flother)
- [Flickr](https://www.flickr.com/photos/riggott)
Š 2023 |
| Readable Markdown | If youâre anything like me youâll have found yourself writing new code and accidentally committing it to the default branch (usually `master` or `main`). This might be alright for your personal repos, but when youâre working in a larger team and collaborating on a project using a branch-based workflow (like [GitHub flow](https://docs.github.com/en/get-started/quickstart/github-flow)), then you need to move that commit to a new branch before you push the code.
One way to do this would be to âundoâ the commit: rewind your repository history so itâs as if that commit never happened, but with your local files still modified. However, if you do that, you lose the detailed commit message you spent ten minutes writing (you did write a good commit message, didnât you?). No, what you want to do is create a new branch that includes your latest commit, and then remove the commit from your default branch.
So what youâve done so far is commit your new code to the `master` branch by accident:
```
git commit # Oops
```
To fix that, you should branch off from the tip of `master` and then move `master` back one commit:
```
git switch -C new-branch
git switch master
git reset --soft HEAD^
```
That last line, `git reset --soft HEAD^`, will remove the last commit from `master` and the files you changed will still be modified but staged. (If you have more than one commit to remove, you can use `HEAD~3` instead of `HEAD^`, where `3` is the number of commits you want to remove. You actually have [loads of options available](https://git-scm.com/docs/git-rev-parse#_specifying_revisions).)
Now youâve cleaned that up, you can unstage the modified files and then push the new branch:
```
git reset .
git switch new-branch
git push -u origin new-branch
```
And now no-oneâs any the wiser. You have a clean `master` branch, a new branch with your new code, and youâre free to carry on [adding new commits to the branch](https://xkcd.com/1296/). |
| Shard | 20 (laksa) |
| Root Hash | 2267687846180591220 |
| Unparsed URL | is,flother!www,/til/git-move-commit-to-branch/ s443 |