2

I have made a commit with message "Recycler View in Main Activity". The Head was on it. I switched to a new branch without seeing the warning. I am now unable to see my commit with message "Recycler View in Main Activity". How can I retrieve a commit which is not associated with any branch? Please help I am very new to GitHub.

My Terminal is as follows:

C:\Users\Dell\AndroidStudioProjects\WorldClockTest>git add .

C:\Users\Dell\AndroidStudioProjects\WorldClockTest>git commit -m "Recycler View in Main Activity"
[detached HEAD 35b478b] Recycler View in Main Activity
 5 files changed, 96 insertions(+), 51 deletions(-)
 rewrite app/src/main/java/com/example/worldclocktest/CitySelectedListAdapter.java (60%)

C:\Users\Dell\AndroidStudioProjects\WorldClockTest>git log
commit 35b478bf8761480eeed417d3fa8ae3bab44cfa36 (HEAD)
Author: Hizafa-Nadeem <hizafa.977@gmail.com>
Date:   Thu May 6 04:44:13 2021 +0500

    Recycler View in Main Activity

commit 6dfc1a1db723e268d967ebcf34a8dac45f41514b (Recycler-View)
Author: Hizafa-Nadeem <hizafa.977@gmail.com>
Date:   Thu May 6 02:40:11 2021 +0500

C:\Users\Dell\AndroidStudioProjects\WorldClockTest>git push -u origin Recycler_View
error: src refspec Recycler_View does not match any
error: failed to push some refs to 'https://github.com/Hizafa-Nadeem/WorldClock-App-'

C:\Users\Dell\AndroidStudioProjects\WorldClockTest>git push -u origin Recycler-View
Enumerating objects: 115, done.
Counting objects: 100% (115/115), done.
Delta compression using up to 8 threads
Compressing objects: 100% (89/89), done.
Writing objects: 100% (115/115), 139.26 KiB | 1.00 MiB/s, done.
Total 115 (delta 19), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (19/19), done.
remote:
remote: Create a pull request for 'Recycler-View' on GitHub by visiting:
remote:      https://github.com/Hizafa-Nadeem/WorldClock-App-/pull/new/Recycler-View
remote:
To https://github.com/Hizafa-Nadeem/WorldClock-App-
 * [new branch]      Recycler-View -> Recycler-View
Branch 'Recycler-View' set up to track remote branch 'Recycler-View' from 'origin'.

C:\Users\Dell\AndroidStudioProjects\WorldClockTest>git checkout cities_declared_in_activity_main
error: pathspec 'cities_declared_in_activity_main' did not match any file(s) known to git

C:\Users\Dell\AndroidStudioProjects\WorldClockTest>git checkout cities_declared_in_activitymain
error: pathspec 'cities_declared_in_activitymain' did not match any file(s) known to git

C:\Users\Dell\AndroidStudioProjects\WorldClockTest>git log
commit 35b478bf8761480eeed417d3fa8ae3bab44cfa36 (HEAD)
Author: Hizafa-Nadeem <hizafa.977@gmail.com>
Date:   Thu May 6 04:44:13 2021 +0500

    Recycler View in Main Activity

commit 6dfc1a1db723e268d967ebcf34a8dac45f41514b (origin/Recycler-View, Recycler-View)
Author: Hizafa-Nadeem <hizafa.977@gmail.com>
Date:   Thu May 6 02:40:11 2021 +0500

    first commit on Recycler View

commit 269b5fee830e7a26a1fabd008f60ff6b236cb111 (cities_declared_in_main_activty)

C:\Users\Dell\AndroidStudioProjects\WorldClockTest>git checkout cities_declared_in_main_activty
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  35b478b Recycler View in Main Activity

If you want to keep it by creating a new branch, this may be a good time
to do so with:

 git branch <new-branch-name> 35b478b

Switched to branch 'cities_declared_in_main_activty'

