share your coffee, share your code

hello there, again, surviving my first actual week of the internship. how’s things? well, hectic, new, and awesome. i may have jumped the gun, but oh well, who doesn’t. 😛

doing things together is fun…urm..yes. and coding included, this continues in a very well structured manner. so, what do we do to manage, share and work together in big program, a big code? well, we have git, particularly, github.

so, what the heck is github? well, it’s kind of an social platform for developers to share and collaborate their codes.

that didn’t make much sense now did it?

well, think of a new social network, where people come in, create a profile, and ogle at each other. (i juts described facebook, didn’t i?) 😛 well, this is just like that, only difference, you get to ogle at work of other people, share your work, and develop something worthwhile while you’re at it. when was the last time that anything productive happened out of ogling at your crush on facebook? 😛

so why use this git..whatever you ask? say you’re working on a little webpage with your friend. you design one heck of a page, you pat yourself on the back, upload it to a common database. whilst all this “mesmerizing” work was happening, your friend was also making a page, with the very same name and for the very same path. he creates that particular page and saves it exactly where you have saved your page……aaaaaaaand poof.. there goes your page.

let’s take another scenario. you’re working on a bit of code from a huge project, several people are working on the same thing and, when it comes down to the final stages, you want to arrange the code together to see if it meshes together. well, i’m not talking about your garden variety hello world program here. if the code in question is huge, it becomes more and more complex to manage it, so we use github to do all that work. (poor guy, doing all that, and for free…)

before we actually get started with github, let’s get ourselves familiarized with the terminology(fancy words) shall we?

command line/terminal
you know what this is. now this has become the center of your universe. no? okay. anyway, we use the terminal to handle all the functions of git. no, git doesn’t code, you can use your favorite coding environment for the development. git is for handling what was created afterwards.

repository
this basically is the location where you keep your source file, locally or online. this can have your code, helping items such as images and other files. many people call this the “repo” (no, not like repo man… but if it was, think a guy collecting code…waaaiiittt…).

version control
this, is what github does. basically, when you save a file, it overwrites the previous version of the file and keep the new one. but what github does is, it keeps all the milestones. so whether you add or reduce some code from the base code, it’ll have records of it, so if you mess up, you have backups. literally you can go back in time.

commit
this is the abra-cadabra of github. what this does is, takes the snapshot on the point where you run this code. so this creates previously mentioned milestone that you can rely on later.

branch
this is where you share your coffee with your friends. what this means and does is, you can share the same code, among the developers, develop separately, and later you can merge them together, without your friend screaming in your ear, no! that should go on five hundred and forty six thousand seven hundred and twenty fifth line.

master
this, is the main branch, the principal branch, finally all the branches come together and make the master. no biggie.

so, to get started with github, you’ll need a github account. so head on over to github.com, and get yourself registered. once you do that, you can start your work on github. then you need to download git to your machine, for that head on over to download git and we can start configuring afterwards.

for ubuntu/debian users, you can easily install with the terminal.

apt-get install git

once you’ve installed git, let’s configure it. first run this code. this will set your user name.

git config --global user.name "Your Name Here"

preferably, use your username you selected when creating the github account. less confusions this way.

then run this. this will set the email to your git.

git config --global user.email "your_email@youremail.com"

then we will configure ssh key for you machine. why the ssh key? this is to easily identify you computer without having to ask for passwords. to do this, first, run the following code.

ls -al ~/.ssh

this, check the current ssh keys in your system. if you have ssh keys installed with git, well you can skip these steps, well, i don’t think so, so let’s continue eh.

to generate the ssh key for your machine, enter and run the following code.

ssh-keygen -t rsa -b 4096 -C "your_email@youremail.com"

the email should be the email you used to register on github.

Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter here]

then,

Enter passphrase (empty for no passphrase): [Type a passphrase]
# Enter same passphrase again: [Type passphrase again]

enter your github password here.
Then you’ll be given your ssh key. then let’s add this key to your ssh agent.

eval "$(ssh-agent -s)"

above is to check whether your agent is enabled.

ssh-add ~/.ssh/id_rsa

this, will add the key to the agent.

then, we have to add the ssh key to your github account. so open your ssh key with your favorite text editor.

emacs ssh-add ~/.ssh/id_rsa.pub

make sure you grab all the data inside the file, and make sure you don’t add anything too. 😛

then go to your account online -> settings -> ssh keys -> add ssh key -> “give a proper name.. like, github, linux pc and cats” and paste the copied key in the key text area. then click add key and you’re done.

to check whether it worked, run the following code.

ssh -T git@github.com

if that timed out, try this.

ssh -T -p 443 git@ssh.github.com

then a message should come up like this.

Hi username! You've successfully authenticated, but GitHub does not provide shell access.

got it? yay! now we can actually start work with github. (if you have the windows/mac github ui clients, well, la-di-da.. ain’t your life a beach?! 😒)

so now let’s go through some commands to get you started with github.

