Mastering Git Magic: The Top 7 Commands You Need to Know
Today, We will learn Mastering Git Magic: The Top 7 Commands You Need to Know. In this tutorial, I will give you the top 7 git commands and his example. Git is a powerful version control system used for tracking changes in source code during software development. Here are some common Git commands and their brief descriptions:
The Top 7 Git Commands
1. Git init
This command lets us create a new repository. A hidden .git directory is added to the folder. Most of the git commands do not work outside the initialized project, so this is the first command you will run in a project. Go to project folder > run git init.
git init
2. Git clone
This command creates a local copy of a remote repository. When you clone a repo the source code gets automatically downloaded to the local machine. This local repo will point to the remote repo and can PUSH and PULL changes to it.
git clone <git-repo-url>
3. Git add
This command add your changes to a staging area where you can compare your local version with the remote repo code. It is mandatory to stage the code before committing (push to remote) using the git add command.
To stage all files use (.) – git add . in the same repo.
git add .
--- OR ---
git add <file-1> <file-2> <file-n>
4. Git commit
This command saves your changes to your local repository. Every time you commit you have to add a small message about the changes you made. This will help to keep track of the changes later.
git commit -m "commit-message"
5. Git push
This command push your changes from the local repository to your remote repository. One can only push the committed changes. It also creates the repository with the branch name you enter if the repository does not exist in a remote location. If the branch is already connected to the remote then run – git push.
git push <remote> <branch-name>
6. Git pull
This command fetches the latest changes from the remote repository to your local. This is helpful when multiple people are working on the same repository. It will help to keep your local repo updated with the latest code. If the branch is already connected to the remote then run – git pull.
git pull <remote> <branch-name>
7. Git checkout
This command helps to switch to an existing branch or create a new branch. Before checking out make sure the branch exists in your local machine and that the changes in the current branch are already staged or committed.
git checkout -b <branch>