0

So I'm fairly new to git, but I have basic knowledge of how it operates and I have been playing around with the Android source recently (more specifically LineageOS, but it doesn't really matter for my question).

Basically, I followed the Android and Lineage guides to setting up a build environment with Linux (Xubuntu 16.04 LTS), downloaded the source, got my phone's proprietary blobs and successfully built it. Next I began cherry-picking some features.

According to the Google documentation for AOSP, I should move to a project folder and do a "repo start BRANCH_NAME ." to create a topic branch, then make my changes, git add them and commit them. The thing is, I don't want to push these changes to anywhere. I am simply wanting to work locally, only pulling new changes while keeping my cherry picks. So I did a bunch of cherry picks and ran a build again, which was successful but one of the things I cherry picked is causing me an issue and I want to completely revert it.

I first did repo sync and moved to a project folder, when I did "git branch", it returned "* (no branch)" in green, and when I did "git status", it told me that I'm not currently on a branch and that the working directory is clean.

Now, since I created a topic branch in one of these projects and cherry picked a couple commits, git status tells me I am 2 commit aheads, so to remove them, I did "git reset --hard github/cm-14.1", which completed and I verified that the files were reverted back to their original state. I ran "repo prune" to get rid of the now unneeded topic branch, and when I run "git branch" again, it says "* (HEAD detached at 12f0903)" in green and when I run run "git status" it also says HEAD is detached at 12f0903 in red, but also tells me that there is nothing to commit and that my working directory is clean.

Finally, after all of that, if I just create a new branch now, status and branch return clean, normal output with no detached head message. Is there a way to get back to the very beginning state of not being in a branch and not having a detached head or am I totally confused and doing something wrong?

PloobOobl
  • 3
  • 2
  • Being on no branch and being on detached HEAD are technically the same. It's okay as long as you are on the right commit. – ElpieKay May 13 '17 at 03:42

1 Answers1

2

Is there a way to get back to the very beginning state of not being in a branch and not having a detached head

Simply checkout the SHA1 referenced by your current branch:

git checkout $(git rev-parse HEAD)

As already stated, being in a detached HEAD mode is not a big deal, unless you want to add new commit (in which case, creating a dedicated branch that you would push to a fork is a good idea)

The Android Lineage guide mention the command

repo init -u https://github.com/LineageOS/android.git -b cm-14.1

That means you can go back to the local branch cm-14.1 with

git reset --hard cm-14.1
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This command is executes fine but my status and branch commands still say that my head is detached unless I create a new branch. – PloobOobl May 13 '17 at 06:05
  • @PloobOobl the beginning state would be in a detached head as you checkout a commit. You need to checkout a branch to avoid a detached head – VonC May 13 '17 at 06:07
  • That's not the official Lineage page and that guide is not exactly the same was what is on the official [Lineage Wiki](https://wiki.lineageos.org/devices/shamu/build#put-the-bin-directory-in-your-path-of-execution) and I already repo init before my first sync, so I don't see how it's related to my question. – PloobOobl May 13 '17 at 06:18
  • @PloobOobl Could you do a git branch in your repo then? a git checkout will avoid the detached head – VonC May 13 '17 at 06:20
  • I understand that checking out a local branch will avoid it, but that's my entire question. I just want to know if it's possible to go back to the very initial state where it told me I was not in any branch and the head was NOT detached. – PloobOobl May 13 '17 at 06:28
  • @PloobOobl if you are not in any branch, then the HEAD *is* detached: that is what a detached HEAD is. – VonC May 13 '17 at 06:29
  • which makes sense to me, except for the fact that when I freshly download the entire Lineage or AOSP source with repo sync, when I go to a project folder and do branch or status, it doesn't say the head is detached, it just says that I'm not in a branch. This is what I don't understand. – PloobOobl May 13 '17 at 06:31
  • Can you copy the exact git status you saw at the beginning? – VonC May 13 '17 at 06:32
  • I went into a project within the source that I have not touched and ran "git status", which gave me this result: "Not currently on any branch. nothing to commit, working directory clean.", where the not in branch message is red. I then ran "git branch" and got: "* (no branch)" in green. – PloobOobl May 13 '17 at 06:35