Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
In the most general case[1] that you're asking about, you want the three-term rebase --onto. First, make your feature-2 branch: git switch --create feature-2 --A--B--C main \ ...
#2: Post edited
- In the most general case[^cheats] that you're asking about, you want the three-term `rebase --onto`.
- First, make your `feature-2` branch:
- ```sh
- git switch --create feature-2
- ```
- ```text
- --A--B--C main
- \
- `Q--R--S--X--Y--Z feature-1, feature-2
- ```
- Next, move `feature-2`'s commits. Notice the `^` that I use to mark the *parent* of the last commit I want to move.
- ```sh
- git rebase --onto main feature-2 X^
- ```
- ```text
- --A--B--C main
- |\
- | `Q--R--S--X--Y--Z feature-1
- \
- `X'-Y'-Z' feature-2
- ```
- Finally, move `feature-1` back where it belongs:
- ```sh
- git switch feature-1
- git reset --hard S
- ```
- ```text
- --A--B--C main
- |\
- | `Q--R--S feature-1
- \
- `X'-Y'-Z' feature-2
- ```
- The [Git rebase documentation][docs] for the three-term `--onto` starts in a position where `feature-1` is already where it needs to be.
- For whatever reason, I'm much more likely to still be on `feature-1` and realize that I've started making a second feature. For that I'd rather use the `^` on the last commit I need to move rather than trace the lines of a `git log --graph` down to the parent commit.
[^cheats]: There are some tricks in simplified cases involving cherry picking instead of a rebase.- [docs]: https://git-scm.com/docs/git-rebase
- In the most general case[^cheats] that you're asking about, you want the three-term `rebase --onto`.
- First, make your `feature-2` branch:
- ```sh
- git switch --create feature-2
- ```
- ```text
- --A--B--C main
- \
- `Q--R--S--X--Y--Z feature-1, feature-2
- ```
- Next, move `feature-2`'s commits. Notice the `^` that I use to mark the *parent* of the last commit I want to move.
- ```sh
- git rebase --onto main feature-2 X^
- ```
- ```text
- --A--B--C main
- |\
- | `Q--R--S--X--Y--Z feature-1
- \
- `X'-Y'-Z' feature-2
- ```
- Finally, move `feature-1` back where it belongs:
- ```sh
- git switch feature-1
- git reset --hard S
- ```
- ```text
- --A--B--C main
- |\
- | `Q--R--S feature-1
- \
- `X'-Y'-Z' feature-2
- ```
- The [Git rebase documentation][docs] for the three-term `--onto` starts in a position where `feature-1` is already where it needs to be.
- For whatever reason, I'm much more likely to still be on `feature-1` and realize that I've started making a second feature. For that I'd rather use the `^` on the last commit I need to move rather than trace the lines of a `git log --graph` down to the parent commit.
- [^cheats]: There are some tricks in simplified cases involving cherry picking instead of a rebase. See [hkotsubo's excellent range cherry-pick answer](https://software.codidact.com/posts/295739/295751#answer-295751) for more.
- [docs]: https://git-scm.com/docs/git-rebase
#1: Initial revision
In the most general case[^cheats] that you're asking about, you want the three-term `rebase --onto`.
First, make your `feature-2` branch:
```sh
git switch --create feature-2
```
```text
--A--B--C main
\
`Q--R--S--X--Y--Z feature-1, feature-2
```
Next, move `feature-2`'s commits. Notice the `^` that I use to mark the *parent* of the last commit I want to move.
```sh
git rebase --onto main feature-2 X^
```
```text
--A--B--C main
|\
| `Q--R--S--X--Y--Z feature-1
\
`X'-Y'-Z' feature-2
```
Finally, move `feature-1` back where it belongs:
```sh
git switch feature-1
git reset --hard S
```
```text
--A--B--C main
|\
| `Q--R--S feature-1
\
`X'-Y'-Z' feature-2
```
The [Git rebase documentation][docs] for the three-term `--onto` starts in a position where `feature-1` is already where it needs to be.
For whatever reason, I'm much more likely to still be on `feature-1` and realize that I've started making a second feature. For that I'd rather use the `^` on the last commit I need to move rather than trace the lines of a `git log --graph` down to the parent commit.
[^cheats]: There are some tricks in simplified cases involving cherry picking instead of a rebase.
[docs]: https://git-scm.com/docs/git-rebase
