Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,729,690 Members 47,788 Now Online
XDA Developers Android and Mobile Development Forum

[GUIDE]Git tuts drive me nuts -a Roofer's guide to GIT

Tip us?
 
jamieg71
Old
#1  
jamieg71's Avatar
Senior Member - OP
Thanks Meter 7458
Posts: 2,959
Join Date: Mar 2011
Default [GUIDE]Git tuts drive me nuts -a Roofer's guide to GIT

For all the things you can do in Android
somewhere someone has made a great definitive guide
that will help to both guide you
and provide a basic understanding of the steps involved
assuring you of success in your android tweaking endeavour.
This is not one of those guides.
I'm going to do my best to articulate my
dysfunctional
and admitted retardation
of an understanding of git.
I'll provide some links to guides I have found
so you can check them out and hopefully get a better grasp than I.
When you do, post your tips and tricks here..
I will add links to any posts with tips tricks or guides so you get your proper credit.
(if your into that sorta thing)


This guide is in no particular order and
is subject to random mistakes and inaccuracies.
I will be putting all git commands in red
If it's in red you can assume you just type that and hit enter.


What is Git?
Git is a tool used to manage projects housed on github and/or locally on your machine. For a greater understanding and comprehensive guide try gitready.com



Cloning

The simplest of things to do with git. Same steps to clone any repository be it your own or one from another github.

git clone <URL of your repository on github> -b <branch> (name of repo)

Example:
Code:
git clone git@github.com:jamieg71/kernel.git -b master msm8960
This tells it what to clone, denotes the branch with -b in this case branch
name is 'master' and finally (name of repo) tells git what you wish for your
cloned repository to be named in this case 'msm8960'.

Making commits & Pushing

Let's say you want to make a couple changes to an app and then push those changes back to github,
we'll use ROMControl for an example.

Navigate to root of ROMControl directory.


First do
repo start ska43 .
Here use the name of the branch you are on, for SofaKingAndroid 4.3 it's "ska43",
also include the dot [space dot] "." at the end of the command.
If instead of working within the rom source in /packages/apps/ you
instead had cloned ROMControl to it's own seperate folder, this is not necessary.

Some helpful commands-
Quote:
gitk a helpful GUI for git

git status
This will give you an update of what branch you are on and what if any changes have been made.

git log
Will give you a commit history with change ID's commit messages authors time stamps etc,
it can be very handy when looking for specific commits to cherry-pick or revert. This should
show the complete history of a given project with all contributions along the way. To exit this
quickly hit the "q" key.

git reflog
This will give you a short history of your local cloned repository listing each seperate head
Once you've made your changes, (and built to be sure everything works as planned....)

git status Just for a reference of what you've done, it will also show any files with a "~" in case your like me and occasionally screw up

To stage changes before committing you have multiple options-

Quote:
git add .
This will add ALL changed files, sometimes it's handy but if you have done several unrelated
things it can be nice to have them in separate commits with their own commit messages and ID's

git add -a
Same as above

git add (directory/name of file) Example: git add src/com/aokp/romcontrol/AOKPActivity.java
The easiest way to do this, for me, is to just c/p the names of modified files
from the list after running git status
Now to commit-

git commit -m 'your message here' Put a brief description of what your commit involves

To Push-

git push <URL of your repository on github>
Example:
Code:
 git push git@github.com:SofaKingAndroid/packages_apps_ROMControl.git
Fetching Upstream Changes

What if you had forked a project and been working on it for awhile, and you notice there where changes to the original source. Now you need to pull in upstream changes. To do this let's clone our fork to it's own folder on our machine-

We will assume you work in ~/android/ and know how to mkdir upstrm4krnl && cd upstrm4krnl

git clone <URL of your repository on github> -b <branch> (name of repo)
Example:
Code:
 git clone git@github.com:jamieg71/kernel.git -b master msm8960
This tells it what to clone, denotes the branch with -b in this case branch
name is 'master' and finally (name of repo) tells git what you wish for your
cloned repository to be named in this case 'msm8960'.

To add a remote repository to fetch upstream changes, (where you forked from)

git remote add upstream <URL of remote repository>
Example: [code]git remote add upstream git@github.com:CyanogenMod/android_kernel_htc_msm8960.git

