I have an account on github
and I use it from two different machines. On one, I created a new branch myNewBranch
and switched to it. Then I did my modifications to my code, I committed and pushed to myNewBranch
.
On the second machine, I can't figure out how to push to it.
$ git pull origin myNewBranch
From https://github.com/myUsername/myProject
* branch myNewBranch -> FETCH_HEAD
Already up-to-date.
[ I had already successfully pulled from it]
Then I try to switch to it, but I get an error:
$ git checkout myNewBranch
error: pathspec 'myNewBranch' did not match any file(s) known to git.
What am I missing?
Force a Checkout You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.
New Branches The git branch command can be used to create a new branch. When you want to start a new feature, you create a new branch off main using git branch new_branch . Once created you can then use git checkout new_branch to switch to that branch.
If you have a single remote repository, then you can omit all arguments. just need to run git fetch , which will retrieve all branches and updates, and after that, run git checkout <branch> which will create a local copy of the branch because all branches are already loaded in your system.
when you switch to a branch without committing changes in the old branch, git tries to merge the changes to the files in the new branch. If merging is done without any conflict, swithing branches will be successful and you can see the changes in the new branch.
You need to fetch the data onto your local repository on machine 2 first:
$ git fetch origin
$ git checkout origin/myNewBranch
My guess on what happened there is a remote origin/myNewBranch, but not a local branch myNewBranch. What your command did was to fetch origin/myNewBranch to your current local branch. When you did the git checkout myNewBranch
, the error happened because there was no local branch named myNewBranch. I suggest try git checkout -b myNewBranch origin/myNewBranch
.
Try doing git checkout origin/myNewBranch
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With