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.
1) git config
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>"
2) git init
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
3) git clone
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>
4) git status
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.
5) git add
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 .
6) git commit
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>"
7) git push
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
8) git branch
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
9) git checkout
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>
10) git merge
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>
11) git pull
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
12) git log
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!