C:\Users\Dell\AndroidStudioProjects\WorldClockTest>git status
On branch cities_declared_in_main_activty
nothing to commit, working tree clean

C:\Users\Dell\AndroidStudioProjects\WorldClockTest>git push -u origin cities_declared_in_main_activty

Nicolas
  • 6,611
  • 3
  • 29
  • 73
Hizafa
  • 81
  • 1
  • 8
  • On what branch were you originally? On was the HEAD detached? – Nicolas May 06 '21 at 00:49
  • See [Browse orphaned commits in Git](https://stackoverflow.com/q/2092810/). – jamesdlin May 06 '21 at 01:08
  • You don't seem to have much idea what you're doing with Git. That's unwise; you should learn more about it. However, the question is extremely well asked: you copied your history and pasted it into the question. I wish everyone would do that! – matt May 06 '21 at 01:37

2 Answers2

3

Thing to keep in mind: In Git, a branch is just a name for one commit. It has some other implications for how it behaves, but that is all it is. Branch names are crucial in Git for a number of reasons; one is that they keep commits alive. If a commit has a branch name, or is the parent of such a commit, or the parent of that commit..., the commit stays alive. If not, it can die. Luckily for you, it does not die quickly.

So here's a quick review of what you did and what it means!

% git add .
% git commit -m "Recycler View in Main Activity"
[detached HEAD 35b478b]

Detached head mode means there is no branch name for where you are: you are not "on a branch". Before the story even starts, you somehow got into detached head mode and made a commit. That is not illegal but it is extremely unwise. I have no idea how or why you did that to yourself.

% git log
commit 35b478bf8761480eeed417d3fa8ae3bab44cfa36 (HEAD)
commit 6dfc1a1db723e268d967ebcf34a8dac45f41514b (Recycler-View)

So it seems you have at least two commits on this parent chain: the detached one you just created, and the Recycler-View branch commit which precedes it.

% git push -u origin Recycler_View
error:

You got the branch name wrong. It was right in front of you! Next time, try copy-and-paste of the branch name. Even better, switch to a shell that gives you branch name autocompletion.

% git push -u origin Recycler-View

You got the branch name right, and pushed. But what you pushed is 6dfc1a1. You have not pushed the new commit 35b478b, which is still detached.

% git checkout cities_declared_in_activity_main
error:

No such branch.

% git checkout cities_declared_in_activitymain

Still no such branch. Why do you not even ask what the branch names are? That command is (wait for it) git branch!

% git log

Rather a silly way to find out the real branch name, but it did work.

% git checkout cities_declared_in_main_activty
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

Git is warning you as loudly as it can that you are about to lose all named access to commit 35b478. It is detached!

Okay, but you've done it. Is this a problem? No! Not immediately. Remember, commits do not die quickly. The commit is still there. Just get yourself all evened up and then say

% git checkout 35b478

Presto, your lost commit is back again! This time, do something reasonable with it. A detached commit can eventually be deleted, so if you don't want that to happen, rescue this commit! The simplest first step is, having checked out the commit, give it a branch name:

% git branch rescue

Now you can decide what to do with the commit that you've rescued. I don't know what your intentions were so I can't advise you about that.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Parents of tags don't die either, no? – Mad Physicist May 06 '21 at 02:58
  • Of course! But a detached commit even with a tag doesn’t put you “on a branch” so you can’t add and commit in the usual coherent way; you’re still detached. – matt May 06 '21 at 03:15
2

Your command line shows that you can create a branch for the commit with

git branch <new-branch-name> 35b478b

Here, 35b478b is the the commit ID. That means you can also switch to the new branch as you make it using

git checkout 35b478b -b <new-branch-name>

A headless checkout without a branch would be

git checkout 35b478b

You can see the commit with

git reflog

This lists the commits you've visited, not just the ones in your immediate history.

You may also be able to get it with

git checkout origin/Recycler-View
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264