-1

This is a question that is not similar to existing Q&A that I've found. It is closely related to the git workflow that I'm following, which is

  • I do git updates in the default master branch, so that I can do git pull from time to time.
  • My updates are pushed to a remote branch for PR.

Now the situation is that I need a quick fix and need to create a git branch from off from the real master on the server, not the master of my local change. But I was unable to do that following the advices I found on the internet.

My current status:

. . . 
On branch master
Your branch is ahead of 'origin/master' by 52 commits.
. . . 

$ git checkout master
Already on 'master'
Your branch is ahead of 'origin/master' by 52 commits.

git checkout -b new-feature
Branch 'new-feature' set up to track local branch 'master' by rebasing.
Switched to a new branch 'new-feature'

$ git status
On branch new-feature
Your branch is up to date with 'master'.

The problem is that such "up to date with 'master'" is the master of my local change, not the real master on the server, as it is tracking "local branch 'master' ", but I want it to base and track "origin/master" instead.

xpt
  • 20,363
  • 37
  • 127
  • 216
  • If you want to update your local `master`, then `git pull`. If you don't want to do that, then you can `git fetch` and then check out `origin/master` under a different local branch name. – matt Dec 11 '20 at 17:29
  • @matt, I want to and need to update my local master with "git pull". How can I check out origin/master under a different branch name with such specific situation? – xpt Dec 11 '20 at 17:31
  • You're saying two opposite things. If you want to and need to `git pull`, then `git pull`. Now you will not be ahead of `origin/master` any more. I just don't understand what the problem _is_ here. – matt Dec 11 '20 at 17:32
  • Simply put, if I'm already ahead of origin/master, is there any way to create branches off from origin/master (and tracks it) instead of my local branch 'master' @matt? – xpt Dec 11 '20 at 17:35
  • Yes, as I said, you can just check out a new branch that tracks `origin/master`. For example, `git checkout -b hotfix --track origin/master`. – matt Dec 11 '20 at 17:38
  • Ah, that's the answer that I didn't know. thx @matt – xpt Dec 11 '20 at 17:40
  • But that is the _first_ thing that the [documentation](https://git-scm.com/docs/git-checkout) suggests. Perhaps that explains the downvotes. – matt Dec 11 '20 at 17:43
  • 1
    Does this answer your question? [How do I check out a remote Git branch?](https://stackoverflow.com/questions/1783405/how-do-i-check-out-a-remote-git-branch) – matt Dec 11 '20 at 17:45
  • The problem with user-manual without any example, is that it is hard for me to put it into practical use, just like this one. – xpt Dec 11 '20 at 17:46
  • *"Does this answer your question? How do I check out a remote Git branch?"* Not quite, he can see the remote branch but cannot check it out, much different than the situation that I'm having here, although speaking in theory it might be even the same thing. – xpt Dec 11 '20 at 17:50

1 Answers1

4

Just to clarify your problem to future readers...

You are working on the master branch which has a remote called origin/master.

You have made 52 commits to master and you are now ahead of origin/master.

You want to create a new branch, but you want to create the branch at the last commit where origin/master is.

To do this, you'll want to tell git to create a new branch at origin/master and then tell it to track origin/master

You can use:

git branch --track new-branch origin/master
Chad Baldwin
  • 2,239
  • 18
  • 32