Mastering Git: 18 Essential Commands for Becoming a Version Control Pro
The Top 18 Commands You Need to Know for Effective Version Control
Git is a popular version control tool that makes it easier for developers to collaborate effectively on a project. It enables multiple developers to collaborate on the same codebase without eliminating one another's changes. To properly take advantage of Git's potential, one needs to grasp a few key commands that can turn them into version control experts.
We'll go through some important Git commands in this article to help you become an expert in version control.
1. git config
To configure Git in your local machine, the
git config command is used. It enables you to set up your name and email, define aliases for frequently used commands, and set various preferences.
#To display the current configuration
git config --list
#To set up your name and email address
git config --global user.name "Your Name"
git config --global user.email "firstname.lastname@example.org"
#To define an alias for frequently used commands
git config --global alias.co checkout
git config --global alias.co checkout will create an alias "co" for the "checkout" command. So, now you can type "git co" instead of typing "git checkout".
2. git init
A new Git repository is set up using the git init command. In the current directory, it generates a new .git subdirectory that contains all the files required to manage the repository. To create a Git repository, this command must be used once at the beginning of a project.
Imagine you wish to use Git for version control when starting a new software project. The first thing you would do is to initialize a new Git repository using the
git init command in the directory (Let's say ~/Desktop/my-project) where you want to keep that project. So, you need to go to that directory and run the following command to initialize the Git which will only create a local git repository.
3. git remote
If you want to push your commits to a remote repository like GitHub, GitLab, or others, the first thing you need to do is to create a remote repository there and then configure the local repository you have just created to push to that remote repository. To do this, you need to run the following command:
git remote add origin https://github.com/<your-username>/my-project.git
This command will add a remote called "origin" to the local repository, which directs to the URL of your remote repository on GitHub. After executing this, you can push your local changes to the remote repository by running
If you want to remove the local Git repository from a remote origin repository, you can use the following commands:
git remote remove origin
To check if there are any remote repositories associated with your local Git repository, you can use the following command which will list all the remote repositories associated with your local Git repository.
git remote -v
4. git add
To add files to the staging area, use the
git add command. Changes are temporarily held in the staging area before being committed. Every time a file needs to be committed after being modified, this command needs to be run.
Let's say you have a project that contains the file named main.txt. You wish to commit the file's modifications to your Git repository because you made some changes to it. But, you must first add the file to the staging area using the
git add command before you can commit the modifications.
git add main.txt
In case you have modified multiple files in your local repository and want to commit all those files to your Git repository, you can run the following command:
git add .
5. git commit
The local repository is updated using the
git commit command. It is used to create a snapshot of the staged modifications along a timeline of a Git project's history. Each commit includes a message that details the modifications made.
git commit -m "Added new content to main.txt"
The -m option allows you to add a commit message that details the changes made in the commit. It is recommended that the commit message should be descriptive and summarize the changes you made.
6. git push
To submit local repository changes to a remote repository, the
git push command is used. After a commit, this command must be executed to make the changes available to other developers.
git push origin master
7. git pull
git pull command is used to update the changes from the remote repository to the local repository. Before making any changes to the local repository, this command must be executed to make sure it is up-to-date.
git pull origin
8. git status
git status command displays the local Git repository's current status. It displays information about any deleted or untracked files as well as changes made to the local working directory, staging area, and repository.
9. git clone
A copy of a remote repository can be made on a local machine using the
git clone command. When creating a new development environment or working on a project with multiple developers, this command is helpful.
git clone <URL of the repository>
10. git branch
You can create, list, or delete branches with the
git branch command. It allows the developer to work on different features or versions of the project without affecting the main branch. To start a new branch or change branches, this command must be used.
To list all the branches we can use the following command:
git branch -a -v
To delete a local branch, you need to run the following command:
git branch -d new-branch
#To delete a local branch forcefully
git branch -D new-branch
Git will not allow you to delete the local branch if there are unmerged changes in that branch. In that case, you need to use -D flag that will forcefully delete that branch.
To delete a remote branch, the following command needs to be executed but if the remote branch needs to be deleted forcefully then the --force/--f flag should be used.
git push origin --delete new-branch
#To delete the remote branch forcefully
git push origin --delete --f new-branch
11. git diff
git diff command displays the modifications made to a file or set of files between two versions.
12. git log
A list of commits made to the repository is displayed by the
git log command. It shows the information about the commit, including the author, date, and commit message.
13. git merge
git merge command is used to combine changes from one branch into another branch. When working on the same code, developers can utilize this to integrate their changes before putting them up in a branch.
14. git show
git show command provides information about a particular commit. It display the changes made in the commit and other metadata, such as the commit message, author, and date.
15. git reset
git reset command resets the repository's state to a particular commit. It can be applied to undo repository modifications or to undo erroneous commits. With the
git log command, you can get the commit id.
#To reset the reposiotry to a specific commit id
git reset [commit id]
#To reset the repository to the previous commit
git reset HEAD^
git reset HEAD^ will reset the repository to the previous commit and move the HEAD pointer back to one commit.
16. git stash
Changes that are not yet ready to be committed are stored temporarily using the
git stash command. It can be used to save modifications before merging or to transition between branches without committing changes.
17. git checkout
To switch between branches or to create a new branch,
git checkout command is used.
# Switch between branches:
git checkout feature-branch
#Create and switch to a new branch
git checkout -b another-new-branch
18. git rm
To remove a file (main.txt) from both the working directory and the Git repository, the
git rm command is used.
git rm main.txt
If you want to keep it in the working directory but remove from Git repository, you can use the following command:
git rm --cached main.txt
In conclusion, Git is a crucial tool for version control in software development. You can become an expert in version control by learning these key Git commands as well as other Git commands. These commands make it possible to manage the codebase and collaborate effectively with other developers. Git commands not only help you save time but also enable you to track changes, correct issues, and maintain a transparent history of the project. You may work smarter and more productively in software development by correctly using Git commands.
I appreciate you taking the time to read this. Your support is much appreciated! If you found this article valuable, please consider clicking the 👉 Follow button and giving it a few claps by clicking the ❤️ like button to help me create more informative content like this. Thank you for your time! 🖤
Also, follow me on Medium, Twitter & LinkedIn.