jurchiks
jurchiks

Reputation: 1404

git create new non-tracking branch based on remote branch

Situation: open-source project with multiple branches, and my fork of it for contributions. I want to create a new branch for pull requests on github. I want to base this branch on one of the existing branches on the open-source project, BUT I do NOT want my commits to go into that original branch.

As per my experience, if I clone my fork, execute git checkout -b my-branch origin/some-branch and then commit+push to my-branch, in Github the commits will go to some-branch and not my-branch. Obviously, this is problematic if I want to make multiple branches for multiple pull requests on the same branch.

The workaround that I've found for this is the following:

git checkout -b some-branch origin/some-branch

git checkout -b my-branch (new branch based on some-branch, but not tracking it)

after that - commit+push, first push will create my-branch on Github.

However, this leaves me with an extra local copy of some-branch. I can delete it later, obviously, but is there a way to avoid creating it at all? Is there a way to create my-branch, based on origin/some-branch, but not tracking it, in one command, without the extra branch in the middle?

Upvotes: 1

Views: 254

Answers (1)

SzG
SzG

Reputation: 12609

Use the SHA1 hash of the same commit instead of origin/some-branch. Example:

git checkout -b my-branch 92fc7a2

Remember, a branch is just a label on a commit, nothing more. It's a 41-byte file, containing a SHA1 hash name of a commit and a linefeed.

Upvotes: 2

Related Questions