Top 12 Most Used Git Commands For Developers - GeeksforGeeks

18 Oct, 2021
Git is a distributed version control system and open-source software. It enables developers to manage many versions of a source code with ease. You can use it to figure out who did what, when, and why. Git has become a must-have tool for any developer nowadays, and knowing Git commands is required for developers to fully utilize Git. There are hundreds of Git commands, but just a few are used regularly.
Before you can start using Git, you need to configure it. This command allows you to specify the username and email address that will be used with your commits.
# sets up Git with your name

git config --global user.name "<Your-Full-Name>"

# sets up Git with your email

git config --global user.email "<your-email-address>"
A git repository must first be created before you can make commits or do anything else with it. We’ll use the git init command to create a new Git repository. The init subcommand stands for “initialise,” which is useful because it’s the command that handles all of a repository’s initial setup. In a moment, we’ll look at what it does. The git init command creates all of the necessary files and directories for Git to keep track of everything. All of these files are kept in a directory called .git (note the . at the beginning; this indicates that it will be hidden on Mac/Linux). The “repo” is this .git directory!
$ git init
The command we’ll be doing on the terminal is git clone, which is how cloning is related to Git. The git clone command takes a path (typically a URL) to the Git repository you want to clone. The command is git clone, and the path to the Git repository you want to clone is passed as an argument. This is the URL for the project we’ll be working on throughout the course. git clone produces a local working copy of a remote repository’s source code. When you clone a repository, the code is downloaded to your machine automatically. If you have permission, this command will add the original location as a remote location, allowing you to grab changes from it and push changes to it.
$ git clone https://github.com/<repo-url>
The git status command is our key to Git’s mind. It will inform us of Git’s thoughts and the state of our repository as seen by Git. When you’re first getting started, you should always use the git status command! Seriously. It’s a good idea to start running it after any other command. This will assist you in learning Git and avoiding making (potentially) inaccurate assumptions about the state of your files/repository.
$ git status
Depending on the state of your files, working directory, and repository, the git status tool will display a lot of information.
To move files from the Working Directory to the Staging Index, use the git add command. The git add command saves your changes in a file to the staging area, allowing you to compare your local version to the remote repository’s version. Before committing your new or changed file, use the git add command to add it to the staging area. To add specific files:
$ git add <file1> <file2> ... <fileN>
To add all the files:
$ git add .  
This command saves a log message along with the commit id of the modifications made to the git repository. The modifications are saved in your local repository with git commit. You must include a brief description of the changes made every time you commit your code changes. This commit message aids others in comprehending the changes made.
$ git commit –m "<Type your commit message here>"
This command pushes the contents of your local repository to the remote repository you’ve added. This pushes your master branch’s commits to the newly added remote repository. If a named branch does not exist in the remote repository, it will be created.
$ git push
Add a new branch to an existing branch, list all existing branches, and delete a branch with git branch. This command is used to perform operations on a branch that has been specified. When you run this command, Git will remove all files and folders from the Working Directory it is tracking (files that Git tracks are stored in the repository, so nothing is lost) and pull all of the files and directories from the commit that the branch links to from the repository. Create a new branch locally:
$ git branch <branch-name>
Take a look at the branches and check out on which branch you’re currently working:
$ git branch or $ git branch --list
We can use the git checkout command to switch to an existing branch or to create and switch to a new one. To accomplish this, the branch you want to switch to must be present in your local system, and any modifications made to your current branch must be committed or stashed before switching. This command can also be used to check out files. When you run this command, Git will remove all files and folders from the Working Directory it is tracking (files that Git tracks are stored in the repository, so nothing is lost) and pull all of the files and directories from the commit that the branch links to from the repository.
$ git checkout <branch-name>
The history of the specified branch is merged into the current branch with this command. The command git merge joins your branch to the parent branch. Depending on your process, the parent branch can be either a development or a master branch. If there are no conflicts, it will make a new commit automatically. You should be on the branch you want to merge with your parent branch before running the git merge command. The history of the specified branch is merged into the current branch with this command.
$ git merge <name-of-branch-to-merge-in>
The contents of the remote repository are fetched and integrated into your local repository using this command. git pull pulls the most recent changes from the remote server into the local repository, ensuring you have the most up-to-date information from your coworkers.
$ git pull
The git log command is used to show all of a repository’s commits. This command displays a log of all commits made to the current branch so far.
$ git log
TrueGeek-2021
GBlog
Git
TrueGeek
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
How to Start Learning DSA?
Introduction to Recurrent Neural Network
12 pip Commands For Python Developers
A Freshers Guide To Programming
How to integrate Git Bash with Visual Studio Code?
How to Set Git Username and Password in GitBash?
Working on Git Bash
Deploy Python Flask App on Heroku
Setting up Sublime Text for C++ Competitive Programming Environment