FORUMS
Remove All Ads from XDA

GIT 101 - Newbie Friendly

270 posts
Thanks Meter: 450
 
By The Arrow, Retired Forum Moderator on 9th December 2016, 12:24 PM
Post Reply Email Thread
Hello,

Today I would like to share an easy git tutorial for all of you. We at XDA believe everyone loving Android and passionate about the same can contribute something or other to the community. Not everyone is an expert at everything like theming, bootanimations, building ROMs, Kernels etc etc. But git is an essential yet powerful tool in most aspects. This thread is for new users and old alike , wanting a correct direction of learning basic Android by starting with git. Okay enough of lectures? Bwahaha I know it's boring lol

So let's start

Gitlab? What exactly it is? How is it different from github?
GitLab is an application to code, test, and deploy code together. It provides Git repository management with fine grained access controls, code reviews, issue tracking, activity feeds, wikis, and continuous integration. Inshort and in simple english, it is a place to store all your sources, it maybe private, public or restricted.

Difference : GitHub is a publicly available, free service which requires all code (unless you have a paid account) be made open.GitHub currently hosts the source code for tens of thousands of open source projects. GitLab is a github like service that organizations can use to provide internal management of git repositories.



Signing up, Creating an account and setting up git
Go to gitlab web

You have an option to sign in with google, github, twitter or bitbucket. It's the easiest way. I prefer github

Now let's download the git package. I use Linux Mint 18 so,
Code:
sudo apt-get install git
Verify if it is correctly installed in your system
.
Now let's configure the terminal
Code:
git config --global user.name "Your Name Here"
git config --global user.email "[email protected]"


