I could use a hand with learning how to push a local branch to a remote branch. Please see below. Help much appreciated!
The local branch was created after cloning the repo then doing
$ git checkout -b mybranch remotes/origin/mybranch
$ git branch -a
master
* mybranch
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/mybranch
But when trying to push changes back up:
$ git push mybranch mybranch
fatal: 'mybranch' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
$ git push remotes/origin/mybranch mybranch
fatal: 'mybranch' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
$ git push origin/mybranch mybranch
fatal: 'mybranch' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
In the command line, navigate to the root directory of your project. Initialize the local directory as a Git repository. To create a repository for your project on GitHub, use the gh repo create subcommand. When prompted, select Push an existing local repository to GitHub and enter the desired name for your repository.
The git push command is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote repo. It's the counterpart to git fetch , but whereas fetching imports commits to local branches, pushing exports commits to remote branches.
Try
git push origin mybranch
This pushes your branch named mybranch to the remote named origin
As Abizern says, this works:
git push origin mybranch
But, to explain further, the mybranch part is a refspec. This specifies the remote ref which should be updated with the given local commit.
So, the command above is equivalent to:
git push origin mybranch:mybranch
or even:
git push origin mybranch:refs/heads/mybranch
and, indeed, since you're on the local mybranch, you could have done:
git push origin HEAD:mybranch
This is good to understand, because I often find myself doing things like:
git push origin HEAD^:mybranch
where you want to push all but the topmost patch to the remote branch.
Finally, if you want to delete the remote mybranch, you do:
git push origin :mybranch
This is an old question, but I used this page as a ref back in the day, and have an answer with a different perspective. From my experience, the best way is to tweak your config settings such that a git push
is all that you will have to enter in the end.
You will push to the same remote branch that you update your code from:
git config --global push.default upstream
And now, you set the remote branch as upstream (if it wasn't already):
git branch --set-upstream-to origin/the_master
git branch --set-upstream local_branch origin/the_master
You have two branches - a local and a remote. Doing a git pull
or git push
without args should, and will now, do what you want. This will not limit you to pushing to remote branches with the same name as the local one, as some of the above commands do.
One final thing I usually do: modify git pull
to change from this sequence:
git fetch && git merge [remote_upstream]
togit fetch && git rebase [remote_upstream]
With this command: git config --global branch.autosetuprebase remote
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