first things first, change the directory to your project folder, for example, i’m working on this awesome hello world java program and i want to do the git commands with it. so first up, i change my directory in the terminal to that particular fol..directory. (ahhh.. i give up! d-i-r-e-c-t-o-r-y)

so, to initialize your current directory as a git repository, run the following code.

git init

by running that code, you create a local repo, this still hasn’t been added to the online repo yet. so, hold on!

this next command is something you’ll need a lot.

git status

this command will give you the breakdown of things for you. the files that have been added to be committed, any files you’ve missed, any new files that you haven’t added yet..everything will be displayed here. so, it’s good to run this once a while, just to make sure that everything is smooth.

this next command will add files to git so it’ll start to track that file.

git add myclass.java

once you run the above command and upon success, try running git status again. it’ll show that there are changes to be committed. this means there are some files git is tracking but not committed yet. so to commit, run the following code.

git commit -m "my awesome hello world program v1"

if you have several files of the same type to be added, just run the following code.

git add '*.java'

to add all the files in the directory, run the following command.

git add *

don’t forget to check status and commit the added files.

this next command, is also very valuable. as programmers, we learned to love logs. (no, not the ones you get from cutting trees. cheesus!) even github has a log option.

git log

this command will display the log of things you’ve done so far. comes in real handy when you’re trying remember what you did last on last friday!

so, now to adding your files to the online repository. to do this, either you have to have created a repository at github.com or, use the following command to create one from your terminal.

curl -u 'username' https://api.github.com/user/repos -d '{"name":"helloworldproject","description":"this is a hello world program"}'

you might have to install curl using apt-get first, but you know those now. 😛 make sure you replace ‘username’ with your github username, “helloworldproject” with your project name, “this is a hello world program” with a little description about you project.

**if this didn’t work, you might want to check this post at viget on how to,as you might have a token issue. if not, you can always rely on going to github web and creating the repo from there. that works always.

so once you have created the repo online, we can connect our local repo to the online repo. to do this, use the following code.

git remote add origin https://github.com/username/helloworldproject.git

make sure you alter username and the name of the .git according to what you named it.

so once we’re connected, now, finally we can push (send the files) the files to the online repository. to do that, run the following code.

git push -u origin master

the name our remote is origin and, we pass master as it’s our main branch. -u tell git to remember the parameters, so you can just say git push the next time you push files.

so now, your local repo and online repo and successfully synchronized. yay! 🎉😁

so, once you’ve done all this, and later if you want to check changes with the online repo, you can run the following.

git pull origin master

if you see any changes and if you want to view the differences between the repos, you can run…

git diff HEAD

now here, diff is the command and HEAD is a pointer.

to view the changes that you have staged, you can run the following command.

git diff --staged

so, what if you need to unstage a file? if so, you can do that by..

git reset classes/imnotsupposedtobehere.java

if you want to undo the changes upto certain point regarding a certain file, you can use the following command.

git checkout -- helloworld.java

this will get rid of any changes since the last commit of helloworld.java

so, how to remove a file from git?

git rm sorry.php

this will remove that specified file, but if you want to remove some files of the same type, use..

git rm '*.php'

be careful though, these commands will remove the files from disk too! once you removed the files, make sure you commit those changes too. (with a nice description of course)

so, before, we talked about something called branches. and…. how to add one? simple.

git branch my_branch

simple as that. to view the existing branches, you can use..

git branch

if you want to see.. aaaaallll the branches, you can add the parameter -a after the above command.

to select a branch, you can use the following command.

git checkout my_branch

to move to your main branch, we call it the master, use..

git checkout master

to merge your branch with the master, you can use..

git merge my_branch

this will seamlessly merger the two branches. if you have any conflicts, git will tell you, those can be handled with accordingly then.

when you’re done with work with a branch, you can remove it by…

git branch -d my_branch

once you make the changes, don’t forget to push, check status and the log as well. just make sure you make them little habits.

yes yes, i understand, github can be confusing at the beginning. but as i am learning, i’m discovering how it can be of use. learning to love..sort of.

there are so many articles and guidelines to help you out with git. so have a play around and figure out things for yourself as that’s how you remember things easily!

to practice i use code school’s git path. you can register there and have go at github before you actually getting it on your computer. don’t be afraid, this will come really handy!

for further use, you’ll need more commands and tools, for that, i shall present you with this awesome git command cheat sheet (thank you lishni! ❤). there you will find all the commands you need. many we haven’t discussed here.

so give it a go, and let me know what you think. we can talk over a coffee eh? 😊☕

so until the next time, have fun gitting around! heh.. gitting… 😋 see ya! ❤

Advertisements

One comment

  1. the20sguy · July 11, 2015

    Bit of an Advice Leave that git passphrase Empty, unless of course you are a security nut. plus if you have your home directory of the workstation encrypted it safe too.

    #####Why Leave it Empty
    it’s a Pain in the Ass .. Every-time you connect to the remote repo. it will ask for the passphrase .. i mean literally every-time.

    Liked by 1 person

spill a bit of coffee?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s