Now let's generate ssh keys
go to your terminal and type
Code:
ssh-keygen
and hit enter thrice I guess until you are back to bash.
Or
Code:
ssh-keygen -t rsa -C "[email protected]"
Now
Code:
cd .ssh
cat id_rsa.pub
(.pub is your public key while the other one is private key, Don't ever share private keys with anyone)

Now let's verify all that we've done so far.
Code:
git config --global --list


Now let's add ssh-keys we generated to our account at gitlabs OR we get this :P


Click : Add an ssh key and copy paste the ssh-key (id_rsa.pub) there and click okay.

Now let's check if everything works
Code:
ssh -T [email protected]
This warning...
Code:
The authenticity of host 'github.com (207.97.227.239)' can't be established.
# RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
# Are you sure you want to continue connecting (yes/no)?
Type YES and here we're done with configuration part




XDA:DevDB Information
GIT 101, Tool/Utility for the Samsung Galaxy Chat

Contributors
Vatsal, malybru, sawdoctor

Version Information
Status: Testing

Created 2016-12-09
Last Updated 2016-12-20


XDA:DevDB Information
GIT 101 - Newbie Friendly, ROM for the Android General

Contributors
The Arrow
ROM OS Version: 2.3.x Gingerbread

Version Information
Status: Testing

Created 2018-04-25
Last Updated 2018-04-25
The Following 49 Users Say Thank You to The Arrow For This Useful Post: [ View ] Gift The Arrow Ad-Free
 
 
9th December 2016, 12:24 PM |#2  
The Arrow's Avatar
OP Retired Forum Moderator
Flag Starling City
Thanks Meter: 450
 
More
Now let's start with creating a new repo and importing it from github (If you are a previous github user and want to switch to gitlabs)

Just click new project button and you'll have this



And I click import the project with github and I choose the project I wish to import



And I have this :



Please be patient and grab some coffee?
It will import the entire project with all the branches.

And it's done..



Now let's clone the project locally to make changes. We can make the changes on web too but well it's really a mess so most people prefer CLI way.

Click the copy to clipboard button as in screenshot



and clone it locally.
Command :
Code:
git clone <Paste the URL copied> -b <branch name>
Note : the default branch is cloned with you clone with .git



Wait for it to finish and get back to bash done.
Now we have the repo locally. Now we go to the repo to check the files and edit as we wish.

First we grab the latest changes from the original repo to check if we have latest changes.

Code:
Example : the repo I am cloning is forked on my github and I imported from there to gitlabs.
The original repo is here https://github.com/CyanogenMod/android_device_xiaomi_hydrogen. 
So I make sure I have the latest changes.
To get the latest changes, I use
Code:
git pull <original repo URL> <branch name>
So here, I am pulling from the CM repo I mentioned above and cm-14.1 branch.



Now I just edit something and make change so as to commit it.
So I just deleted the cm.dependencies file from the repo.
Now we
Code:
git add -A
to add the untracked changes.
A more detailed info is here

The important point about git add . is that it looks at the working tree and adds all those paths to the staged changes if they are either changed or are new and not ignored, it does not stage any 'rm' actions.

git add -u looks at all the already tracked files and stages the changes to those files if they are different or if they have been removed. It does not add any new files, it only stages changes to already tracked files.

git add -A is a handy shortcut for doing both of those.

So let's commit now by using
Code:
git commit
and we get this



Write the commit name (Any you like). Now save and exit and we have this



Now let's learn further about
Code:
git status
.
Just as the name suggests, it tell us the status of the particular repo.
It displays the path and difference between cloned repo and the orginal repo.


DESCRIPTION
Displays paths that have differences between the index file and the current HEAD commit, paths that have differences between the working tree and the index file, and paths in the working tree that are not tracked by Git (and are not ignored by gitignore[5]). The first are what you would commit by running git commit; the second and third are what you could commit by running git add before running git commit.


Now it's time to push the changes we made to the gitlabs repo.
Calm down! It is no rocket science. Infact the easiest part.
Command :
Code:
git push <URL of the repo> HEAD:<branch name>


Enter your username and password correctly.

And pushed...


And now let's check at gitlabs if it is successfully pushed.



Done :victory:
The Following 31 Users Say Thank You to The Arrow For This Useful Post: [ View ] Gift The Arrow Ad-Free
9th December 2016, 12:24 PM |#3  
The Arrow's Avatar
OP Retired Forum Moderator
Flag Starling City
Thanks Meter: 450
 
More
So now we learnt pull, push, status. We proceed with The most important command
Code:
cherry-pick
{Most of the people might have heard of it, and wonder what it is?}
Cherry picking in Git is designed to apply some commit from one branch into another branch, one repo into another

Given one or more existing commits, apply the change each one introduces, recording a new commit for each. This requires your working tree to be clean (no modifications from the HEAD commit).

When it is not obvious how to apply a change, the following happens:

The current branch and HEAD pointer stay at the last commit successfully made.

The CHERRY_PICK_HEAD ref is set to point at the commit that introduced the change that is difficult to apply.

Paths in which the change applied cleanly are updated both in the index file and in your working tree.

For conflicting paths, the index file records up to three versions, as described in the "TRUE MERGE" section of git-merge[1]. The working tree files will include a description of the conflict bracketed by the usual conflict markers <<<<<<< and >>>>>>>.

No other modifications are made.


So I would wish to cherry-pick a particular commit on the same repo we have been working upon.
This is the commit I want to pick in my repo.



For cherry-picking, first we need to learn
Code:
git fetch
What does git fetch do?
git fetch - Download objects and refs from another repository

Command :
Code:
git fetch <URL> <Branch>
Fetch branches and/or tags (collectively, "refs") from one or more other repositories, along with the objects necessary to complete their histories. Remote-tracking branches are updated (see the description of <refspec> below for ways to control this behavior).

By default, any tag that points into the histories being fetched is also fetched; the effect is to fetch tags that point at branches that you are interested in. This default behavior can be changed by using the --tags or --no-tags options or by configuring remote.<name>.tagOpt. By using a refspec that fetches tags explicitly, you can fetch tags that do not point into branches you are interested in as well.

git fetch can fetch from either a single named repository or URL, or from several repositories at once if <group> is given and there is a remotes.<group> entry in the configuration file.

When no remote is specified, by default the origin remote will be used, unless there’s an upstream branch configured for the current branch.


So we fetch it from the AICP github with branch name n7.1

and then we cherry-pick it by using
Code:
git cherry-pick <commit ID>


and now it's cherry-picked. We have that commit in our repo.

Note : This was an easy pick but most picks aren't this easy. We will learn about fixing conflicts later on

Now we push it to gitlabs repo.


and we'll check if our gitlabs repo has it now !?



Now we learn how to create a new branch and push to that branch.
We create a new branch by
Code:
git branch <new branch name>
This creates a new branch with the new branch name specified in the command.

Now we checkout the branch to switch to that particular branch.
Command :
Code:
git checkout <branch name>

git checkout <branch>
To prepare for working on <branch>, switch to it by updating the index and the files in the working tree, and by pointing HEAD at the branch. Local modifications to the files in the working tree are kept, so that they can be committed to the <branch>.

If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to

Code:
git checkout -b <branch> --track <remote>/<branch>
You could omit <branch>, in which case the command degenerates to "check out the current branch", which is a glorified no-op with a rather expensive side-effects to show only the tracking information, if exists, for the current branch.




Now that we have successfully cherry-picked,
we will learn to show the commits logs in terminal itself (Not GUI).
Code:
git log
can help us with that.
We can also add a flag besides it for example 'git log -3' to see the last 3 commits.

Shows the commit logs.

The command takes options applicable to the git rev-list command to control what is shown and how, and options applicable to the git diff-* commands to control how the changes each commit introduces are shown.




Now we learn how to revert a cherry-pick ?
Well it's pretty easy.
So I grab the commit ID from the commit I wish to revert.
Here's the command
Code:
git revert <commit ID>

After reverting make sure to push it to the repo to see the revert there as well :)

