Introduction to Git and Common Git Commands
Introduction
Git is a distributed version control system used to track and manage changes in source code and other project files. It allows developers and teams to work on projects collaboratively while maintaining a complete history of changes.
Git can be used with remote repository platforms such as GitHub, GitLab, and Bitbucket, as well as private Git servers. This article provides an overview of commonly used Git commands, including cloning repositories, configuring Git, creating commits, pushing changes, managing branches, merging changes, viewing commit history, and reverting changes.
Prerequisites
Before working with Git, make sure the following requirements are available:
- A Linux, macOS, or Windows system with command-line access.
- Git installed on the system.
- Access to a Git repository hosted on GitHub, GitLab, Bitbucket, or a private Git server.
- Basic knowledge of Linux/Unix command-line operations.
- A valid Git username and email address configured for commits.
- Appropriate permissions to clone, pull, and push changes to the remote repository.
IMPLEMENTATION
Git Repositories:
Git repositories are the location where you can store the all your project/application code. It could be either on github account or your own private repositories such as gitlab or bitbucket account,etc.,
Clone repository:
Normally, there are two ways to clone the repositories. One is via http protocol or other way is to use SSH protocol.
The below command is to clone the git repository to your local machine using https protocol.
git clone https://gitrepourl/repo.git
This will create a directory called repo on your local machine.
Incase, if 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 setup 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 setup git. Now, we are making changes the files and commit to the repository.
We are creating a file test.html on your local machine and push it to git repository.
vi test.html
Test Content
Now, add the file to git repository
git add test.html
Commit the file with your changes.
git commit -m "Adding test.html"
Add remote repository to origin.
git remote add origin https://gitrepourl/repo.git
Push the changes to the remote origin.
git push origin master
Where
Origin is remote URL
master is a local repository.
Additional Information:
The above example is a simple one which explains on how to push a single file to git repo. In this section, we will explain few more git command which can help on how to 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 checkout to another git branch.
git checkout -b name-of-branch
To merge a branch to master. First Checkout to master branch and merge the branch
git checkout master
git merge name-of-branch
To delete a file from 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 previously saved git commit.
git reset --hard HEAD~1
Conclusion
Git provides a reliable and 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, git log, git stash, and branch-related commands, developers can effectively manage their code and collaborate with others.
The commands covered in this article provide a foundation for working with Git repositories. As you become more familiar with Git, additional concepts such as pull requests, rebasing, conflict resolution, tags, remote branches, and advanced Git workflows can be explored to improve development and collaboration.
