ShadenCodes Logo

A Step-by-Step Guide to becoming a Git Expert

Git is the best and most widely used version control system in the world so far. It’s a very well supported open source project with over a decade of solid management that meets the long term needs of most software teams and individuals worldwide. And guess what! It’s free.

cover

What is Git?

Let’s make it simple and clear, Git is a software that helps you track the changes made to a project over time. It stores these changes and allows you to reference them when needed.

Mastering Git is not as hard as you might think! It’s all about understanding the concepts behind everything it offers. Let’s Start!

Basic Git Workflow

Initially, enter your local project directory and run the following command:

$ git init

Init command will create a new git repository for this project. This local repository will be created as a ‘.git’ folder inside your project’s root directory so you will be the only person allowed to work with it by editing and committing changes.

Take a look at the following workflow chart:

Basic Git Workflaw

The local Git project consists of three parts:

  1. Working Directory: where you will be doing all the work: create, insert, modify, add, delete, copy, rename and organise files.
  2. Staging Area: where you will list all the changes that you want to commit.
  3. Repository: where the changes will be permanently stored as a new version.

After editing the files in the working directory, use the following command in order to pass the changes you have made to the Staging Area:

$ git add filename

‘filename’ refers to the name of the file that you want to add.

You can always track the difference between the working directory and the staging area by using:

$ git diff filename

The last step of the basic Git workflow is to commit the changes. A commit will permanently store all the changes from the staging area into the repository.

$ git commit -m "[descriptive message]"

Make sure to describe what this commit is all about by writing a clear message wrapped with quotation marks.

This is all you need to know for the basic Git workflow! knowing these simple commands and mastering them will give you the ability to track changes in your local repository easily and professionally.

Log

From time to time, you would like to take a look of what happened so far in your project. This is where the log command comes to help:

$ git log

Branching & Merging

In real software project, there are always multiple contexts where developers work. You might have to develop new feature and also fix some troubling bugs, or maybe you need to work remotely with other teammates each in different feature.

The best way to do this is by creating a new branch for each different context and keep the development process isolated. This will also gives you the freedom to experiment new things without worrying about what might happen to the project, since you can keep the original branch untouched and working perfectly.

newbranch

In order to know in which branch you currently exist, use the following command:

$ git branch

To create a new branch, use:

$ git branch new_branch_name

‘newbranchname’ is the name of the new branch you would like to create, it should be related to the purpose of the branch.

The new created branch is identical to the current branch. In order to start working on our new branch, we probably want to switch to it:

$ git checkout branch_name

After we are done with developing the new feature and we want to include it in our main branch, we can easily accomplish this by using the merge command:

$ git merge branch_name

merge

In case the main branch hasn’t changed since the time we created the new branch, the merge process will be completed successfully. Otherwise, if the main branch contains new commits which are not included in our new branch, we will face conflicts. In this case, Git will need your help to decide what changes to keep, and what to dismiss.

After the conflict occurs, you can open the files manually and edit them the way you want them to be, add the files to the staging area and finally commit the changes.

After the development process of the new feature is done, and the new changes are merged into the main branch, there is no need to keep the new branch. You can simply remove it by using the following command:

$ git branch -d branch_name

Branching is one of Git’s most powerful features, it is fast and easy and there is no reason not to use it! If you aim to enhance your everyday workflow, use it extensively!

Remote Repository

When it comes to working with a team, remote repositories come into play. A remote is a shared Git repository that enables multiple collaborators from different locations to work on the same Git repository independently.

In case you get on board for an existing project, you can have a copy of it in your local machine by using the clone command:

$ git clone remote_location clone_name

‘remote_location’ is the location of the remote repository, it can be a web address or file path. Git will give the remote address the name ‘origin’ so you can easily refer to it.

Git Collaboration Workflow This is the typical Git collaboration workflow that you should always follow:

  1. Fetch new changes from the remote and merge them into your branch.
  2. Create a new branch for your work (new feature, bug fixes, …).
  3. Commit your work.
  4. Fetch and merge again.
  5. Push your branch to the remote repository.

You might be wondering why step 4 is needed. When you work with a team, other teammates might update the remote while you are working locally. This is why it is always an important and smart step to make, always fetch new changes before you update the remote.

To fetch new updates, use the following command:

$ git fetch

You’ve got the new changes, but your local branch has not been updated yet. Use the merge command we’ve mentioned previously in order to integrate the new branch into your local branch.

And, for the last step:

$ git push origin your_branch_name

This command will push your branch to the remote so other teammates can see it.

That’s it!

Congratulations, you now know all you need to start working with Git! Mastering Git is one of the most powerful and essential skills that every developer should have. I hope that you enjoyed reading this guide and as always, if you have any questions, do not hesitate to leave a message.

In a future article, I will talk about Github! Subscribe to my blog to make sure you don’t miss it.

Created by @ShadenCodes, © 2020