10 simple commands to become a master at Git.

You only need these 10 Commands to Master Git

Git is a popular version control system used by software developers to manage code changes, collaborate with other developers, and keep track of project history. In this article, we will provide an overview of Git, and walk you through the 10 commands to master Git.

Getting Started with Git

First, install Git on your computer. You can do this by downloading the latest version from the Git website (https://git-scm.com/downloads). Once you have Git installed, open up a terminal or command prompt and navigate to the directory where your code is located. if you don’t know how to do that, read our 10 simple Linux commands for beginners

10 Must Learn Git Commands

Initialize the repository

The first step is to initialize Git. This step will set up your project with the default configuration.

git init
Check changes made to the files

To view the files that were modified you can use the below command and it shows you the list of all files that have been modified.

git status

To see the modified contents of every file use the command below.

git diff
Add your files

Once your code is ready first thing you need to do is to add the files. You can either add the files individually one by one as shown below or

git add filename

You can also add all the files at once using the command below. Notice the dot. Adding a dot here means we are selecting all the files and folders in the current directory.

git add .
Add a commit message

Think of this step as “SAVING”. This is an important step as it helps in saving your changes and keeping them from getting lost. You should always add meaningful messages for your commits. This helps other developers understand your code a bit better.

git commit -m "Commit Message" 
Modify recent commit

If you want to modify your most recent commit, then this is the command to remember.

git commit --amend
Push your code to a branch in the Repository

The last step is to push your code to the remote repository. It can be on Github, Bitbucket, etc.; The word “remote” in the below command refers to the URL of the remote repository and can be named as anything. Most of the time it is named “origin”.

git push remote branch_name

So far we’ve learned about 6 Git Commands and these are the main commands any beginner developer or programmer should remember.
Now let’s talk about 4 more essential Git commands every beginner developer should learn.

Pull the latest changes from the main branch

Most of the time you’ll be working as a team and it is your responsibility to make sure to keep your local branch changes in sync with the remote branch.
To do that, you need to commit your local changes first and then execute the below command.

git pull origin branch_name
Start working from another branch

If you need to switch to a new branch first commit your local changes and push to remote if necessary. Then use the below command to checkout to another branch.

git checkout branch_name

You can also create a new branch and checkout to it at the same time using the below command.

git checkout -b branch_name
Stash your changes locally.

Using this command you can save or stash your current changes to use them later without committing them.

git stash 

To get back your stashed changes, use the below commands and they will be injected into your current code.

git stash pop
Insert code from a specific commit

This command can be a lifesaver but use it wisely. This command will let you copy and paste the changes made in a specific commit into your code.

git cherry-pick commit-hash 

You can also read about the Top 10 Git Commands For Beginner Developers.