Post Date: April 16, 2017
Last Updated: September 28, 2026
Introduction
Git is a distributed version control system used to manage and track changes in source code and other project files. It allows developers to work collaboratively, maintain different versions of a project, create branches, and synchronize changes with remote repositories such as GitHub, GitLab, or Bitbucket.
This guide explains the basic Git commands required to clone a repository, configure Git, add and commit files, push changes to a remote repository, manage branches, view commit history, and perform other common Git operations.
Prerequisites
Before working with Git, ensure that the following requirements are met:
- A Linux system with administrative or sudo access.
- Git should be installed on the system.
- Access to a Git repository hosted locally or on a remote Git service.
- Basic knowledge of Linux command-line operations.
- A configured Git username and email address.
- Appropriate permissions to access and push changes to the remote repository.
- For private repositories, the required authentication method, such as HTTPS credentials or SSH keys, should be configured.
Implementation
Git Repositories:
Git repositories are the location where you can store all your project/application code. It could be either on a GitHub account or in your own private repositories, such as a GitLab or Bitbucket account, etc.
Clone repository:
Normally, there are two ways to clone the repositories. One is via the HTTP protocol, and the other way is to use the SSH protocol.
The command below is to clone the git repository to your local machine using the HTTPS protocol.
git clone https://gitrepourl/repo.git
This will create a directory called repo on your local machine.
In case we receive an error git command not found, we need to install git on the machine.
For Ubuntu,
apt-get install git
For CentOS,
yum install git
Now, we can set up Git on the machine.
git config --global user.name "Your Name"
git config --global user.email "your-username@domain.tld"
Git Commands:
So far, we have cloned the repository and set up Git. Now, we are making changes to the files and committing to the repository.
We are creating a file test.html on your local machine and pushing it to the Git repository.
vi test.html
Test Content
Now, add the file to the Git repository
git add test.html
Commit the file with your changes.
git commit -m "Adding test.html"
Add the remote repository as origin.
git remote add origin https://gitrepourl/repo.git
Push the changes to the remote origin.
git push origin master
Where
Origin is the remote URL
master is a local repository.
Additional Information:
The above example is a simple one that explains how to push a single file to a Git repo. In this section, we will explain a few more Git commands that can help you use git effectively.
To show recent changes, current commits, etc.
git status
To view the remote repository.
git remote -v
To view the git log and commit IDs.
git log
To stash all the local changes
git stash
To create a new branch from master
git branch name-of-branch
To check out to another git branch.
git checkout -b name-of-branch
To merge a branch into master. First, check out the master branch and merge the branch
git checkout master
git merge name-of-branch
To delete a file from a Git branch. First, delete the file, commit, and push the file.
git rm test.html
git commit -m "Delete from git"
git push origin master
To move to a previously saved git commit.
git reset --hard HEAD~1
Conclusion
Git provides an efficient way to manage source code and track changes throughout the development lifecycle. Using commands such as git clone, git add, git commit, git push, git status, and git branch, developers can manage repositories and collaborate effectively.
The commands covered in this guide provide a foundation for working with Git repositories. As projects become more complex, additional Git features such as branching strategies, remote repository management, merging, rebasing, and conflict resolution can be used to manage development workflows more effectively.
FAQs
1. How do I clone a Git repository?
You can clone a remote Git repository using the git clone command:
git clone https://gitrepourl/repo.git
2. How do I check the current status of a Git repository?
Use the following command to view modified, staged, and untracked files:
git status
3. How do I create and switch to a new Git branch?
You can create and switch to a new branch using:
git checkout -b name-of-branch
This creates the branch and switches to it immediately.
Related Articles
Talk to our experts
Looking for the right technology solution for your business?. Our experts can help with web hosting, domain registration, DevOps and cloud, software development, web applications, mobile applications, and enterprise solutions. Get in touch with our team here.