Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push to upstream in Git?

Tags:

git

github

I have a project with a few friends in GitLab, and there is of course the master branch, and there are some others too. When I cloned the repository, I created also an upstream with the command git remote add upstream ....

Then, I issued the git fetch upstream. Followed by git checkout upstream/test1. Now, if I type git branch -a, I get an output like this:

* (HEAD detached at upstream/test1)
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/upstream/test1
  remotes/upstream/master

This is all fine, but then I did some changes to the code in my upstream/test1 branch. I don't want yet to push them to origin/test1, but I want to be able to push my changes to upstream/test1 so that my other friend can see it. But, if I issue the following set of commands:

git add .
git commit -m "Sample message"

After the commit I got the message:

[detached HEAD 4f20e95] Sample message
 5 files changed, 12 insertions(+), 1 deletions(-)

And the hash value changes to 4f20e95 in my command prompt. Then if I do git push, I get the following error messages:

fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
state now, use

    git push origin HEAD:<name-of-remote-branch>

How can I push to my upstream branch without actually pushing to origin.

like image 546
typos Avatar asked Oct 19 '16 20:10

typos


People also ask

What does push to upstream mean in git?

pushing " upstream " means that your current branch B has remote/B has its upstream branch. Ie: branch. B. merge is set, when your are pushing the " upstream " branch. Ie: when pulling to B , git knows what branch to pull (as well as which remote repo: branch.B.remote )

Does git push push to origin or upstream?

If you run the simple command git push , Git will by default choose two more parameters for you: the remote repository to push to and the branch to push. By default, Git chooses origin for the remote and your current branch as the branch to push.

How do I switch to upstream branch?

There are two ways to set an upstream branch in Git: Using git push , which is the fastest method if you need to set a single upstream branch. Using a short alias command. This method makes sense if you often change the flow of your current branch.


1 Answers

The branch upstream/test1 is a remote tracking branch, which can't be updated manually. Such branches track branches on remote servers, and are only updated during git fetch/git push.

Instead, check out a new local branch first:

git checkout -b test1 upstream/test1

And commit there as usual. Since you have already committed, instead do:

git checkout -b test1 4f20e95

When you are ready, push to upstream:

git push upstream test1

When you do a plain git push, Git uses default values for the remote and branch to push based on certain config options. However, if you aren't on a branch (thus getting the detached HEAD message), then Git doesn't know what branch to push, thus giving you the error you received.

like image 160
Scott Weldon Avatar answered Oct 27 '22 01:10

Scott Weldon