Wednesday, June 14, 2017

Hasura Internship - 2.2 The wonders of git

Git is a magnificent version control tool. What does that mean? 😅

Let's do a tiny insignificant project to get the basics out of the way.

Start a project

$ mkdir ~/Projects/chicken_soup
$ cd ~/Projects/chicken_soup
$ touch recipe.txt

This project will contain only a text file, having a silly recipe for chicken soup 😎
With this, we have a base project structure in place.

Asking git to track this project

$ git init

When we do this, we see that there is a .git folder magically appearing on our project folder. This is where git stores all relevant information to file changes in our project. If we have not configured git, now is the best time.

$ git config --global user.name "superman"
$ git config --global user.email "kalel@kryptonmail.com"

Let's write something to our recipe file.

$ echo "1. Get a chicken gutted." > recipe.txt

We want git to know that recipe.txt is a file it should track within our project. So we do

$ git add recipe.txt # or git add . 
# the alternative will keep track of all files in the folder

Our first commit

$ git commit -m "My first commit"

This tells git that we want to update our additions and git should consider this as a check point. Each commit will be a separate checkpoint in our project. The -m is simply a way to pass a message along with our commit, so that we can understand the commit logs later.

$ git log

Will show us that our change has indeed been tracked. That's it 😉

Let's do that again

$ echo "2. Apply masala over it." >> recipe.txt
$ git add recipe.txt
$ git commit -m "applying masala"

Now if we do a git log, we can see 2 entries. Do note the weird number strings, like 2edfr5t0fd649d774ca3fb77614bde9a62axsd9 in my case. Also check the order in which they appear. The one at the top, is our latest commit.

Yikes, I screwed up. Abort

If I want to reset the file to the first commit, we can do that

$ git reset --hard bd959d6a0fd649d774ca3fb77614bde9a61f64f9   

Now if we look at the contents of recipe.txt, we see just the one line. Of course this isn't earth shattering. Try adding a few files and add content to these files. This is where git shows its magic.

Onto the internet

Github is a service on top of git that takes our projects and gives it an online presence. After we register for a github account, we can add our chicken soup project to it.

The following is our projects URL after we go through the initial stages of adding a new project to our github account.

https://github.com/superman/chicken-soup.git

From our project folder in our machine, do

$ git remote add origin https://github.com/superman/chicken-soup.git
$ git push -u origin master

This will set our projects repo url and push our code to github. If we go to  https://github.com/superman/chicken-soup in a browser, we can see our project. 

Let's add some more changes

$ echo "100. Eat chicken soup." >> recipe.txt
$ git add recipe.txt
$ git commit -m "Final step added"

We push these changes to github

$ git push -u origin master

So, later we're on our friend's machine and we want to show what we made. Assuming that our friend has git installed, a

$ git clone https://github.com/superman/chicken-soup.git

will download the entire project to the friends machine.

Conclusion

This was just a rudimentary intro. An awesome book on git can be found here



0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home