Top 11 Git Commands That Every Developer Should Know
Master the most important Git commands that every developer should know!
Join the DZone community and get the full member experience.Join For Free
Git is a version control system that has become an essential tool for developers worldwide. It allows developers to keep track of changes made to a project's codebase, collaborate with others on the same codebase, and roll back changes when necessary.
Here are the top 11 Git commands every developer should know.
1. git config
git config is a command that allows you to configure Git on your system. It enables you to view and modify Git's settings, such as your user name and email address, default text editor, and more.
git config command is used to set configuration values that affect the behavior of Git. Configuration values can be set globally or locally, depending on whether you want the configuration to apply to all Git repositories on your system or just the current repository.
Some common use cases of the
git config command includes setting your user name and email address, configuring the default text editor, and customizing Git's behavior.
git config, you can tailor Git to your specific needs and preferences, making it easier and more efficient to work with Git on your projects.
Setting your user name and email address globally:
git config --global user.name "Riha Mervana" git config --global user.email "email@example.com"
You can read back these values as:
git config --list
user.name=Riha Mervana firstname.lastname@example.org
When you open the global configuration file
~/.gitconfig, you will see the content saved as:
[user] name = Riha Mervana email = email@example.com
2. git init
The first command every developer should know is
git init. This command initializes an empty Git repository in the current directory. This command creates a
.git directory in the current directory, which is where Git stores all the information about the repository, including the commit history and the files themselves.
git init command can be used in two ways:
Either changes a directory using the
cd command and run
git init to create a Git repository….
Or create an empty Git repository by specifying a directory name using the
git init command.
git init <directory-name>
3. git clone
git clone is used to create a local copy of a remote repository. This command downloads the entire repository and its history to your local machine. You can use this command to create a local copy of a repository that you want to contribute to or to start working on a new project.
Here is an example of how HTTPS looks.
git clone <https://github.com/reactplay/react-play.git>
This will clone the
react-play project locally for you. Then you can change to the directory and start working on it.
4. git add
git add is used to stage changes made to a file. This command tells Git that you want to include the changes made to a file in the next commit. You can add individual files or directories or all changes in the current directory by using the
git add . command.
git add command is used to send your file changes to the staging area.
git add <file-name>
git add <directory-name>
5. git commit
git commit is used to save changes made to the repository. This command creates a new commit with a message that describes the changes made. The message should be descriptive and provide context about the changes made.
git commit -m "add a meaningful commit message"
6. git push
git push is used to upload local changes to a remote repository. This command sends the changes made in your local repository to the remote repository, where other developers can access them. You can use this command to contribute changes to an open-source project or to share changes with your team.
git push <remote> <branch-name>
7. git pull
git pull is used to download changes made to a remote repository to your local repository. This command is useful when you want to work on the latest version of a project or when you want to merge changes made by other developers into your local repository.
8. git branch
git branch is used to create, list, and delete branches. A branch is a copy of the repository that you can use to work on new features or fixes without affecting the main branch. You can use this command to create a new branch, list all the branches in the repository, or delete a branch.
List all the branches:
Create a new branch with a branch name:
git branch <branch-name>
Delete a specific branch:
git branch -d <branch-name>
Rename a branch:
git branch -m <branch-name>
List all Remote branches (with a marking of the current branch):
git branch -a
9. git merge
git merge is used to merge changes made in one branch into another branch. This command is useful when you want to incorporate changes made in a feature branch into the main branch. You can use this command to merge changes made by other developers into your local branch or to merge your changes into the main branch.
git merge <branch-name>
10. git checkout
git checkout is used to switch between branches or to revert changes made to a file. This command allows you to move between branches or switch to a specific commit in the commit history. You can also use this command to discard changes made to a file and revert it to a previous state.
git checkout <branch-name>
11. git log
git log is used to view the commit history of a repository. This command displays a list of all the commits made to the repository, including the commit message, the author, and the date and time of the commit. You can use this command to track changes made to the repository over time and to identify which commits introduced specific changes.
git log <options> <branch_name>
Git is a powerful version control system that is widely used in software development. Knowing how to use Git effectively is essential for developers to collaborate on projects, keep track of changes, and maintain code quality. These above commands provide developers with the basic tools they need to manage their codebase effectively. However, Git is a complex system with many additional features and commands that can be used to improve workflow and productivity. Therefore, developers should strive to learn more about Git and its capabilities in order to take full advantage of its benefits.
Opinions expressed by DZone contributors are their own.