Moving git branch from one fork point to another

George Shuklin
OpsOps
Published in
1 min readFeb 24, 2024

ChatGPT start to scare me (in a good sense). It really knows git, and it knows it better than me. This solution is absolutely from it, I had had no idea that there is a fast way.

The problem:

There is master branch. There is branch feature1, containing some big changes. There is feature2 branch, containing independent changes. It was created by calling git checkout -b feature2, but it was created from feature1 branch, not from master. A silly mistake. We need to fix it.

Bad situation:

master:  A---B---C
\
feature1: D---E
\
feature2: F---G

Desired fix:

master:  A---B----C
\ |
feature1: D---E |
\
feature2: F---G

Or, slightly better view (the same tree):

feature2:           F---G
/
master: A---B----C
\
feature1: D---E

My old, inefficient approach was to redo feature2 branch by cherry-picking required commits.

Turned out, there is a command specifically for that

Solution

git checkout feature2
git rebase --onto master feature1 feature2

The problem with this command is not it’s complexity, but absolute (mine) inability to understand --onto before hitting the problem it solves.

I reread man page (here it is: https://git-scm.com/docs/git-rebase) and it didn’t helped me, even I knew what I’m searching for.

Fortunately, ChatGPT is better at reading man pages than me. There is a solution, and it’s simple.

--

--

George Shuklin
OpsOps

I work at Servers.com, most of my stories are about Ansible, Ceph, Python, Openstack and Linux. My hobby is Rust.