0

I have a local master modified something.I want to clone origin master as a new local branch.I tried some way as below.But I found there are some different ways between master and the new branch.I don't know why this happend.And how can I clone origin master as a new local branch totally same.

  1. git fetch origin master:newMaster
  2. git checkout -b newMaster origin:master
Schwern
  • 153,029
  • 25
  • 195
  • 336
zyMacro
  • 675
  • 7
  • 14
  • How did you check for differences between master and the new branch? Are you checking `master` or `origin/master`? – Schwern Oct 29 '19 at 02:50
  • just [rename](https://stackoverflow.com/a/6591218/4648586) your local master to something else then pull the `origin/master`. – Bagus Tesa Oct 29 '19 at 02:51
  • I can ran the remote master successfully , but I failed to ran local branch. – zyMacro Oct 29 '19 at 02:54

1 Answers1

4
  1. git fetch origin
  2. git checkout -b newMaster origin/master

origin is the name of the remote repository you clone from. origin/master is what is known as a "remote tracking branch". It is how your local repository tracks the master branch on the origin repository.

git fetch origin updates your view of the remote repository by pulling down new commits and updating your remote tracking branches (ie. origin/master). Then you can simply make a branch off origin/master like any other branch.

See Working with Remotes in the Git Book for more.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • Sorry, I don't understand.Why can't I just run the second line – zyMacro Oct 29 '19 at 03:00
  • @zyMacro You could, but then your local `origin/master` might be out of date. `git fetch` is safe, it only affects remote tracking branches. – Schwern Oct 29 '19 at 03:02
  • 1
    @zyMacro Your step 1 is fine. `git fetch origin master:newMaster` does what you want. It fetches `master` from `origin` and creates a new local branch called `newMaster` at `origin/master`. Done, you don't need your step 2. Your step 2, `git checkout -b newMaster origin:master`, is incorrect and should have resulted in an error like `fatal: 'origin:master' is not a commit and a branch 'newMaster' cannot be created from it`. `git checkout` takes a branch like `origin/master`. See https://git-scm.com/book/en/v2/Git-Internals-The-Refspec for more about refspecs. – Schwern Oct 29 '19 at 03:36
  • But in first way, i found that there some some differences between my new local branch with origin master.My local branch had some changes inherited by my old local master. – zyMacro Oct 29 '19 at 05:32
  • That shouldn't be. How are you checking this diff? `git diff newMaster origin/master`? Can edit your answer with the output of `git log --graph --decorate --all --oneline` showing newMaster, master, and origin/master? – Schwern Oct 29 '19 at 06:01
  • In the new branch, I find some code based on old local master uncommited.So I think the new branch is different from the origin master. – zyMacro Oct 29 '19 at 07:00
  • @zyMacro To help you further I would need to see the information I've requested. Please [edit it into your question](https://stackoverflow.com/posts/58600835/edit). – Schwern Oct 29 '19 at 19:17