DEV Community

Tamilselvan K
Tamilselvan K

Posted on

Day-38 Today I Learned: All the Essential Git Commands You Need to Know

Version control is a must-have skill for any developer, and Git is by far the most popular tool in this space. Today, I took a deep dive into Git and learned all the essential commands that every developer should know.

Here’s a summary of what I learned—bookmark this blog if you want a cheat sheet for your future Git workflows!


Git Basics

1. Initialize a Repository

git init
Enter fullscreen mode Exit fullscreen mode

Creates a new Git repository in your current folder.

2. Clone a Repository

git clone <repo_url>
Enter fullscreen mode Exit fullscreen mode

Clones a remote repository to your local machine.


Working with Changes

3. Check Status

git status
Enter fullscreen mode Exit fullscreen mode

Displays the state of the working directory and staging area.

4. Add Changes

git add <file_name>
git add .
Enter fullscreen mode Exit fullscreen mode

Adds files to the staging area. Use . to add all changes.

5. Commit Changes

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

Saves the staged changes with a descriptive message.


Working with Branches

6. Create a Branch

git branch <branch_name>
Enter fullscreen mode Exit fullscreen mode

7. Switch to a Branch

git checkout <branch_name>
Enter fullscreen mode Exit fullscreen mode

8. Create and Switch (Shortcut)

git checkout -b <branch_name>
Enter fullscreen mode Exit fullscreen mode

9. List Branches

git branch
Enter fullscreen mode Exit fullscreen mode

10. Delete a Branch

git branch -d <branch_name>
Enter fullscreen mode Exit fullscreen mode

Pushing and Pulling

11. Push to Remote

git push origin <branch_name>
Enter fullscreen mode Exit fullscreen mode

12. Pull Latest Changes

git pull origin <branch_name>
Enter fullscreen mode Exit fullscreen mode

History and Logs

13. View Commit History

git log
Enter fullscreen mode Exit fullscreen mode

14. Show Changes

git diff
Enter fullscreen mode Exit fullscreen mode

Undoing Changes

15. Undo Changes in File

git checkout -- <file_name>
Enter fullscreen mode Exit fullscreen mode

16. Unstage a File

git reset <file_name>
Enter fullscreen mode Exit fullscreen mode

17. Amend Last Commit

git commit --amend
Enter fullscreen mode Exit fullscreen mode

Remote Repository

18. Add Remote Repo

git remote add origin <url>
Enter fullscreen mode Exit fullscreen mode

19. View Remotes

git remote -v
Enter fullscreen mode Exit fullscreen mode

Advanced Commands (Optional)

20. Rebase

git rebase <branch_name>
Enter fullscreen mode Exit fullscreen mode

21. Stash Changes

git stash
Enter fullscreen mode Exit fullscreen mode

22. Apply Stashed Changes

git stash apply
Enter fullscreen mode Exit fullscreen mode

Summary

Learning Git can be overwhelming at first, but once you understand the basic commands, it becomes second nature. With these 20+ commands, I now feel more confident in handling any Git workflow — from initializing repositories to branching, committing, and syncing with remotes.

Top comments (0)