Supposing we make a change to a repo and we don't wish to commit it. Instead we wish to reset the repo to the previous state,
Here is a ways we can do it.
Code:
git stash
It discards all the local changes and gets it back to the last commit.

Also suppose if we want to revert 20 odd commits and we have force push access (And you are lazy like me :P), we don't revert it one by one. Instead we can just hard reset the repo to the last commit
Command :
Code:
git reset --hard <commit ID>
This is the easiest way to do it.

Now the last one. Force pushing
Sometimes github doesnt let us push after resetting hard because the previous commits dont exist in our repo.
We can just force push to force it accept the commits
Command :
Code:
git push -f <URL name> <branch name>
It is basically a noob method to just add changes easily after a failed or unsuccessful push.
The Following 35 Users Say Thank You to The Arrow For This Useful Post: [ View ] Gift The Arrow Ad-Free
9th December 2016, 03:16 PM |#4  
The Arrow's Avatar
OP Retired Forum Moderator
Flag Starling City
Thanks Meter: 450
 
More
Credits
- @malybru , @sawdoctor for always being there for me.
- @eagleeyetom , @Neo , @Perseus , @Darth , @KennyG123 , @The Merovingian for a lot of things (Will fill up pages if I start writing ).
- The entire XDA Staff
- @TechExhibeo and @@jackeagle for helping me through my noob days
- Gitlab ofcourse
-GZR Team for not killing me when I asked a lot of stupid questions.
- Legacy Team for guiding me through lot of stuffs.
- Everyone, that I forgot to mention.
The Following 30 Users Say Thank You to The Arrow For This Useful Post: [ View ] Gift The Arrow Ad-Free
9th December 2016, 03:17 PM |#5  
The Arrow's Avatar
OP Retired Forum Moderator
Flag Starling City
Thanks Meter: 450
 
More
And this too. Moar spams incoming.. :P
The Following 7 Users Say Thank You to The Arrow For This Useful Post: [ View ] Gift The Arrow Ad-Free
14th December 2016, 02:48 PM |#6  
TechExhibeo's Avatar
Recognized Contributor
Flag Ropar
Thanks Meter: 1,476
 
Donate to Me
More
Great Help for Newbies
Thanks Vatsal Sir _/|\_

This will be very helpful for Newbies learning stuffs.

Great Work :*

-TechExhibeo
The Following 11 Users Say Thank You to TechExhibeo For This Useful Post: [ View ] Gift TechExhibeo Ad-Free
14th December 2016, 03:59 PM |#7  
jackeagle's Avatar
Retired Forum Moderator / Recognized Developer
Flag XDA
Thanks Meter: 7,703
 
Donate to Me
More
Wonderful Guide and great work @Vatsal ! This will help newbies a lot Every aspect of git is mentioned in 1 Guide Keep up the spirit sir!

Cheers!
jackeagle
Forum Moderator
The Following 7 Users Say Thank You to jackeagle For This Useful Post: [ View ]
14th December 2016, 05:38 PM |#8  
Shreesha.Murthy's Avatar
Recognized Developer/Contributor
Flag 127.1.1.0
Thanks Meter: 6,897
 
More
Nicely written
14th December 2016, 05:43 PM |#9  
Anik_khan's Avatar
Inactive Recognized Developer
Flag Mymensingh, Dhaka
Thanks Meter: 2,725
 
More
Nias works
14th December 2016, 05:44 PM |#10  
Sudarshankakoty's Avatar
Senior Member
Thanks Meter: 32
 
More
Well tutorial
14th December 2016, 05:46 PM |#11  
srisurya95's Avatar
Recognized Contributor
Flag Hyderabad
Thanks Meter: 7,273
 
Donate to Me
More
Good work mate!
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes