user2250246
user2250246

Reputation: 3967

git create branch in my repo that is not spun off from existing branches

I have a repo that I forked from another repo.

Let us call my repo as origin and the parent repo as upstream

I regularly work in my origin repo's master branch and when ready, I submit the same via pull request to upstream.

Recently, I did a lot of work in my origin repo's master branch and created a PR to upstream but since the work was quite a bit, it ran into several issues related to conflicts, tests etc etc.

Now another important task came up that I need to work upon.

But when I try to create git checkout -b branch_for_new_work, it takes all the changes from the master branch of origin repo. How do I create a branch whose code-base aligns with the remote repo's master branch and not my origin repo's master branch? (I now know that its a good practice to create branches for such work and not pollute the master).

Upvotes: 1

Views: 41

Answers (1)

Sajib Khan
Sajib Khan

Reputation: 24156

How do I create a branch whose code-base aligns with the remote repo's master branch and not my origin repo's master branch?

$ git fetch upstream
$ git checkout -b <new-branch> upstream/master     # create a new branch with upstream/master history

If you want to create a Pull request where base: upstream/master and compare: origin/master then, always pull upstream/master into your origin/master to avoid Conflicts.


fatal: Cannot update paths and switch to branch 'new_branch' at the same time

Make sure you have fetched your remote (upstream here).


Alternate Solution: Create a new branch new_branch from your current branch then reset local new_branch with upstream/master

$ git remote -v              # make sure your remote name is 'upstream' or else

$ git fetch upstream
$ git checkout -b new_branch

$ git reset --hard upstream/master    

Upvotes: 1

Related Questions