In software development, Git (/ɡɪt/) is a distributed revision control and source code management (SCM) system with an emphasis on speed. Initially designed and developed by Linus Torvalds for Linux kernel development, Git has since been adopted by many other projects.
Git isn’t big deal, but many finds it very tough to begin with. To master git its matter of time. Here comes a simple note on how you can start with git and push your first code to github. Eventually you can grow into git-master. So lets begin!
1) Create A Repo
go to www.github.com and sign up. I leave it to you to install git. Even you haven’t configured your ssh keys, its cool. I will go “http” way in which you ain’t need to manage your ssh keys with github. If you are thinking what the hell is “ssh” keys then just ignore it.
So, by now I assume you have signed up for github and you have logged it.
Lets create a repo from top right corner as shown in image below.
Next Page will look like image below:
Describe your repo, initiate with README.md and your repo is ready
Done. Click “Create repository” and come back to your terminal.
➜ ~ mkdir my_git_repos (choose a directory name of your choice)
➜ ~ cd my_git_repos
copy the link from your repository, we will need it in a while.
2) Clone the Repo
now hit the following:
➜ my_git_repos~ git clone https://github.com/linuxcandy/sample.git
Cloning into ‘sample’…
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
➜ my_git_repos~ ls
➜ my_git_repos~ cd sample
➜ sample ~ ls
hit above commands and you should see exactly like what is shown above. Let me quickly tell you about what happened. “mkdir” created a directory, “cd <directory name>” took you into the directory, “git clone” cloned a version of what is their on github.com of your repository to your computer in the folder named “sample” which is your repository name and “cd sample” took you inside your repository on your local machine, and finally “ls” told you that “hey dude, you have a README file in here” . Congratulations!!
Cool, not lets push some code from our local machine to our github repository.
Create a file inside “sample” folder. I have created a “my_code.py” file. and inserted a “hello git world” line in it. I am going to show you how to push this to our repo in github.
From your terminal hit the bold codes below without thinking what they are going to do, and I will tell you after you are don.
3) Commit and Push a Code
➜ sample~ git status
# On branch master
# Untracked files:
# (use “git add <file>…” to include in what will be committed)
nothing added to commit but untracked files present (use “git add” to track)
➜ sample~ git add my_code.py
➜ sample~ git commit -m “my first code to git”
[master 0780b5f] my first code to git
1 file changed, 1 insertion(+)
create mode 100644 my_code.py
➜ sample~ git push
Username for ‘https://github.com’: linuxcandy
Password for ‘https://firstname.lastname@example.org’:
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 315 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
83c5881..0780b5f master -> master
➜ sample~ git status
# On branch master
nothing to commit (working directory clean)
➜ sample~ cat my_code.py
print “hello git world”
Congratulations again! you have pushed your code to your repo in github. Don’t believe it ? Lets have a look.
You see ? you have your code there!
ok now let you tell you what you did or what the codes above do.
git status -> to check which stage you are in. it will show you a detail about your status.
git add <filename> -> after you make changes in a file. you need to add it
git commit -m “<message>” -> this commits your addition of file above with a short message about your edit.
git push -> pushes your commits, changes to repo remotely. Here you are done!
Wait, dont rush. there is one thing I forget to tel you blame my memory . Here we created our own repo, but what if we want to work on somebody else’s repo ? or want to clone it and start contributing or helping a friend in his/her repo at github ?
Everything is same like above but before you push any code you need to “pull” latest code because this time you are not the only one which will be working on the code. Working in a team on a same code is different.
so you need to follow like this:
Pull > Make Changes > Add <edited file names> > Commit -m “<message>” > Push
Remember the rule, and you are a git hero!
Wanna be super hero ? Read this book http://git-scm.com/book