Git is a version control system developed in 2005 by Linus Torvalds, the creator of the Linux kernel. It helps you keep track of the code changes you've made to files in your project. It comes bundled with a large number of commands that you could use to efficiently manage your source code.
In this article, we'll go over the 20 most frequently needed Git commands that every software developer should know.
The above command displays a list of information about your git configuration including user name, email, default code editor, etc.
The above command could be used to configure your git user name. Please make sure to replace your git username with harshal.
The above command could be used to configure your email address. Replace your email address with
[email protected].
Initialize a Git Repository
The above command could be used to initialize i.e. creates a new git repository. It can be used to convert an existing project to a Git repository. The above command creates a new .git subfolder in your current working directory, which contains all of the necessary metadata for the new repository
Add a single file to the staging area
The above command adds a file to the staging area. Make sure to replace my_file with the name of the file that needs to be added to the staging area.
Add all files to the staging area
The above command adds all the files to the staging area.
Check Git status
The above command will display the status of the current repository including the current branch, list staged, unstaged, untracked files, etc.
Commit changes
The above command commits the changes to head. When executed it will open a code editor in the terminal where you can write a commit message.
Commit changes with a message
- git commit -m "Your commit message"
This command lets you only specify a short summary for your commit message without opening the code editor. Replace "Your commit message" with your own commit summary which describes the changes in your commit.
Check Git history
The above command displays a list of commit logs
Get branch list
You could use the above command to display the list of all the created branches in the local repository.
Delete a branch
The above command is used to delete a Git branch. Make sure to replace my_branch with your own branch name. Also, don't forget to add the -d flag. It tells the git that you wish to delete the specified branch.
Create a new branch
The above command can be used to create a new branch. One thing that we need to keep in mind is Git won't switch to it automatically – you will need to do it manually with using the checkout command. (See #14)
Switch branches
You could use the above command to switch to a newly created or a different branch.
Create a new branch in Git and switch to it immediately
- git checkout -b branch_name
You can create and checkout to a new git branch in a single command by adding the -b flag to the checkout command.
Add a remote repository in Git
- git add remote https://repo_url
The command adds a remote repository to your local repository. Make sure to replace repo_url with your actual remote repo URL.
Push your changes to a remote repo in Git
You could use the above command to push your changes to the remote repository.
Pull changes from a remote repo in Git
You could use the above command to retrieve the latest changes made to the remote repository.
Stashing changes
The stash command could be used to temporarily park (stash) your uncommitted changes (staged as well as unstaged), saves them away for later use.
Reverting stashed changes
The above command can be used to re-apply the changes parked using the stash command.
And that's it. These were the 20 Git commands I use the most often.
I hope you found this article useful – Enjoy