How to Git Good at Git

Rayhan Arwindra
Pilar 2020
Published in
8 min readOct 8, 2020
Photo by Pankaj Patel on Unsplash

If up to this point you were a lone developer, you might have never even thought of needing to use git. However, when working in a team, you would definitely need a tool to control work between multiple developers within the same project. This is where git comes in, as it will allow you to track the version of your application, and coordinate work between you and your fellow team members.

What is Git?

Git is a version control system, specifically, it’s a DVCS (Distributed Version Control System). With git, you can commit your work into a repository online. Your coworkers could then clone your repository, branch from it, merge to your branch, and many more. Git does all this while maintaining great performance, making it the most popular modern version control system to date.

How to use Git?

Now let’s get into the fun stuff. To use git, you would first need to install it. Installing git is different in every operating system, so please view the website here, and find the guide which is most appropriate to your personal OS.

After installing it, we can begin using git. Create a directory, navigate to that directory using the command line or shell, and type:

git init

What this command does is it creates an empty git repository within the current directory you’re in.

Next, let’s create a file within this directory. To make it simple, let’s just create a simple text file called “file.txt”, like so:

Your layout should be identical to this if you’re on windows.

Feel free to type whatever you want within that file. But now, our repository isn’t so empty anymore. So how do we make git keep track of this change? We simply type within our command line:

git add .

That adds all newly created files to the index. The index holds a snapshot of the next commit. Speaking of commit, let’s do that with:

git commit -m [COMMIT_MESSAGE]

You can add whatever message you want within COMMIT_MESSAGE, just make sure it is descriptive of your current commit.

For example, since we just added file.txt, our commit message could be “Added file.txt”, so our command would be:

git commit -m "Added file.txt"

That’s it! You’ve committed the most recent change of your “project” to your git repository.

However, that is only on your local repository. How can we make an online repository which also keeps track of our little project here?

Remote Git Repository

There are two popular web-based git repositories that I recommend. Those being GitHub and GitLab. If you haven’t done so already, please make an account on any of the two websites.

After that, you would need to create a new repository, feel free to give it any name you wish. If you’re done with that, then copy the HTTP link which should be on the top right of your repository, like so:

Depending on whether or not your repository is empty, it might also be found under the code button like so:

Then, on your local repository, open your command line once more and type:

git remote add origin [LINK_TO_REPOSITORY]

This adds our web repository as a remote repository and calls it origin. Finally, on your command line, type:

git push origin master

This now pushes our local repository to the origin remote repository, which is the web repository we created.

Now if you refresh your repository, you should see “file.txt” which we created on our local computer. Well done!

Branching

On our last command which was:

git push origin master

You might be wondering what “master” is. Master is a branch on our repository, it is the first and default branch created when we initialised our repository in the very beginning.

So what if we wish to create another branch, say one branch for each and every developer on our team?

We can do that with the command:

git checkout -b [BRANCH_NAME]

Feel free to give your branch any name you wish, you could also name the branch your own name if you want. I’ll call my branch “Ray”, so my command would be:

git checkout -b Ray

If you wish to go back to the master branch, you can simply type:

git checkout master

That’s right, the “-b” flag in the checkout command is for creating a new branch. If you don’t add the -b flag, you will simply just switch to a different branch (if it exists).

You can check what branch you are on by typing:

git branch

The apostrophe on master means that I’m currently on the master branch. For now, let’s switch back to the new branch we just made.

Now let’s add something new in our current branch. You can create a new file, or simply add more text to our file.txt file.

Afterwards, we need to repeat the previous procedure of committing into our local repository, by typing:

git add .

And also

git commit -m [COMMIT_MESSAGE]

Finally, let’s push our commit into our remote repository by typing:

git push origin [BRANCH_NAME]

And now if you switch branches on your repository by toggling the button like so:

You should see your latest commit from your new branch. Next, let’s switch back to our master branch on our local repository by typing:

git checkout master

And then pull from our new branch by typing:

git pull origin [BRANCH_NAME]

Now on our local master branch, we have the new changes which were previously only available on our other branch.

Alternatively, we could also merge our branch with the master branch, by first switching to the master branch, and typing:

git merge [BRANCH_NAME]

If we do that, we don’t need to push and pull from our branch to our remote repository.

Revert and Log

Let’s say that the most recent commit from our other branch is problematic. It might cause errors on our master branch after pulling, or maybe we simply don’t like the recent changes made on the other branch.

This is where the version control in git comes in. If you recall, in the very beginning I stated that git tracks our application’s versions.

We can view all of the versions or commits by typing:

git log

The output should be similar to this:

Looks complex? Let’s discuss each part one by one:

The yellow text is your commit id, you will need it later on for what we are going to do. Below that is you, the author. Then the information beneath you is the time and date of that commit. Finally at the very bottom is the commit message.

Now that we understand the logs, let’s get back to our objective, which is to get rid of our latest commit and go back to our previous one.

First, take note of the commit id we wish to revert, and copy it. In my case, it’s the commit with the message of “Added text to file.txt”, and the commit id is:

a9dccd563efa30e2c328d511f0080f91b75d669e

Finally, paste it into the command:

git revert [COMMIT_ID]

What does the revert command do? It basically creates a “mirror” of the current commit.

So if you added a file, it would remove it, if you removed a file, it would add it, and so on. In a way, it reverses the commit to the previous one.

If you run into a problem while reverting, simply type:

git status

And resolve the issue. Usually, the way to resolve the issue is to remove or add files so that the reverting process can be accomplished.

Other Commands

Although the commands we’ve discussed should be more than enough for you to get started on using git, there are other commands out there that you should know of.

  1. git clone

Should you or your coworkers already have a non-empty remote repository, and you wish to get all the data (along with the remote origin) of that repository, you can use this method.

The syntax of this command is:

git clone [LINK_TO_REPOSITORY]

2. git rebase

Similar to merge in terms of its objective. Rebase is a way to fast forward your branch to another branch. By using rebase instead of merge, we can achieve a more “linear” branching. See the illustration below:

Source: https://medium.com/datadriveninvestor/git-rebase-vs-merge-cc5199edd77c

Not familiar with the above illustration? That is a visualization of git branches, we do this in order to understand how our commits and branches look like and behave within git.

If you wish to play around with git branch visualizations, you can visit the website here for a more interactive learning experience.

The syntax for rebase is:

git rebase [BRANCH_NAME]

3. git stash

The stash command is used to store away uncommitted changes to git’s temporary memory. There are a couple of main commands for git stash:

a. git stash save

Will save the changes into temporary memory. The syntax of this command is:

git stash save [STASH_NAME]

Where STASH_NAME can be anything you want.

b. git stash list

Displays the list of stashes you’ve saved. The command is simply:

git stash list

c. git stash pop

Removes the topmost stash from the temporary memory, and adds it back to the project. The command is:

git stash pop

d. git stash apply

Returns a specific stash into the project, without removing it from the temporary memory. The syntax of this command is:

git stash apply [STASH_NAME_AND_ID]

You can see the stash name and id by using the git stash list command.

Conclusion

As a developer in this day and age, it is almost certainly a necessity to learn how to use git. Almost all companies and software projects use it, as it is the fastest and most effective tool to coordinate software projects between team members.

With the knowledge you learnt in this article, you will hopefully be able to start using git in your projects and be on track to become a professional developer in the modern-day.

--

--