Top 15 Git Commands You Should Know To Become A Coding Ninja

Sep 15, 2022

Top 15 Git Commands You Should Know To Become A Coding Ninja

Git is one of the most popular version control systems in the world and it’s no wonder that small and big projects rely on Git for version control. Knowledge of Git commands is a must-have thing to be a “true” developer and, of course, to be able to check and view all the changes in current projects. 

1. Set up username and email 

The username is needed to link commits with the developer's name and it’s not the same as a GitHub username. git config is a well-known command for setting or updating the developer’s username. In case you would like to hide your real name you can use any random text as your Git username, for example: git config –global “Boba Phat”

git config can be used to update the email address also. After, the entered email address will automatically reflect in any future commits that push from the command line. Example: git config –global “”

2. Cache login credentials 

git config –global credential.helper cache is a special command for this. Caching your login credentials help to avoid re-typing the username and password every time you perform a commit. 

3. Initialize a repository 

git init - this one is a command by which you can create or reinitialize an existing one. It will create a hidden directory upon initialization and this directory contains all the objects and references that Git uses and creates as a part of project’s history. 

4. Add single file or all files to repo 

git add singlefile.js - this command is for adding one individual file. In case you need to add all files you should use git add . then. 

5. Check a repository status 

git status - line is used to view the status of the current repo, which includes the staged, unstaged, and untracked files. 

6. Commit changes

git commit -m “your short comment about the commit” - by using this command you can add a single line message to your commit (commit parameter and -m flag are important to text in the command line)

7. Look up changes before committing 

git diff is a command for this. By adding to this line the –staged flag (git diff –staged) you can view the staged changes. If you provide a filename as a parameter you will be able to see the changes of a specific file, for example: git diff layout.js 

8. Rename files

The mv parameter is used to rename a file or a directory. This parameter requires a <source> and a <destination> - the source must exist and can be a file, or directory, and the destination must be an existing directory, example: git mv dirname1/somefile.js dirname2

9. Revert changes 

You can restore the unstaged working tree files by using the checkout parameter, example: git checkout somefile.js 

In case you need to restore staged working tree file you should use reset parameter, like in this example: git reset HEAD somefile.js

But if you want to unstage all staged files you should simply do not provide the file path: git reset HEAD

10. List all branches

The branch parameter is using in this case, let’s see the example: git branch

With using -a flag you can see the list of all remote branches: git branch -a

11. Delete a branch

If you need to remove a branch you need to use the branch parameter, the -d flag and the name of the branch, like in example beneath: git branch -d existing_branch_name

In case a remote branch must be deleted then this command needs to be executed: git push origin –delete existing_branch_name

12. Merge two branches 

git merge existing_branch_name will combine the specified branch into the main branch. 

What if we need to merge a commit? Then it should be used the –no–ff flag: git merge –no–ff existing_branch-name

It will not only merge the specified branch into the main branch but generate a merge commit - and this is important to document all merges that occur in repo. 

13. Add a remote repository 

git remote add someapp https://github .com/someurl - this line with the remote add parameter, short name and the url of the remote repository is adding a remote repository

14. Push changes to a remote repository 

git push origin main - this one is needed to enter in the command line. The above command will help to upload the local changes to a central repository so that other team members can view the changes you have made. 

15. Use rebase 

git rebase branch_name - it will change the base of your branch from one commit to another so it makes it appear as if you have created your branch from a different commit. Rebasing is a process to combine or move a sequence of commits to a new base commit.

This is a list of most-used git commands that almost every developer comes across in daily programming and Innovation Feel team isn't an exception 😉

Related Articles

Hands-On with OpenAI: A Developer's Guide to Using the OpenAI API

Hands-On with OpenAI: A Developer's Guide to Using the OpenAI API

From Sci-fi to Reality: Top 3 Technologies Retailers Should Use Now

From Sci-fi to Reality: Top 3 Technologies Retailers Should Use Now

AI in Healthcare Apps: Examples and Challenges

AI in Healthcare Apps: Examples and Challenges