mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 08:08:02 +00:00
Add Move The Latest Commit To A New Branch as a git til.
This commit is contained in:
18
git/move-the-latest-commit-to-a-new-branch.md
Normal file
18
git/move-the-latest-commit-to-a-new-branch.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# Move The Latest Commit To A New Branch
|
||||
|
||||
I sometimes find myself making a commit against the `master` branch that I
|
||||
intended to make on a new branch. To get this commit on a new branch, one
|
||||
possible approach is to do a reset, checkout a new branch, and then
|
||||
re-commit it. There is a better way.
|
||||
|
||||
```bash
|
||||
$ git checkout -b my-new-branch
|
||||
$ git checkout -
|
||||
$ git reset --hard HEAD~
|
||||
```
|
||||
|
||||
This makes better use of branches and avoids the need to redo a commit that
|
||||
has already been made.
|
||||
|
||||
Note: The example was against the `master` branch, but can work for any
|
||||
branch.
|
||||
Reference in New Issue
Block a user