I never written a line of code. So what is Git? Pt.1

Fire escape procedure for Git users. No, seriously.

Before I begin, this article explains the most fundamental of stuff of Git, how to use and why you should use it. So if you’re new to coding, this article is just right for you 👍 Let’s Git started! :


OK. So what is Git?

Git is a version control system for tracking files changes in your computer.

And what is version control system?

Imagine you are spending hours of coding to fix an issue on your project. The code fix works and issue has been gone. Hooray~! Time to grab a beer 🍺 to celebrate. Unfortunately something nasty happens and your computer crashes. When your computer boots up, seems like the changes are not there anymore. Your hours of effort gone and probably you need put that beer down and redo everything.

Now if you have been using version control system, code fix checked ✅, issue fixed ✅ and then you save your changes in the another computer which is call a server ✅. Nasty stuff happens, no problem. Simply download your recent changes back to your local computer ✅. Sit back and enjoy your beer 🍺.

Another example could be you have a good version of a file. Then you make some changes here and there and your project breaks 😢. So you keep pressing CTRL + Z multiples times but seems unable to retrieve back that good version of the file. Now with a version control system, you have saved snapshot of the file version before you do your spaghetti changes, all you need to do is simply rollback and you’re good to go at it for round 2.

In Git we trust!

Git and Github??

Now I am going to introduce you to another term call Github. And no, Git and Github are 2 different things 😕.

Again Git is a tool for you to track changes. Github on the other hand, is a web-based git repository hosting service. In other words, Github allows you to keep your code in the server and track it using git.

You can always use Git without Github. But then all your copies of the code is in your local computer. If you wish to have a backup and store in server, then you need Github. Also with Github, you can share your project with your friends, or even working together 💪.

Let’s Git Started

  1. First we need to create an account in Github

2. Then we need to create a repository (basically a project name) . Description is optional. You can set your repository as Public or Private as what the description in the snapshot shows. For Public, anyone can view your project and also make changes to it (aka Commit)but only admin (which is you) approves or rejects this changes. This will be our remote repository.

3. Once repository is created. Copy the link highlighted in blue below. We will be needing this later 😅

4. At your local computer 💻, create an empty folder (in this case im using GitLearning_Local). Launch terminal, change directory to this folder using command: cd <foldername>

5. We then use command git init to ask git start tracking this project. This will be our local repository. Remote and local repository, get it?

git init

6. We then need to tell git the link to our remote repository (link is from step 3). Simply run command git remote add origin <link>. We then use git remote -v to confirm the correct link

git remote add origin <link>
git remote -v

7. Now that our remote and local repo has been set up. Time to take a tea break 🍵. Pat yourself on the shoulder 👏. Now it’s time to enjoy the benefits of git. Let’s create a new file call file1.txt with content “content 1”. If i use git status , i should able to see the message

git status

8. Now Git is telling you that there is an untracked file. We should add this for Git to start tracking the changes in this file. [In Git language, we call this add to staging area] Hence the command, git add <filename> to add for tracking. If you have multiple untracked files, you can simply use git add . which means add all the files. [FYI 🤔, to UNDO git add, use command git reset <file>, or git reset all changes]. Let’s try git status again and now Git is telling in the staging area, there is a new file.

git add <filename>
git add .

9. We are ready now to save our changes to our local repository. But we need to attach a commit message that describes what this change is all about.

git commit <file> -m "<message>"

10. Finally we are just one command away from saving our changes to our remote repository.

git push origin master

11. Now if we refresh our remote repository in Github, the file should show up there.

Well done! You have successfully create a new file in both local and remote repo

12. Now let’s say we have an updated version of file1.txt with content “version 2”. If we run git status. We should see the status below: Git is telling us we have modified our file1.txt, and we should either add to staging or discard changes using git checkout

13. We can use git diff <filename>, a handy command to see the difference between updated file and the saved in local repo.

git diff <filename>

14. What happens next? Well you know the drill: git add — commit — push! Now if you enter git log <filename>, you should be able to see the history of changes made to the file: You might be wondering what is the random value appear next to commit? Well each individual changes has its own commit hash which allows them to differentiate from each other.

15. Imagine that you have made a mistake in version 2 of file1.txt and you would like to roll back to the previous version. You can remove the last commit by simply running git revert HEAD and don’t forget push to remote repo.

git revert HEAD
Graphical illustration of basic Git commands

There you go, we have covered the basic bread and butter of using Git. Of course there are more handy commands that will be covered in Pt.2. But I hope this step by step tutorial is beneficial 💪for those who have never heard or use Git.

If you like this articles and would like to see more of it in future, hit the clap 👏 button below!

Thanks for reading! 😃

About the author

Founder of tattweicheah.com. Loves music, sport and most importantly software development.

Leave a Reply

Your email address will not be published. Required fields are marked *