Git ?

Yes, git, you know these strange commands you’ve been using for nearly four weeks now. Unless you’re one of my students, this article will be a short introduction to git, how it works and some features, it will mainly focus on the essentials. However it’s good to know the basics I guess. So here we go.

What is git ?

Nobody really creates perfect code the first time…
Except me, but there is only one of me.

  • Linus Torvalds1

You may not know Linus Torvalds, and if you do, great!
He is a Finnish computer scientist who created the Linux Kernel. But we’re not here today to speak about him, you’re concerned because you want to know more about git, and that’s a good thing.

So what is git ? What stands behind git ? According to Linux Torvalds, it stands for stupid. Okay seriously now, git is a version control2 system for tracking changes in computer files and coordinating work on those files among multiple people. With git you can see what others are working on, you review their code without traveling a thousand miles, you can view your previous changes, you can rollback to your previous code.

There are a lot of commands and I’m going to show you some today.

GiT GuD, discovering the magic behind some keywords

You may know three to five commands today and that’s not a big deal, it’s really good if you really know what stands behind these commands. But I’m sure of something, you don’t even know the most useful one.

Help me, I can’t do this…

❯ git help

As everything, toys, cars, computers, furnitures and anything else, a program comes with some documentations, so git help will be your best ally if you want to learn fast, Google is not the only way. Let’s say you don’t know how git add works ? Just type git help add and it comes right to you with maybe the informations you were looking for, if it does not, spoiler you are seeking at the wrong place. As your second best ally, the man command is there too, it will display you a manual page and it works offline.

❯ git help
❯ man git

So okay, two commands and we’re done, you can do everything you want now, I guess ny job ends here.

Nobody told you to introduce yourself ?

git may want to know you before going further, after all it’s normal, you don’t know each other, I guess it’s better if you tell him who you are.

❯ git config --global user.name "Xavier Login"
❯ git config --global user.email xavier.login@example.com

These two commands will allow git to know your identity, as you may send some of your works on some git servers, you can easily track if you did this or that changes.

A good thing to point is that all this informations are stored in the .gitconfig file located in your HOME.

Getting on the tracks

There two ways to get a repository, and it’s really simple, let’s suppose you have nothing, so you need to init your repository, therefore git init it is. In the other way you already have a git repository, so you want to clone your project, so git clone we have. Note that git clone will always ask a remote3, so you must give it. You can note two commands:

❯ git init
❯ git clone git@github.com/iRyukizo/trantor

How is git ?

Before going further, you want to know the status of your repository, and not just now, at anytime. git status will tell you if your work is tracked or not, it will show paths to files that have been modified. It will display the tracked files in green and the one that are not in red.

❯ git status

Git Status Note that if you commit your changes, the files will be considered as already tracked, and so on not displayed by the git status.

Check what has been done so far

Okay, you can create a git repository, check its status, imagine you are cloning a repository and lots of work has been done before, what if you want to know what has been done or check if your works is right here and you can continue (if we suppose you worked from somewhere else that your current session).

❯ git log

git log will help you at anytime, you can see all of the previous works represented by commits. Git Status As you can see, there are four commits, so I can now be sure that if I pushed more than four commits, my work is not up to date. In the case you pushed less than four commits and you don’t know where it comes from, you should ask you some questions. git log has some cool features, you can check it with git help log.

Commitment

Before creating a commit, you will work on your project and make some modifications, these modifications need to be added to your git repository with git add. Git Status See ? With our example, the file is now considered as tracked and ready to be commited.
So what’s commit ? Suppose you did some works on your repository, and you want to record this changes, you will therefore create a commit with the work you want, you can specify these work with the name of the files you want to add and then commit it.

❯ git add my_precious_work.c README
❯ git commit -m "work: fixed error"

git commit will open an editor for a clearer message.
Two commands and you are the master, your precious work is now saved in your repository as local.
NOTA BENE: It is important to describe your commit well by making the message the clearest as possible.

Local ? I want it to be everywhere

❯ git push

Your work is saved locally and you now want to share it with the world. No problem, git push is here. git push will send your work to the remote3 and after this point you can rest. If you are not sure yet, git log is your friend, let me show you. Git Log Okay, there are two things you need to point, the first one is that when your commit is not pushed, it will appear like this: Git Push And when it is, it will appear like that: Git Pushed See ? The remote3 origin on branch master is at the same state as your local commit.

Up to date please

Let’s assume you work with other people or you work from another place, you need to keep git repository up to date. To do so you have git pull, it will pull from the remote the files if there is and otherwise will declare your repository is up-to-date.

❯ git pull

Miscallaneous

I will show some cool features you should know and which can be very useful in your everyday programming adventures.

Ignore who you want

.gitignore is a file you can drop at the root of your repository and it will tell git to ignore some files. For example:

*.exe
*.o
*.out
bin/
obj/

With this .gitignore, any files with .exe, .o or .out as extension will be ignored. Directory named bin/ or obj/ and their contents will be ignored. Thus git add on them won’t work.

Show me the difference

❯ git diff

Imagine you have changed the content of some files, but you didn’t know what was initally in it. Just call git diff and you will now know. Git Diff Minus will represent the remote state and plus the local state.

Let the color out

❯ git config --global color.ui auto

If you want to have a colorized output, just enter this command.

What did I just do ??

❯ git reflog

git reflog is the most useful thing you should know right now. Imagine you are not sure about the action you just did, you want to debug it, but you have no memory and please stop scrolling you shell history, there is something better. Git Diff You can see the two last actions was commiting.

Recap

I will try to recap each commands I introduced you and their purposes.

  • git help, whenever you have a question, ask him.
  • git init, create a git repository from scratch.
  • git clone, clone a repository.
  • git status, check the status of your repository.
  • git log, show the list of all the commits.
  • git add, add files in order to be commited.
  • git commit, register your work as a commit.
  • git push, send your work to the remote or in a more familiar way “online”.
  • git diff, show you the difference between your local files and your remote files.
  • git reflog, show you all the directives you used.

That’s all ?

Not really, git has a lot of features, and it is too soon to speak about it. I may talk about the use of branch in git later, but as I said all the features I just showed seems essentials to be knowed as an undergraduate student.

Questions ?

Feel free to contact me on discord to ask me some questions or suggest to add things I may have missed and you think is essential.

Links and more

References


  1. Tech Talk: Linus Torvalds on git, https://www.youtube.com/watch?v=4XpnKHJAok8&t=1340s ↩︎

  2. version control: https://en.wikipedia.org/wiki/Version_control ↩︎

  3. Git remote, https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes ↩︎