diff --git a/README.md b/README.md index b74de45..6304741 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,7 @@ smart people at [Hashrocket](http://hashrocket.com/). - [Last Commit A File Appeared In](git/last-commit-a-file-appeared-in.md) - [List Filenames Without The Diffs](git/list-filenames-without-the-diffs.md) - [List Untracked Files](git/list-untracked-files.md) +- [Move The Latest Commit To A New Branch](git/move-the-latest-commit-to-a-new-branch.md) - [Renaming A Branch](git/renaming-a-branch.md) - [Single Key Presses in Interactive Mode](git/single-key-presses-in-interactive-mode.md) - [Staging Changes Within Vim](git/staging-changes-within-vim.md) diff --git a/git/move-the-latest-commit-to-a-new-branch.md b/git/move-the-latest-commit-to-a-new-branch.md new file mode 100644 index 0000000..833cc56 --- /dev/null +++ b/git/move-the-latest-commit-to-a-new-branch.md @@ -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.