|
|
## Pushing to remotes
|
|
|
|
|
|
* **Do not rebase previousIy pushed work**, unless you are willing to basically force every other clone to reclone or perform a great deal of complicated work with git.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Move recent commits to new branch
|
|
|
|
|
|
``` shell
|
|
|
git reset --keep HEAD~3
|
|
|
git checkout -t -b newbranch
|
|
|
git cherry-pick ..HEAD@{2}
|
|
|
```
|
|
|
First it discards the 3 most recent commits (--keep is like --hard, but safer, as fails rather than throw away uncommitted changes).
|
|
|
Then it forks off newbranch.
|
|
|
Then it cherry-picks those 3 commits back onto newbranch. Since they're no longer referenced by a branch, it does that by using git's reflog: HEAD@{2} is the commit that HEAD used to refer to 2 operations ago, i.e. before we 1. checked out newbranch and 2. used git reset to discard the 3 commits.
|
|
|
|
|
|
Warning: the reflog is enabled by default, but if you've manually disabled it (e.g. by using a "bare" git repository), you won't be able to get the 3 commits back after running git reset --keep HEAD~3.
|
|
|
|
|
|
An alternative that doesn't rely on the reflog is:
|
|
|
|
|
|
``` shell
|
|
|
# newbranch will omit the 3 most recent commits.
|
|
|
git checkout -b newbranch HEAD~3
|
|
|
git branch --set-upstream-to=oldbranch
|
|
|
# Cherry-picks the extra commits from oldbranch.
|
|
|
git cherry-pick ..oldbranch
|
|
|
# Discards the 3 most recent commits from oldbranch.
|
|
|
git branch --force oldbranch oldbranch~3
|
|
|
``` |
|
|
\ No newline at end of file |