banner

Blog & Details page

blog_post
Wong Thian Soon

Posted on 20 April, 2023

Using Git On Command Line

Git is the go-to source code management tool for many developers.

Git is the go-to source code management tool for many developers. Its decentralized nature makes it a wonderful tool when working with a team. It is a powerful tool that includes a lot of features.

In this blog post, I am going to show you how to use some basic git commands on the command line.

This blog post is intended for audience with little to no experience in using git on command line.

Clone

The first thing when you start working on a codebase is to clone a repository.

  • A repository is where all source codes are stored and managed.
  • Cloning a repository means to make a copy of a repository on your local computer.

The following command can be used to clone a repository:

git clone repository-url

Make Changes

When cloning is done, you can change your working folder to where the repository is and start looking around and make changes.

All changes are still in your computer, so feel free when making changes.

Correct Mistakes

If you think you have made a mistake and it is a big one, spanning multiple files, you will want to restart from where you last saved.

Use the restore command to discard all unsaved changes.

git restore .

(Notice the full stop mark at the end. It represents the current folder.)

The command will take you back to when you last started editing by restoring the current folder. It is useful when we want to experiment.

Status

You may view your changes with the status command.

git status

The command lists files that have been added, edited, deleted as well as those that will be included when you commit them, which I will show you in the next 2 sections.

Add

If a change looks good to you, you’ll want to include it in your next commit. Use this command:

Use the restore command to discard all unsaved changes.

git add filename

If there are a couple of files that you want to include and they all reside in the same folder, you may use git add folder-name instead. This will include every changed file in folder-name.

Commit

To put it simply, a commit is a list of changed files. Usually, we make a commit when we think the changes up until now look good and we want to save them.

You can commit the added files by using:

git commit

Your terminal application should show you a text editor and ask for a commit message. A commit message is just a note about a commit.

Type in something meaningful to this commit, save it and quit the text editor.

Now, a new commit has been created in your local repository.

Push

When you are satisfied with your commits, you’ll want to publish them so your teammates can see and review them. Git push means to publish all changes between your last publication and now to the repository.

git push

Conclusion

In this tutorial, we showed you how to get a copy of source code that you want to work on, make some changes, save them and share them with your teammates, all from the command line. Good job!

Although there are many graphical git tools out there, understanding how git works on the command line gives us some insight into how it actually works. It is helpful when a GUI git tool show some cryptic error message.

If you want to learn more about a command, using the following command will show you the manual page of that command:

git command --help

HTML version is available on git’s official website: Git Reference.

30 Day Free Trial

Get hands-on experience with ThirdEye for 30 day free of cost and assess it
by using our evaluation license.