DEV Community

Cover image for All the Git Commands You Need to Know
Francisco Inoque
Francisco Inoque

Posted on

All the Git Commands You Need to Know

Git is a powerful version control tool that plays a crucial role in modern software development. Familiarizing yourself with essential Git commands is fundamental to effectively manage your source code and collaborate with other developers. In this guide, we will explore all the Git commands you need to know to become an expert.

Contents:

1. git init

The first step in versioning your project with Git. This command initializes a new Git repository in your directory.

   git init
Enter fullscreen mode Exit fullscreen mode

2. git clone

Use this command to create a local copy of an existing Git repository.

   git clone <repository_URL>
Enter fullscreen mode Exit fullscreen mode

3. git add

Add changes to your upcoming commit. You can specify individual files or use git add . to add all modified files.

   git add <file_name>
Enter fullscreen mode Exit fullscreen mode

4. git commit

Record changes in your repository with a descriptive message.

   git commit -m "Commit message"
Enter fullscreen mode Exit fullscreen mode

5. git pull

Update your local repository with changes from the remote repository.

   git pull
Enter fullscreen mode Exit fullscreen mode

6. git push

Send your local changes to the remote repository.

   git push
Enter fullscreen mode Exit fullscreen mode

7. git branch

List all branches in your repository and show the currently checked-out branch.

   git branch
Enter fullscreen mode Exit fullscreen mode

8. git checkout

Switch between branches or create a new branch.

   git checkout <branch_name>
Enter fullscreen mode Exit fullscreen mode

9. git merge

Combine changes from one branch into another.

   git merge <branch_name>
Enter fullscreen mode Exit fullscreen mode

10. git log

View the commit history of the repository.
Enter fullscreen mode Exit fullscreen mode
```bash
git log
```
Enter fullscreen mode Exit fullscreen mode

11. git stash

Temporarily stash uncommitted changes.
Enter fullscreen mode Exit fullscreen mode
```bash
git stash
```
Enter fullscreen mode Exit fullscreen mode

12. git reset

Undo changes in the repository.
Enter fullscreen mode Exit fullscreen mode
```bash
git reset <commit_hash>
```
Enter fullscreen mode Exit fullscreen mode

13. git remote

List configured remote repositories.
Enter fullscreen mode Exit fullscreen mode
```bash
git remote -v
```
Enter fullscreen mode Exit fullscreen mode

14. git fetch

Download information from the remote repository but do not automatically merge.
Enter fullscreen mode Exit fullscreen mode
```bash
git fetch
```
Enter fullscreen mode Exit fullscreen mode

15. git rebase

Rearrange commits for a cleaner timeline.
Enter fullscreen mode Exit fullscreen mode
```bash
git rebase <branch_name>
```
Enter fullscreen mode Exit fullscreen mode

16. git tag

Mark commits for specific versions of your project.
Enter fullscreen mode Exit fullscreen mode
```bash
git tag <tag_name>
```
Enter fullscreen mode Exit fullscreen mode

17. git status

Check the current state of your repository, including modified and untracked files.
Enter fullscreen mode Exit fullscreen mode
```shell
git status
```
Enter fullscreen mode Exit fullscreen mode

18. git diff

Display the differences between files in your working directory and the staging area.
Enter fullscreen mode Exit fullscreen mode
```shell
git diff
```
Enter fullscreen mode Exit fullscreen mode

19. git remote add

Add a new remote repository to your Git configuration.
Enter fullscreen mode Exit fullscreen mode
```shell
git remote add <remote_name> <remote_URL>
```
Enter fullscreen mode Exit fullscreen mode

20. git remote remove

Remove a remote repository from your Git configuration.
Enter fullscreen mode Exit fullscreen mode
```shell
git remote remove <remote_name>
```
Enter fullscreen mode Exit fullscreen mode

21. git fetch

Update remote repository information and download changes but do not automatically merge.
Enter fullscreen mode Exit fullscreen mode
```shell
git fetch
```
Enter fullscreen mode Exit fullscreen mode

22 git rebase -i

Perform an interactive rebase to rearrange, edit, or merge commits.
Enter fullscreen mode Exit fullscreen mode
```shell
git rebase -i <commit_hash>
```
Enter fullscreen mode Exit fullscreen mode

23. git cherry-pick

Apply a specific commit from one branch to another.
Enter fullscreen mode Exit fullscreen mode
```shell
git cherry-pick <commit_hash>
```
Enter fullscreen mode Exit fullscreen mode

24. git log --graph

Visualize the commit history graphically, showing branches.
Enter fullscreen mode Exit fullscreen mode
```shell
git log --graph
```
Enter fullscreen mode Exit fullscreen mode

25. git clean

Remove untracked files from the working directory.
Enter fullscreen mode Exit fullscreen mode
```shell
git clean -n   # Show files to be removed (dry run mode)
git clean -f   # Remove untracked files (with caution!)
```
Enter fullscreen mode Exit fullscreen mode

26. git submodule

Manage Git submodules within your main repository.
Enter fullscreen mode Exit fullscreen mode
```shell
git submodule add <submodule_URL> <local_path>
```
Enter fullscreen mode Exit fullscreen mode

This guide provides a comprehensive overview of Git commands that are essential for any developer. As you advance in your Git journey, you can explore more advanced commands and branching strategies. However, with these basic commands, you are ready to start effectively managing your source code.

Remember that consistent practice is the key to mastering Git. So, start experimenting with these commands in your own projects and see how they can enhance your efficiency in software development.

Top comments (8)

Collapse
 
maximovj profile image
Victor J. Maximo

I like it. Thanks so much

Collapse
 
franciscoinoque profile image
Francisco Inoque

Welcome

Collapse
 
bcouetil profile image
Benoit COUETIL 💫

git --help ...

Collapse
 
debakarroy profile image
Debakar Roy

Very accurate in the sense that I used most of this in my day to day life. Also for a very large codebase you might want to learn git gc. I ended up using it sometimes.

Collapse
 
alitdarmaputra profile image
Alit Darma Putra

Nice post, could you sharing more about cherry pick and merge/rebase. It would be awesome thank you

Collapse
 
exoutia profile image
Exoutia

I think git fetch is mentioned two times 14 and 21. Other than that it's great article.

Collapse
 
anirbandev profile image
</AnIrBaN>

Thanks for sharing

Collapse
 
t1mmy profile image
XploitSec

That's really helpful, I like it