Git is a version control system that tracks the changes in your project. Git is an essential aspect of day-to-day programming (particularly if you work in a team) and is extensively utilized in the software development industry.
As there are so many different commands to know, learning Git takes time. However, certain Git commands are used more frequently. So, in this post, explain the most frequently used Git Commands that every developer should be familiar with.
Initialize Repository
It will initialize the project folder into a git repository.
1 | git init |
Checking Git version
It displays the version of Git that is currently installed on your PC.
1 | git --version |
Git Status Checking
It will show you exactly which files/folders have been modified.
1 | git status |
The below git command shows the short status.
1 | git status -s |
Staging Files
git add
Add file changes to the git staging area.
You can add all changes by using the below command.
1 | git add. |
To add a single file to the staging area using the below git command.
1 | git add fileName |
To add multiple files to the staging area using the below git command.
1 | git add fileName1 fileName2 |
Stages the files using a pattern.
1 | git add *.php |
The below command controls the source code commit. It provides the yes/no options for adding the code to the git staging area.
1 | git add -p |
git diff
It will differentiate between a file in the staging area and a file in the working tree ( Untracked file ).
Commit the staged/added files
It will save your changes to your local repository. It’s good practice to include a proper commit message which helps in debugging.
1 | git commit -m 'Your Message' |
You can bypass the staging area using the below command.
1 | git commit -am "Your Message" |
git push
It will push all the local changes that you’ve made to the remote Github repository.
git pull
It will pull ( fetch ) all updated code from the remote branch and merge it with your local branch.
git log
It will display your whole commit history, which includes all of the commits you’ve made till now.
git branch BranchName
This command adds a new branch to your local git repository.
git branch
It will display a list of all the local branches you’ve created.
git branch -a
It will display all of the branches, both local and remote, that are available for checkout.
git branch -D BranchName
It will remove the selected local branch forcibly ( even if the changes are not committed ).
git checkout
It refers to switching between local git branches.
1 | git checkout BranchName |
git stash
It allows you to save the changes locally and it allows you to do other git operations.
git remote
It will give the name of the remote repository.
For e.g, ” origin ” or ” upstream ”
git reset
if do you want to undo the commit, you can use the below git command.
1 | git reset --soft HEAD~1 |
git revert CommitId
If you want to revert the particular commit then you can use the below command.
1 | git revert CommitID |
You can get the CommitID using the git log command.
git remote -v
It will give the name as well as the URL of the remote repository.
Remove folder from git:
We use the below command to remove the folder from the git.
1 | git rm -r FolderName |
Commit the change and push the change to your remote repository.
1 | git commit -m "Remove unuse directory" |
1 | git push |
These are the Most frequently Used Git Commands. You can read About Git from the Github site.