DEV Community

Cover image for Mastering Git: 30 Essential Commands Every Developer Should Know
Vishal Yadav
Vishal Yadav

Posted on

Mastering Git: 30 Essential Commands Every Developer Should Know

GitHub is an indispensable tool for developers worldwide. Mastering Git commands is crucial to efficiently managing your codebase and collaborating with others. Here, we've compiled 30 essential Git commands that will help you navigate your projects like a pro.

1. git init

Initializes a new Git repository in the current directory.

git init
Enter fullscreen mode Exit fullscreen mode

2. git clone [url]

Clones a repository into a new directory.

git clone [url]
Enter fullscreen mode Exit fullscreen mode

3. git add [file]

Adds a file or changes in a file to the staging area.

git add [file]
Enter fullscreen mode Exit fullscreen mode

4. git commit -m "[message]"

Records changes to the repository with a descriptive message.

git commit -m "[message]"
Enter fullscreen mode Exit fullscreen mode

5. git push

Uploads local repository content to a remote repository.

git push
Enter fullscreen mode Exit fullscreen mode

6. git pull

Fetches changes from the remote repository and merges them into the local branch.

git pull
Enter fullscreen mode Exit fullscreen mode

7. git status

Displays the status of the working directory and staging area.

git status
Enter fullscreen mode Exit fullscreen mode

8. git branch

Lists all local branches in the current repository.

git branch
Enter fullscreen mode Exit fullscreen mode

9. git checkout [branch]

Switches to the specified branch.

git checkout [branch]
Enter fullscreen mode Exit fullscreen mode

10. git merge [branch]

Merges the specified branch's history into the current branch.

git merge [branch]
Enter fullscreen mode Exit fullscreen mode

11. git remote -v

Lists the remote repositories along with their URLs.

git remote -v
Enter fullscreen mode Exit fullscreen mode

12. git log

Displays commit logs.

git log
Enter fullscreen mode Exit fullscreen mode

13. git reset [file]

Unstages the file, but preserves its contents.

git reset [file]
Enter fullscreen mode Exit fullscreen mode

14. git rm [file]

Deletes the file from the working directory and stages the deletion.

git rm [file]
Enter fullscreen mode Exit fullscreen mode

15. git stash

Temporarily shelves (or stashes) changes that haven't been committed.

git stash
Enter fullscreen mode Exit fullscreen mode

16. git tag [tagname]

Creates a lightweight tag pointing to the current commit.

git tag [tagname]
Enter fullscreen mode Exit fullscreen mode

17. git fetch [remote]

Downloads objects and refs from another repository.

git fetch [remote]
Enter fullscreen mode Exit fullscreen mode

18. git merge --abort

Aborts the current conflict resolution process and tries to reconstruct the pre-merge state.

git merge --abort
Enter fullscreen mode Exit fullscreen mode

19. git rebase [branch]

Reapplies commits on top of another base tip, often used to integrate changes from one branch onto another cleanly.

git rebase [branch]
Enter fullscreen mode Exit fullscreen mode

20. git config --global user.name "[name]" and git config --global user.email "[email]"

Sets the name and email to be used with your commits.

git config --global user.name "[name]"
git config --global user.email "[email]"
Enter fullscreen mode Exit fullscreen mode

21. git diff

Shows changes between commits, commit and working tree, etc.

git diff
Enter fullscreen mode Exit fullscreen mode

22. git remote add [name] [url]

Adds a new remote repository.

git remote add [name] [url]
Enter fullscreen mode Exit fullscreen mode

23. git remote remove [name]

Removes a remote repository.

git remote remove [name]
Enter fullscreen mode Exit fullscreen mode

24. git checkout -b [branch]

Creates a new branch and switches to it.

git checkout -b [branch]
Enter fullscreen mode Exit fullscreen mode

25. git branch -d [branch]

Deletes the specified branch.

git branch -d [branch]
Enter fullscreen mode Exit fullscreen mode

26. git push --tags

Pushes all tags to the remote repository.

git push --tags
Enter fullscreen mode Exit fullscreen mode

27. git cherry-pick [commit]

Picks a commit from another branch and applies it to the current branch.

git cherry-pick [commit]
Enter fullscreen mode Exit fullscreen mode

28. git fetch --prune

Prunes remote tracking branches no longer on the remote.

git fetch --prune
Enter fullscreen mode Exit fullscreen mode

29. git clean -df

Removes untracked files and directories from the working directory.

git clean -df
Enter fullscreen mode Exit fullscreen mode

30. git submodule update --init --recursive

Initializes and updates submodules recursively.

git submodule update --init --recursive
Enter fullscreen mode Exit fullscreen mode

Conclusion

These 30 Git commands are essential for any developer looking to effectively manage their projects on GitHub. Whether you're initializing a new repository, managing branches, or pushing your latest changes, mastering these commands will make your development workflow smoother and more efficient. Happy coding!

By familiarizing yourself with these commands, you can handle most Git operations with ease, allowing you to focus more on writing great code and less on managing your repository.

Top comments (8)

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ

You might want to re-title the post. This is about Git, not GitHub

Collapse
 
vyan profile image
Vishal Yadav

Done!

Collapse
 
hughdbrown profile image
Hugh Brown

How is this about github? Are there git-commands here that do not work with other git servers?

Collapse
 
darktek profile image
DarkteK

#9 should be changed to git switch or you would need to modify the proper description:

The switch command indeed does the same thing as checkout, but only for those usages that switch branches. It cannot restore working tree files โ€” that is done using restore, the other command split off from checkout.

The split checkout commands, in summary:

switch โ€” Switch to a specified branch
restore โ€” Restore file(s) from another branch or source

Git v2.23.0 was release 5 years ago and ppl are still using git checkout as git switch doesn't even exists...

Git logs: github.com/git/git/blob/master/Doc...

Collapse
 
drumm profile image
Sam J.

A lot more can be said about git merge vs rebase. For feature development, I typically use โ€˜git rebase -iโ€™ all day and never โ€˜git mergeโ€™.

Collapse
 
ptcbink profile image
ptcbink

This is basic of our devs.

Collapse
 
boby_tiwari_fd03ffa35b156 profile image
Boby Tiwari

Great

Collapse
 
ismail_mb profile image
Isma'il Hassan

Thanks for sharing