10

I am working with three forked versions of a source code. FYI I changed the names to not disclose identities/repos. Here is how it is laid out:

MyMBP:~/Documents/_library$ git remote -v

abc https://github.com/abc/_library (fetch)
abc https://github.com/abc/_library (push)
origin  https://github.com/123/_library.git (fetch)
origin  https://github.com/123/_library.git (push)
upstream    https://github.com/source/_library (fetch)
upstream    https://github.com/source/_library (push)

Here, upstream is the original source code that is the most up to date and stable version. Origin is my forked version. ABC is someone else's version.

ABC had a branch that I pulled from to work off of. I wanted to then make changes and push up to my repo (origin), and subsequently submit a pull request. The branch on ABC, was called "abc/obs-noise".

Upon git status:

git status
HEAD detached from abc/obs-noise

When I made the changes to said file, I committed:

git commit
[detached HEAD e8eeea3] OBSERVATION NOISE ADDITION TO THE MONITORS
 1 file changed, 23 insertions(+), 11 deletions(-)

Then I git pushed (supposedly to my origin).

git push -u origin abc/obs-noise
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 733 bytes | 733.00 KiB/s, done.
Total 5 (delta 4), reused 1 (delta 0)
remote: Resolving deltas: 100% (4/4), completed with 4 local objects.
To https://github.com/123/_library.git
 * [new branch]      abc/obs-noise -> abc/obs-noise

However, I go to my repo... no abc/obs-noise, or any evidence of this commit. I am a noob at git, but I am almost sure I did everything right and it looks that way. I am wondering where do I find my commit? I would hate to redo all my work.

ajl123
  • 1,172
  • 5
  • 17
  • 40
  • Try `git checkout -b newbranch e8eeea3`. Is everything there? – Christoph Mar 01 '18 at 17:42
  • haha... yes! omg thank you. I see where I should've seen that now actually. It's unfortunate though. I'm not sure why this isn't pushed up to the remote, but only locally? – ajl123 Mar 01 '18 at 17:59

2 Answers2

9

First of all, you are sitting on a detached HEAD. It means that any commit that you have just made will not move the original branch pointer. So you have great chances to lose these commits if you change active branches, and that's why git push doesn't push anything new: original branch pointer hasn't been moved.

First of all, when you start working on any of remote branch you should create a local branch, corresponding to a given remote branch. Use

git checkout -b <local_branch_name> <remote>/<branch>

Then as you make commits, this <local_branch_name> will advance to follow your commits line. When you need to push a given commit line to a remote branch, use

git push <remote> <local_branch_name>:<remote_branch_name>

If you need to have several development lines for different remotes, you should created several corresponding local branches and push them separately.

user3159253
  • 16,836
  • 3
  • 30
  • 56
4

You can't push from detached HEAD. The only way is

git checkout -b newbranch e8eeea3
git push origin origin_branch_name

For further details see here

Edit after input from @Torek: There might be uses cases, where you want to push a detached head. For details see here. See comment below.

Christoph
  • 6,841
  • 4
  • 37
  • 89
  • 2
    You *can* push from a detached HEAD, but that's kind of advanced Git usage. Better to say "don't do it, it's too confusing" :-) – torek Mar 01 '18 at 18:28
  • That's one example, yes. – torek Mar 01 '18 at 19:17
  • @Torek is there a use case where you should/could push a detached Head? – Christoph Mar 02 '18 at 05:57
  • 1
    [Yes, but it's pretty unusual (and often there's something better, or at least more comprehensible).](https://stackoverflow.com/a/48969434/1256452) – torek Mar 02 '18 at 06:35