typos
typos

Reputation: 6642

How to push to upstream in Git?

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.

Upvotes: 13

Views: 48705

Answers (1)

Scott Weldon
Scott Weldon

Reputation: 10227

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.

Upvotes: 20

Related Questions