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?
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
Related
So I have an android studio project that I'm syncing between my laptop and my computer with git. Every time I push with one and pull with the other when I try to pull I get error refusing to merge unrelated histories
I tried using --allow-unrelated-histories but that causes a ton of merge conflicts.
I need to be able to sync between the two because my computer supports the emulator and my laptop is portable.
This happens because your repositories was initialized independly.
You should create the repository only in one location, and clone it to the other.
If the other repository already exists and you have there some change which you don't want to lose, you could do the following:
(from location2, commit all uncommitted changes first!)
git fetch location1
git branch save_location2
git reset --hard origin/location1
So you switch to the history started at location1, and would not lose your history started at location2, and will be able to look up stuff from there.
There are exceptional cases where you should use --allow-unrelated-histories, but I'm sure it is not your case.
I had same problem. Try this:
git pull origin master --allow-unrelated-histories
git push origin master
Reference:- Github unrelated histories issue
Something very weird is happening.
I am on the master branch of my repository. After calling git pull I get the latest version of the code on the master branch, everything is ok.
But for some reason, Android Studio, after some seconds, changes the code, so that now, even if I do git pull I get Already up-to-date but if I call git status I see changes not staged in red. How is that possible?
Also, if I click cmd+z I can see for few seconds how the code added by Android Studio goes away, and immediately comes back.
I tried deleting the entire project and cloning again from the repository, and again I get the latest version of the fresh code, but after few seconds Android Studio changes it.
All that happens in a particular file and section of code.
When you make git pull it doesn't reset or throw the changes you have non staged, you have to commit them the only reason you would see a problem with these files were is there was a conflict and git would notify you and tell you you need to commit before the pull.
so if you want to commit these change you need to do:
git add .
git commit
or if you want to throw away these changes you need to do:
git reset --hard HEAD
that last command would let you with the exact same code as in the remote repository.
That's happening because each time you opening a project with Android Studio, it will check for Android Studio project configuration files. When Android Studio did not find any of these files, it will create them. The configuration files will be in the .idea folder. Therefore, you will see a red mark for those unstaged files.
I want to discard the local changes I made in an Android Studio project.
I tried to perform a pull, the GUI gives me 5 options, which option should I choose?
Octopus
Ours
Subtree
Recursive
Resolve
In Android Studio do the following:
-Open the Version Control tool window Alt+9 and switch to the Log tab.
-Select the recent commit and right click on it than select Reset Current Branch to Here.
-A Git Reset popup will open -> select Hard -> click Reset
You originally asked which strategy argument to use with git pull to discard your own work (there is a pending edit that will change the question, if the edit is approved). The answer is: None.
Don't use git pull at all. Run git fetch first:
git fetch origin
This brings over all the new stuff from the other Git you have your Git calling "origin".
Now that you have everything they have, simply stop using what you have been using, and switch to theirs:
git reset --hard origin/master # assuming you're on your "master"
You may also want to use git clean -fdx to remove build artifacts, but that's a separate issue.
In Android studio do the following:
Go to VCS -> Git -> Reset HEAD
Change Reset type to hard
Let's say I'm building Android or CyanogenMod from source and want to make changes to its source. Also, let's assume I don't want to submit these changes (since they are incomplete or are changes that have already been rejected, for example).
What is the best way to manage that? How can I have proper source control of my "personal" changes, but at the same time be able to use repo sync so that I have the latest changes?
Can I have local branches (for each project I make changes) and simply merge from the master branch to my local branches after every repo sync?
When you run "repo sync" what actually happens is that each git repository is rebased on a new upstream. If you don't have any local patches in a specific git repository, it's a simple fast-forward. If you do have some patches and upstream does as well(your branch and upstream branch have diverged), repo will attempt an automatic rebase.
So lets say you have a patch on top of upstream code, and upstream has had some new comits since you applied that patch. When you run repo sync, repo will try to rebase your code on top of upstream. If the automatic rebase fails, repo will throw an error message letting you know that you should fix the patch manually.
To sum it up: You can create a branch in each project you want to modify, store your commits on that branch. Repo sync will automatically rebase your patches(unless it fails and then you'd have to apply them manually).
You will need to use repo start command to create a topic branch that tracks the remote repo branch. Or you need to use --track option of the git branch command to manually create a local branch with a remote tracking branch. Use the --set-upstream option of the git branch command to add a tracking branch to an existing local branch.
Once you have setup the tracking branch correctly, the repo sync command will fast forward and reapply your local patches as Anton Cherkashyn has described in his answer.
Use gerrit in conjunction with repo and git.
This seems to work for me.
First mentioning some setup
# cd to root of source tree
repo start MyBranch # Create working branch for all projects
repo checkout MyBranch # switches all projects to use MyBranch
Time passes, fabulous edits made and committed (in MyBranch), working branch is clean. Now want upstream changes ...
# Current active branch is "MyBranch"
# The following sync -d as per repo docs:
# -d: switch specified projects back to the manifest revision.
# Helpful if the project is currently on a topic branch,
# but the manifest revision is temporarily needed.
# In other words, it automatically syncs default manifest's upstream
repo sync -d -j12
# Active branch may be "MyBranch" or possibly a detached head or whatever.
# So, if necessary, switch back to MyBranch
# - I usually do this just to make sure all projects are in MyBranch
# - NOTE: If a new project appears it won't have MyBranch
repo checkout MyBranch
# Now we're in MyBranch. Its "upstream" is our local master so sync it.
# - This is usually rather quick
repo sync
The "repo sync -d" may not be necessary but hasn't caused any problems as far as I have seen. Plus it pulls the master codeline locally to keep it in sync for handy diffs and such.
Perhaps "repo sync" from within MyBranch does that too. But I don't seem to get any updates when I omit the "repo sync -d" step and just do "repo sync" when MyBranch is checked out. (Although maybe my local setup is messed up somehow)
To summarize:
Option A: Might work
cd RootOfRepoSourceTree # wherever you have it
repo checkout MyBranch
repo sync
Option B: Works consistently for me
cd RootOfRepoSourceTree # wherever you have it
repo sync -d -j12
repo checkout MyBranch
repo sync
I am able to clone the Android source code by using the "repo" tool. However, what I want to do is clone the source code in a more minimal way than having an 11GB footprint. It seems to download things related to every Android device and every prior release. I tried thought I could reduce this by checking out a specific branch like this:
repo init -u https://android.googlesource.com/platform/manifest -b android-4.0.1_r1
However, what ends up happening is that I still get everything involved, just at a specific snapshot (understandable). But is there any way to limit the amount that is cloned?
The android source tree is made up of many separate git repositories, which are all managed by repo. You can't really reduce the amount of data that's downloaded for a given git repository.
However, you can only download a subset of the git repos that are available, using repo sync <project>. I.e. if you only wanted the frameworks/base package, you should be able to do repo sync frameworks/base, after doing the initial repo init.
If you are actually wanting to build the source though, you probably want the full thing.
You might be able to save a gig or two by removing the device repositories that you don't need. You can do this by editing <source>/.repo/manifest.xml and removing the repositories for the devices you don't want.
There are a number of things you can do. First, "repo sync" with a -c argument will limit the checked-out sandbox to only the "current" branch. The repo sync will not download material for other branches.
You can also edit the .repo/manifests/default.xml manifest. What I do is make a backup of it such as "default.bak" and then strip out the lines from the default.xml file I know I don't need. Because I'm not building on a Macintosh, I know I don't need any of the "darwin" tools, so I remove every line that contains "darwin". Then "repo sync" doesn't download any darwin projects or install the source code in the sandbox.
If you already synched a sandbox, and you want to trim its size, you can strip projects out as above, and then do "repo sync -c -l", and repo sync will only strip the directories that you just removed. The -l flag only does the local part of the sync, which means it only syncs the local git projects with your source tree. The network is not used. See the "-n" flag for the other half of a normal sync, which does the network sync to update the local git projects with the upstream repository.
To also remove the git backing object stores for the unwanted projects (which take up a lot of room) I use this:
for project in `diff ~/android/.repo/manifests/default.xml ~/android/.repo/manifests/default.bak | awk '{print $3}' | grep path | cut -f2 -d\"`; do rm -rf ~/android/.repo/projects/$project.git ; rm -rf ~/android/.repo/project-objects/$project.git ; done
This finds all the projects that are in your backup manifest, but have been removed from your active manifest, and removes the git projects and all the backing data for them. This recovers a lot of space.
If you remove too much, just recover the project line from your backup manifest, and add it back into your active manifest. Then, a repo sync will get your git projects and your sandbox straight again. You can test the build to see if you've removed too much by doing "mma -B -n" in your project directory. This will try to do a full dependency build on your target, forcing all the targets to build even if they don't need it, and it will do it as a dry run. If the build fails, you removed something your project needs.
You can also set up a local mirror sandbox, and create small, working, reference sandboxes from your mirror. The working sandboxes don't contain git object stores, but refer to the central ones in the mirror. Use "repo init -u ... --mirror" to set up the mirror, and "repo init --reference=~/android-mirror -u ..." to refer to the mirror. The -u flag in the latter allows a real upstream repository to be used as the authority, while the mirror reference is used as a local cache. Local mirrors also avoid the download quota that the AOSP project enforces, and they are faster to sync to.