DEV Community

Ludivine A
Ludivine A

Posted on

Git commands you must know

Git is a very important tool for any developer. I had a hard time understanding it at first, and I’m sure some of you are still a bit lost.

So here is a rundown of 13 important git commands to better manage your workflow. There are many more commands available that you can find here, but these are the most useful and frequently used ones and they will definitely speed up your coding process.

If you are not sure to understand what Git is, or if maybe you don’t know the difference between Git and Github, I have an article explaining everything available here.

1. git clone

If you want to clone a remote repository use : git clone <repository url>

2. git init

To start a new project in an empty repository or to initialize an existing project, use this command in the project root : git init

3. git fetch

With git fetch you will get the latest updates of the repository, changes in the code and the new branches that were created.

4. git checkout

With this command, you can switch branches : git checkout <branch name>

To create a new branch and switch to it : git checkout -b <branch name>

5. git add

This command selects the files you updated, and that you wish to send to the remote repository.

You can add a specific file with git add <file path> or add all the files with git add -A or git add .

6. git stash

If there are files that you don’t wish to commit now, you can set them aside or “stash” them.

You first need to add them with the previous command, and then use git stash.

To revert those changes, use git stash apply.

7. git commit

Once you made some changes in the code and you saved it, you have to commit them. You are creating a little “container” of updates that you describe with a message :

git commit -m “commit message”

Example : git commit -m “added a new react component”

8. git push

This command will send your commits to the remote branch : git push

9. git revert

You can revert commits you created. In order to do that, you will need to know the hash code of the commit. To get that hash code, use the command git log — oneline

https://cdn-images-1.medium.com/max/800/1*GGQWcvliqYnXTwCxVlFPEg.png

This will display a list of commits you previously created alongside a little code, that’s what you are looking for.

If I wanted to revert that first commit, I will use the command git revert 7804f7c.

Exit the screen that will appear with shift + q or type :qa in the terminal. A new commit will created reverting the last changes without deleting the previous commit.

https://cdn-images-1.medium.com/max/800/1*CfpaSSh6U_F7AtMESc1QCw.png

10. git pull

Using git pull will fetch all the changes from the remote repository and merge any remote changes in the current local branch.

11. git branch

git branch gives you the list of all the branches of the repository.

You can also create new branches like this :

git branch <new branch name>

You can delete a branch as well :

git branch -d <branch name>

12. git status

The git status command gives you information about the current branch.

You will know if the current branch is up to date, if you need to push, or pull, if content is staged, unstaged or untracked and if files were created, modified or deleted.

13. git merge

git merge is the command that “merges” or combine branches commits. Once you are done working on your branch, the last thing to do is to merge your branch with the main branch.

First you need to be on the branch you want your branch to be merged into.

Example : If you want to merge your branch “my-new-feature” into the branch called ”main”, you need to checkout into “main” and run the command here.

If you don’t remember how to switch branches, type :

git checkout main

Before merging branches you should always fetch the latest updates:

git fetch

Now, you can merge you branch into the main branch :

git merge my-new-feature


Knowing these commands is very important, but I also advise you to use a software to manage git. Github Desktop is available for free to handle your GitHub repositories, but you can find other alternatives like GitKraken.

I hope my article helped you understand all of this a little bit more ! Please don’t hesitate to ask questions if you need !


Originally posted on my blog. Check out my instagram account to learn more about web development.

Top comments (0)