git fetch upstream
This will add the log and pre stage all the changes on the remote repository.
After doing this Be aware of what your intentions are. If there have only been
minor changes with low likelyhood of major conflicts go ahead with merge.
Otherwise it may be best to cherry-pick specific changes.

To add these changes to your working files you have some options, I'm positive these are not the extent of those options but perhaps someone with better know-how will chime in....
Quote:
git merge upstream/master
This merges all commits from both the upstream repository and any commits
you made to your forked repo.

git pull upstream/master
This will pull in changes only from the remote repository
For a better explanation see Git for Computer Scientists

Now do-

git checkout master
This should return you to your working branch

of course do a git status



Finally do-

git add .

git commit -m 'commit message'

git push
From cloned repo a simple git push should suffice. If not go ahead
and do git push <URL of your forked repo>
Example:
Code:
git push git@github.com:jamieg71/kernel.git
Cherry-picking

What is cherry-picking? Simple answer- grabbing commits from a different repo and adding to your own.

Here's some links-

Cherry-Pick-Manual from Kernel.org it don't 'git' more comprehensive than that.

Gitready Guide Greate guide for beginners to experts

I never really got it until I found this on Pastebin. Unknown author or I'd give em creds in all my threads.

And another here it has a different take at the begining

A multitude of guides can be found on Google, I have found the majority take for granted there is a roofer trying to follow them and don't necessarily explain some of the simple things. Which is always what screws me up..

Here's what works for me. I'm going to c/p a session from terminal and will highlight the actual git commands in red and add notes in green (0)
Code:
jamie@KANGBOT-1:~/android/sofa/frameworks$ git clone git@github.com:SofaKingAndroid/frameworks_base.git -b ska43 base
Cloning into 'base'...
remote: Counting objects: 796444, done.
remote: Compressing objects: 100% (192783/192783), done.
remote: Total 796444 (delta 525643), reused 795809 (delta 525149)
Receiving objects: 100% (796444/796444), 1.06 GiB | 6.67 MiB/s, done.
Resolving deltas: 100% (525643/525643), done.
Checking out files: 100% (21192/21192), done.
jamie@KANGBOT-1:~/android/sofa/frameworks$ cd base

jamie@KANGBOT-1:~/android/sofa/frameworks/base$ git status
# On branch ska43
nothing to commit, working directory clean
jamie@KANGBOT-1:~/android/sofa/frameworks/base$ git remote add ok git@github.com:AOKP/frameworks_base.git    (1)
jamie@KANGBOT-1:~/android/sofa/frameworks/base$ git fetch ok 
remote: Counting objects: 40, done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 23 (delta 14), reused 12 (delta 3)
Unpacking objects: 100% (23/23), done.
From github.com:AOKP/frameworks_base
 * [new branch]      jb         -> ok/jb
 * [new branch]      jb-broke   -> ok/jb-broke
 * [new branch]      jb-mr1     -> ok/jb-mr1
 * [new branch]      jb-mr2     -> ok/jb-mr2
jamie@KANGBOT-1:~/android/sofa/frameworks/base$ git cherry-pick 4829174d6f6fa33cd51f2bde9df2e100420ef3bf  (2)
[ska43 f8380a3] SystemUI: 4G/LTE icon toggle
 Author: Roman Birg <romanbirg@gmail.com>
 2 files changed, 17 insertions(+), 2 deletions(-)

jamie@KANGBOT-1:~/android/sofa/frameworks/base$ git status
# On branch ska43
# Your branch is ahead of 'origin/ska43' by 1 commit.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean

jamie@KANGBOT-1:~/android/sofa/frameworks/base$ git remote add upstream git@github.com:SofaKingAndroid/frameworks_base.git  (3)
jamie@KANGBOT-1:~/android/sofa/frameworks/base$ git push
Counting objects: 31, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (16/16), 1.61 KiB, done.
Total 16 (delta 10), reused 0 (delta 0)
To git@github.com:SofaKingAndroid/frameworks_base.git
   05ade0e..f8380a3  ska43 -> ska43
Notes

1) git remote add (initials) <URL of repository to grab commits>
The initials are what you make em, I used "ok"

2) git cherry-pick <Change ID or SHA of commit you want>
To find the change ID refer to commit history for
the project you are working on after fetch
you may be able to do git log and find the
change ID you want.

