Early bird prices are coming to an end soon... ⏰ Grab your tickets before January 17

This article was published on September 2, 2020

A beginner’s guide to the most popular Git commands

Learn the most popular git commands and a simple but effective branching model


A beginner’s guide to the most popular Git commands Image by: Unsplash: Ilya Pavlov

So you like TNW? Then join our upcoming online event, TNW2020, you don’t want to miss it.

Software developers usually write tons of code every single day. They might be working on a new project or tweaking an existing one. But, after some time the codebase may grow enough that it becomes very difficult to manage or track any changes made.

For example, if a team of software developers are working on an application, how can they track the team member who made a specific change?

It might not be a major issue if a team only consists of two or three members. But, just imagine if they’re around 10, 20, or even 1000 developers.

The 💜 of EU tech

The latest rumblings from the EU tech scene, a story from our wise ol' founder Boris, and some questionable AI art. It's free, every week, in your inbox. Sign up now!

[Read more: We asked 3 CEOs what tech trends will dominate post-COVID]

This might all seem a little daunting, but this is where a Version Control System (VCS) comes into play.

Before executing any commands I think it’s better to first talk about the concept of VCS and Git. It will help you understand why we need it and how can it improve an existing workflow.

What is a version control system?

VCS is a software that automatically maintains a record of every change in a project. This way, a team can focus on creating an actual product instead of dealing with non-productive tasks like who modified a specific file.

Also, as VCS is a computer software so it’s more accurate and faster than us. Anyways, let’s have a look at different types of Version Control Systems.

  1. Centralized Version Control – As its name suggests, in the Centralized Version Control system a project is stored in a single/central location (e.g. a server). Here, a team member can download files that they want to change and after doing the work simply upload them to the server.The most popular CVC software is known as ‘Subversion.’
  2. Distributed Version Control – In a Distributed Version Control System (DVCS) every team member has access to a full copy of the project on their local computer. It doesn’t require a central server but you might need one to easily collaborate with your teammates.These days, the concept of DVCS is very popular due to the fact that it enables us to work on a project without worrying about an internet connection. We only need to connect whenever we want to upload changes to the server.Git is an example of a Distributed Version Control System which you will learn today.

Overview of Git

Git is the most commonly used Distributed Version Control System. It keeps a record of every change in a project by taking snapshots. It means you can easily roll back to any previous version/state of a project.

In 2005, Linus Torvalds released the first version of Git. Also, Torvalds also created the Linux operating system.

At first, Git was intended for use by software development teams. But, it’s flexible enough to track any kind of file. So, no matter your profession, whether you’re a teacher, businessperson, graphic designer, or a content writer, Git can help you automatically track your projects.

Collaborate with teammates

In this section, you’ll learn how team members can use Git for collaboration. Basically, I’ll help you understand the actual workflow of Git. Later on, you’ll see how Git provides us predefined commands for each purpose. So, let’s get started:

  1. Create a “Repository” on the central server. A repository is just a folder that is being tracked by Git software.
  2. Download the repository on your computer. In terms of VCS, we usually refer to this step as “cloning”.
  3. Add files in this repository and commit them. Git takes a snapshot of the project whenever you commit something. It then maintains a history of snapshots for later use.
  4. Upload the modified files on the server.
  5. Download any files that are modified by your teammates.

So, this is the basic workflow of any project that uses Git for version control. Now, let’s see how we can actually implement these concepts using Git commands.

Create a repository

By default, Git software doesn’t track each and every file on your computer. We have to inform Git about the specific folder that it must track.

To do so, open your command prompt or terminal inside the specific folder and execute the below command.

This command converts the current directory into a repository. Basically, it tells the Git software to start tracking all files and folders inside this repository.

A point to be noted is that all Git commands start with the keyword git.

Working directory, staging Area, and repository

Basically, a Git development environment consists of three sections. The concept behind these sections is of key importance. So, pay extra attention because everything else will depend on this.

  1. Working directory simply refers to the current state of files and folders inside your file system. At this point, Git doesn’t track these files.
  2. Staging Area – Before saving any file to a repository, you have to place it in a staging area. It’s like a temporary location for your files and folders before commit. You can easily add or remove files from a staging area.If you’ve modified any files and you want to add them in a staging area then simply execute the below command.
    Here, the dot means that all files are modified.
  3. repository holds your actual committed files. Git stores all this information inside a hidden folder called .git. When you commit something, whatever inside your staging area is permanently saved in a repository.You can think of a commit as a checkpoint. So basically, Git compares your previous checkpoint with recent commit to only store the modified files. You can run the below command with a brief message about what is changed.
    Commit messages are really helpful as it informs other team members about what you did in the commit.

Check the status

Git allows you to easily check which files are tracked/un-tracked inside a staging area. To do so, you can use the below command:

Many people use it to get the current status of the staging area.

Connect local repository with server

In a real-world scenario, you may find it easier to first create a Repository on a Git hosting provider (e.g. GitHub or Bitbucket ). Then, simply clone it to your local computer. The reason is that they automatically set the remote for you. Meaning your local and remote repositories are connected by default.

But, in case you’ve created a repository on your local computer and then want to connect it with a server. Then, run the below command.

You can specify anything as a [remote_name]. But, conventionally we name it as the origin.

Whereas, remote_url is the path to your Git repository on the server. This URL will be provided by your Git hosting provider.

Here’s an example of how this command will look in a real-world scenario:

In case, you want to check how many remote repositories are connected with your local repository. Simply use the below command:

Push changes to server

Now you have connected your local repository with a remote one. It’s time to upload changes to the server. Have a look at the syntax of this command:

It requires the name of your “remote” and “branch” where it will push the changes. By default, we only have one branch which is known as “master”. You’ll learn about branches later in this tutorial.

So, an example of this command will look like this:

Pull changes from server

In case your teammates are also working on the same project and they have pushed some changes to the server. You can retrieve those changes by using this command:

For example:

By default, when you run this command, Git downloads the modified files and then merges it with your repository. But, if you just want to check whether something is changed on the remote repository or not then use this command instead:

Branches

The Git branching model is similar to that of a tree with branches. They’re particularly useful when you want to add a new feature or fix a bug in your project.

Basically, you create a new branch to separately test something. When your work is done then simply merge it with your main branch (i.e. master). In case you don’t like the new idea then you can easily discard the changes without affecting the project.

Use this command to create a new branch:

You can also delete a branch using Git by using this command:

To make any changes in a specific branch, you have to move inside it first. To do so, use this command:

Merge

Let’s suppose you have made some changes to a new branch and want to integrate them with your master branch. You can do that by performing a merge operation. Simply use this command:

In simple words, this command is used to combine two branches. If a specific file is changed in both branches then it will create a merge conflict. In that case, you have to manually look into the file and select the change you want and remove the other one.

You just saw how easy it is to start tracking the history of a project using Git. It is a tool that is generally used by programmers and software developers. But, you don’t need to know anything about software development to learn the Git software. It’s packed with commands and has a very simple syntax that anyone can understand.

This article was originally published on Live Code Stream by Juan Cruz Martinez (twitter: @bajcmartinez), founder and publisher of Live Code Stream, entrepreneur, developer, author, speaker, and doer of things.

Live Code Stream is also available as a free weekly newsletter. Sign up for updates on everything related to programming, AI, and computer science in general.

Get the TNW newsletter

Get the most important tech news in your inbox each week.

Also tagged with


Published
Back to top