Git / GitHub.com

Git is a version control application for code developpers, and it was created by Linus Trovald, 2005.
Ther is two kind of version control applications : centralized ( CVS,SVN,…) and distributed (Git, Mercurial, Bazaar,…).
The centralized model, is a server based model,i.e., the data is in a server.
In a distributed model, the data can be hosted in many machine and at https://GitHub.com, the risk is minor to loose everything because the code is in many places, and there is no need of internet connection to work.

1. Instructions to install Git, on iOS, Linux and Windows
http://fr.openclassrooms.com/informatique/cours/gerer-son-code-avec-git-et-github/installer-git

2 Install on Linux : download lastest version on http://git-scm.com/downloads

Install on Windows
http://msysgit.github.io/
launch the downloaded file, and let the standards settings.
Look for now for “Git Bash” on your start menu and launch it

0

type as you can see below in the picture the following commands :
git config –global user.name “name  or nick name”
git config –global user.email “your email”
These commands set your name and email that you will use to create an free account on GitHub.
GitHub is free if you let your code open source.

1

.

use Ins key to paste, ctrl + v will not work.

.

git init
git status

1

git add

2

git commit
-m   for message

3

.

Changing a file

add a new file to our project
touch second_file.txt
git add second_file.txt
git commint -m “added new file”

Edit the file with a text editor ( Vim, or notepad …) and change it.
Type git status to see what we get ( look picture below),
we get a message “not staged”, that´s because we not tell that the changes in the file have to be indexed, we have to repeat :
git add second_file.txt
type again git status to see what we get now ( look picture below).

1

.

git log

We can list all commits with the command line git log.

A you can see, a commit have an identifier SHA, an author, the date, and the comment.

1

It is very important to have good comments messages.
The messages will be availabes for ever, and we have to be able to “understand” them a long time after, like one year.

.

Do we have to  “git add”  every time a file is changed ?

We can add the -a or –all flag to the end of the command. that will tell git to to check for changed files and any with changes should be comitted.

1

git commit -am “last changes”

.

get history

we can get an old version with git checkout command :

1

we can see than after the checkout, if we type git log we are in an older version, when we had only one commit.

git checkout master to get back to current version.

.


 

GitHub

It is important to have a backup of all our project and its commits.

GitHub is free, but there also have a payed option for private projects.

let´s first discover GitHub….

Rails

https://github.com/rails/rails/find/master

1

https://gist.github.com/discover allow to share/send code with someone, instead of using email….

We can search for code/projects…also big companies put their code in public domain.

1

 

Sign Up

You have to create an account to create a repository, just click on  Sign Up at https://github.com, and follow the instructions.

.

Repository cloning with HTTPS

-> Go to the project you want to clone.
-> Select HTTPS like in the picture below.
-> Copy the link of the project.

1

.
use git clone command to clone the repository…paste the link that you have previously copied.

1

.

Repository cloning with SSH

you can clone a repository with SSH

https://help.github.com/articles/set-up-git, you can find instructions about using SSH, and more…

click on link generate SSH keys to go to the following link   https://help.github.com/articles/generating-ssh-keys

Creating a repository and clone it on your PC

at https://github.com, select New repository

1

.

.

2

.

copy the URL of your project :

1

.

use the command git clone to clone your project on your PC.

.

1

we have now our project on GitHub and in our PC.

.

Send changes back to GitHub

change a file on you project on the PC.
-> git status                                   ( you have a red message )
-> git add README.md
-> git status                                   (the message is now green)
-> git commit -m “changed”
-> git status                                    ( you got the message “nothing to commit, working directory clean”)

1

we will send to GitHub with the command git push origin master
origin, is the default name of the remote if we only have one remote, like on GitHub,
but we could have a second remote on another PC, and another remote on you smartphone…
master, is the name of the branch.

2

Go to your project on GitHub, and verify that the project was updated.

you can click on commit to view/edit old versions of your file

1

click on the commit you want to look at …

1

highlight Green -> added code line
highlight Red     -> deleted

2

.

Get modifications

You can modifie code, directly on GitHub.

Select the file you want to change, and click on the pen as you can see on the picture :
1

change the code on the editor directly on GitHub :

2

go down in the same page, and commit your changes.

3

git pull origin master, and will get updated the files that have been modified.

you can see a message that README.md has been modified,i.e.,  we downloaded only one file.

1

We just had synchronized our remote with our local PC.

It is important when we working together with other developers.

Creating Branchs

Imagine you want to try something in you code that you not sure about…
So you create a branch,i.e., you get out from the master branch, and at the final if you try as the good results you wanted,  you can merge the two branchs.

git branch  to check in what branch we are. By default the main branch is called master. The * tell us on which branch we are on.
git branch my-test to create a branch.
git checkout my-test, to switch to the branch my-test.

1

once at my-test branch, you can modifie the code…
if we get back to master branch git checkout master, the previous changes will not there.

you can modifie code on master and in other branches, in parallel…

.

Merging Branchs

put you on master branch and type command git merge my-test

or

put you on my-test branch and type command git merge master

Solving Conflicts

It is recommended to do not work in same files code, simultaneously, with another developer partner,

but it is unavoidable that it will happen.

.

.

.

.

.

 

Leave a comment