3) git remote add upstream <URL of your github repository>
Pretty sure I could have done git checkout ska43 and
it would have returned me to my working files, but I
wasn't sure so I did it this way.

This is an example of a very simple cherry-pick with no conflicting code. Most times when cherry-picking there will be many conflicts. You can use gitk (a GUI for git) to help resolve conflicts and get ready to merge.

More to come.
If you see a mistake or have anything to add
please by all means
respond in this thread.
More to come-
The Following 36 Users Say Thank You to jamieg71 For This Useful Post: [ Click to Expand ]
 
jamieg71
Old
#2  
jamieg71's Avatar
Senior Member - OP
Thanks Meter 7458
Posts: 2,959
Join Date: Mar 2011
reserved
The Following User Says Thank You to jamieg71 For This Useful Post: [ Click to Expand ]
 
Powerhouse
Old
#3  
Powerhouse's Avatar
Senior Member
Thanks Meter 1965
Posts: 795
Join Date: Mar 2013
Great Guide .!
The Following User Says Thank You to Powerhouse For This Useful Post: [ Click to Expand ]
 
v_superuser
Old
#4  
v_superuser's Avatar
Senior Member
Thanks Meter 1226
Posts: 454
Join Date: Jan 2013
Location: Jammu
Thanks for this one! Bookmarked!


Join the HTC Desire 816 Facebook group for all Development Info and Discussion!
 
Q9Nap
Old
#5  
Q9Nap's Avatar
Senior Member
Thanks Meter 440
Posts: 540
Join Date: Feb 2009

 
DONATE TO ME
haha, love the title, thanks for the laugh and useful info!
The Following User Says Thank You to Q9Nap For This Useful Post: [ Click to Expand ]
 
mikshepard
Old
#6  
Senior Member
Thanks Meter 1204
Posts: 417
Join Date: Apr 2010
Location: Minneapolis
Nice work Jamie!

-Mike
The Following User Says Thank You to mikshepard For This Useful Post: [ Click to Expand ]
 
MaDc0w
Old
#7  
MaDc0w's Avatar
Senior Member
Thanks Meter 474
Posts: 220
Join Date: Aug 2009

 
DONATE TO ME
Great work there Jamie!
HTC One m7
- HBoot: 1.56.0000 S-OFF, SuperCID Radio: 4A.22.3263.14_10.38n.1157.04L
- Recovery: RomSwitcher Custom Recovery ROM: AHD / PAC-man 4.4
Samsung S3 LTE i9305
- ROM: PAC-man 4.4

The Following User Says Thank You to MaDc0w For This Useful Post: [ Click to Expand ]
 
wyelkins
Old
#8  
wyelkins's Avatar
Senior Member
Thanks Meter 83
Posts: 114
Join Date: Jul 2013
Good Job! Lots of work went into this along with a sense of humor.
The Following User Says Thank You to wyelkins For This Useful Post: [ Click to Expand ]
 
percy_g2
Old
#9  
percy_g2's Avatar
Senior Member
Thanks Meter 12159
Posts: 4,074
Join Date: Oct 2012

 
DONATE TO ME
forking in another way

git remote add fork <url of fork>
git fetch fork
git checkout -b fork_branch fork/<branch>

example :-

I want to fork https://github.com/omnirom/android_frameworks_av android-4.4 branch into https://github.com/Legacy-Xperia/android_frameworks_av xyz branch

make a directory as you wish in your PC/COMP

git init
git pull https://github.com/Legacy-Xperia/and...meworks_av.git
git remote add fork https://github.com/omnirom/android_frameworks_av
git fetch fork
git checkout -b xyz fork/android-4.4


done then push it


PS:- You can directly fork using browser but i love working via terminal
The Following User Says Thank You to percy_g2 For This Useful Post: [ Click to Expand ]
 
Nico_60
Old
#10  
Nico_60's Avatar
Senior Member
Thanks Meter 1581
Posts: 950
Join Date: Apr 2012
You can also add gitg in OP, it's a great software for working with git.

The Following User Says Thank You to Nico_60 For This Useful Post: [ Click to Expand ]
Tags
bangshead, cherry-pick, commit, git
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes