2

I wanted to create a new branch from upstream. What I did are

$ git init
$ git clone https://github.com/nalam-nmef/LearnGitWithGithubFlow.git
$ git remote add upstream https://github.com/twentyTwo/LearnGitWithGithubFlow.git

then, to create a new branch from upstream

$ git checkout -b changes1 upstream/master

But in this point an error occurs. It says

fatal: Cannot update paths and switch to branch 'changes1' at the same time. Did you intend to checkout 'upstream/master' which can not be resolved as commit?

What I will do after that is pushing that new branch to my fork

$ git push -u origin changes1

That is, I always create a branch for a new change request locally. The branch is created from the upstream/master branch. Commit changes locally and push it to my fork. Then, create a pull request to the upstream master. But, I am getting the error in $ git checkout -b changes1 upstream/master

Whats the wrong with this? What is the best practice?

Noor A Shuvo
  • 2,211
  • 18
  • 35

2 Answers2

4

You need to fetch upstream first.

git fetch upstream
git checkout -b changes1 upstream/master

By default, only origin was cloned/fetched.
Type:

git remote -v

You will see your remotes.

Ideally, upstream URL should not be the same as the origin one (which should be your fork).
upstream should refer to the original repo URL that was forked.


Second, don't do:

git init
git clone ...

That would clone a git repo inside another git repo, which is why git remote -v only shows upstream.

Try instead:

cd /a/path
git clone https://github.com/twentyTwo/LearnGitWithGithubFlow.git
cd LearnGitWithGithubFlow
git checkout -b changes1

(no need for origin/master: by default, origin/master is checked out already)

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • `git remote -v` is not showing the origin. It shows only the upstream. Whats the wrong here? – Noor A Shuvo Aug 01 '18 at 04:49
  • `upstream https://github.com/twentyTwo/LearnGitWithGithubFlow.git (fetch) upstream https://github.com/twentyTwo/LearnGitWithGithubFlow.git (push) ` – Noor A Shuvo Aug 01 '18 at 04:51
  • @NoorAShuvo That is because of the init/clone steps: see my edited answer. – VonC Aug 01 '18 at 04:52
  • I am sorry. I need some things to clarify. I need to checkout the new branch `changes1` from upstream. To do this, I run `git checkout -b changes1 upstream/master`, but it shows the same error `fatal: Cannot update paths and switch to branch 'changes1' at the same time. Did you intend to checkout 'upstream/master' which can not be resolved as commit? ` – Noor A Shuvo Aug 01 '18 at 05:03
  • And another thing is, should I clone the repo from my origin or from upstream ? – Noor A Shuvo Aug 01 '18 at 05:04
  • @NoorAShuvo First, repeat the command I mention it will work. Second, upstream makes sense only if it refers an URL different from the repo you are cloning. – VonC Aug 01 '18 at 05:04
  • @NoorAShuvo By default, cloning a repo makes that repo origin. Clone your fork. go into that folder. Declare upstream there (pointing to the original repo) – VonC Aug 01 '18 at 05:05
  • Sorry again, I am cloning from `https://github.com/nalam-nmef/LearnGitWithGithubFlow.git` which is my forked repo. Updated the original question – Noor A Shuvo Aug 01 '18 at 05:08
  • Got it, thanks, Really appreciate it for your time. Thank again. – Noor A Shuvo Aug 01 '18 at 05:18
1

So, in brief, these are the steps if you want to create a new branch from an upstream branch

  • First fork the desired repo;
  • Clone your forked repo
  • cd in the repo
  • Add upstream
  • Fetch the upstream
git clone https://github.com/forkedRepo/demoRepo.git
cd demoRepo\
git remote add upstream https://github.com/originalRepo/demoRepo.git
git fetch upstream
  • create a new branch from upstream

git checkout -b newLocalBranchName upstream/fromUpstreamBranch

  • Push that new branch to my fork

git push -u origin newOriginBranchName

Noor A Shuvo
  • 2,211
  • 18
  • 35
  • 1
    Yes (+1). That is what I explained 8 years earlier with https://stackoverflow.com/a/9257901/6309 and https://stackoverflow.com/a/3903835/6309 and https://stackoverflow.com/a/6286877/6309 – VonC Aug 01 '18 at 11:25