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
The rebase solution proposed in the other answer is the most straightforward way (and the one I'd use as well). Just to provide an alternative, you can also use cherry-pick. First, let's create b...
#2: Post edited
- The `rebase` solution proposed in the [other answer](/posts/295739/295740#answer-295740) is the most straightforward way (and the one I'd use as well).
- Just to provide an alternative, you can also use [`cherry-pick`](https://git-scm.com/docs/git-cherry-pick).
First, let's create branch feature-2 from main, and switch to it:- ```bash
- git switch --create feature-2 main
- ```
- Now we have:
- ```none
- --A--B--C main, feature-2 (HEAD)
- \
- `Q--R--S--X--Y--Z feature-1
- ```
And feature-2 is the current branch. Then we cherry-pick all commits from X to Z:- ```bash
- git cherry-pick X^..Z
- ```
Obviously, replacing X and Z by their respective hashes. Note that after X there's a `^`, so `X^..Z` corresponds to all commits reachable from Z but not reachable from X's parent (in this case, it's X, Y and Z).This will apply commits X, Y and Z to feature-2, resulting in:- ```none
- --A--B--C main
- |\
- | `Q--R--S--X--Y--Z feature-1
- \
- `X'-Y'-Z' feature-2
- ```
Finally, we just move feature-1 to S (this part is the same as the other answer):- ```bash
- git switch feature-1
- git reset --hard S
- ```
- ```none
- --A--B--C main
- |\
- | `Q--R--S feature-1
- \
- `X'-Y'-Z' feature-2
- ```
- ---
- Alternatively, you can also list the commits one by one, like `git cherry-pick X Y Z`. But if there are too many commits, this can be tedious and very error-prone, and the two-dots syntax is a better solution.
- ---
As I said, using rebase is more straightforward, I'd just wanted to show an alternative. After all, [you can think of rebase as a faster way to cherry-pick a specific set of commits](https://think-like-a-git.net/sections/rebase-from-the-ground-up/using-git-cherry-pick-to-simulate-git-rebase.html).
- The `rebase` solution proposed in the [other answer](/posts/295739/295740#answer-295740) is the most straightforward way (and the one I'd use as well).
- Just to provide an alternative, you can also use [`cherry-pick`](https://git-scm.com/docs/git-cherry-pick).
- First, let's create branch `feature-2` from `main`, and switch to it:
- ```bash
- git switch --create feature-2 main
- ```
- Now we have:
- ```none
- --A--B--C main, feature-2 (HEAD)
- \
- `Q--R--S--X--Y--Z feature-1
- ```
- And `feature-2` is the current branch. Then we `cherry-pick` all commits from X to Z:
- ```bash
- git cherry-pick X^..Z
- ```
- Obviously, replacing X and Z by their respective hashes. Note that after X there's a `^`, so `X^..Z` corresponds to all commits reachable from Z but not reachable from X's parent (in this case, it'll be commits X, Y and Z).
- This will apply commits X, Y and Z to `feature-2`, resulting in:
- ```none
- --A--B--C main
- |\
- | `Q--R--S--X--Y--Z feature-1
- \
- `X'-Y'-Z' feature-2
- ```
- Finally, we just move `feature-1` to S (this part is the same as the other answer):
- ```bash
- git switch feature-1
- git reset --hard S
- ```
- And the repository will look like this:
- ```none
- --A--B--C main
- |\
- | `Q--R--S feature-1
- \
- `X'-Y'-Z' feature-2
- ```
- ---
- Alternatively, you can also list the commits one by one, like `git cherry-pick X Y Z`. But if there are too many commits, this can be tedious and very error-prone, and the two-dots syntax is a better solution.
- ---
- As I said, using `rebase` is more straightforward, I'd just wanted to show an alternative. After all, [you can think of `rebase` as a faster way to `cherry-pick` a specific set of commits](https://think-like-a-git.net/sections/rebase-from-the-ground-up/using-git-cherry-pick-to-simulate-git-rebase.html).
#1: Initial revision
The `rebase` solution proposed in the [other answer](/posts/295739/295740#answer-295740) is the most straightforward way (and the one I'd use as well).
Just to provide an alternative, you can also use [`cherry-pick`](https://git-scm.com/docs/git-cherry-pick).
First, let's create branch feature-2 from main, and switch to it:
```bash
git switch --create feature-2 main
```
Now we have:
```none
--A--B--C main, feature-2 (HEAD)
\
`Q--R--S--X--Y--Z feature-1
```
And feature-2 is the current branch. Then we cherry-pick all commits from X to Z:
```bash
git cherry-pick X^..Z
```
Obviously, replacing X and Z by their respective hashes. Note that after X there's a `^`, so `X^..Z` corresponds to all commits reachable from Z but not reachable from X's parent (in this case, it's X, Y and Z).
This will apply commits X, Y and Z to feature-2, resulting in:
```none
--A--B--C main
|\
| `Q--R--S--X--Y--Z feature-1
\
`X'-Y'-Z' feature-2
```
Finally, we just move feature-1 to S (this part is the same as the other answer):
```bash
git switch feature-1
git reset --hard S
```
```none
--A--B--C main
|\
| `Q--R--S feature-1
\
`X'-Y'-Z' feature-2
```
---
Alternatively, you can also list the commits one by one, like `git cherry-pick X Y Z`. But if there are too many commits, this can be tedious and very error-prone, and the two-dots syntax is a better solution.
---
As I said, using rebase is more straightforward, I'd just wanted to show an alternative. After all, [you can think of rebase as a faster way to cherry-pick a specific set of commits](https://think-like-a-git.net/sections/rebase-from-the-ground-up/using-git-cherry-pick-to-simulate-git-rebase.html).
