Using Git On Command Line
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.
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
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.
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.
You may view your changes with the status command.
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.
If a change looks good to you, you’ll want to include it in your next commit. Use this command:
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.
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:
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.
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.
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.