DEV Community

Parthiv
Parthiv

Posted on

30 Git Commands Every Developer Uses Regularly (With Practical Examples)

Git is one of those tools you'll use almost every day, whether you're building applications, collaborating with a team, or managing deployment pipelines. While there are hundreds of Git commands available, only a handful become part of your daily workflow.

This guide covers 30 practical Git commands that every developer, DevOps engineer, and software engineer should know. Instead of memorizing them, focus on understanding when to use each one.


1. Initialize a Repository

git init
Enter fullscreen mode Exit fullscreen mode

Creates a new Git repository in your current project folder so version control can begin.


2. Clone an Existing Repository

git clone <repository-url>
Enter fullscreen mode Exit fullscreen mode

Downloads an existing Git repository from GitHub or another Git provider to your local machine.


3. Check Repository Status

git status
Enter fullscreen mode Exit fullscreen mode

Shows modified, staged, and untracked files so you always know the current state of your project.


4. Stage Specific Files

git add <filename>
Enter fullscreen mode Exit fullscreen mode

Adds selected files to the staging area before creating a commit.

To stage everything:

git add .
Enter fullscreen mode Exit fullscreen mode

5. Commit Your Changes

git commit -m "Describe your changes"
Enter fullscreen mode Exit fullscreen mode

Saves the staged changes as a new snapshot in the repository with a meaningful commit message.


6. Upload Changes

git push
Enter fullscreen mode Exit fullscreen mode

Pushes your local commits to the remote repository.


7. Download Latest Changes

git pull
Enter fullscreen mode Exit fullscreen mode

Fetches updates from the remote repository and merges them into your current branch.


8. View Commit History

git log
Enter fullscreen mode Exit fullscreen mode

Displays the project's commit history along with author information and timestamps.


9. List All Branches

git branch
Enter fullscreen mode Exit fullscreen mode

Shows every local branch available in your repository.


10. Create a New Branch

git checkout -b feature/login
Enter fullscreen mode Exit fullscreen mode

Creates a new branch and immediately switches to it.


11. Switch Between Branches

git checkout main
Enter fullscreen mode Exit fullscreen mode

Moves your working directory to another branch.


12. Merge Another Branch

git merge feature/login
Enter fullscreen mode Exit fullscreen mode

Combines changes from another branch into your current branch.


13. View Remote Repositories

git remote -v
Enter fullscreen mode Exit fullscreen mode

Displays the configured remote repositories and their URLs.


14. Add a Remote Repository

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

Connects your local repository to a remote Git server.


15. Remove a Remote

git remote remove origin
Enter fullscreen mode Exit fullscreen mode

Deletes a configured remote repository.


16. Compare Changes

git diff
Enter fullscreen mode Exit fullscreen mode

Shows the differences between your working directory, staged files, and commits.


17. Fetch Without Merging

git fetch
Enter fullscreen mode Exit fullscreen mode

Downloads the latest commits from the remote repository without changing your current branch.


18. Save Work Temporarily

git stash
Enter fullscreen mode Exit fullscreen mode

Temporarily stores uncommitted changes so you can switch tasks without committing unfinished work.

Restore them later:

git stash pop
Enter fullscreen mode Exit fullscreen mode

19. Unstage a File

git reset <filename>
Enter fullscreen mode Exit fullscreen mode

Removes a file from the staging area while keeping your local modifications.


20. Delete a Tracked File

git rm <filename>
Enter fullscreen mode Exit fullscreen mode

Deletes a file and stages that deletion for the next commit.


21. Configure Git Identity

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Enter fullscreen mode Exit fullscreen mode

Sets the default identity used for all your commits.


22. Create a Version Tag

git tag v1.0.0
Enter fullscreen mode Exit fullscreen mode

Creates a tag that marks an important release or milestone.

Push tags using:

git push --tags
Enter fullscreen mode Exit fullscreen mode

23. Delete a Branch

git branch -d feature/login
Enter fullscreen mode Exit fullscreen mode

Removes a branch that has already been merged.


24. Rebase a Branch

git rebase main
Enter fullscreen mode Exit fullscreen mode

Moves your branch on top of another branch to maintain a cleaner project history.


25. Abort a Merge Conflict

git merge --abort
Enter fullscreen mode Exit fullscreen mode

Cancels an ongoing merge and restores the repository to its previous state.


26. Apply a Single Commit

git cherry-pick <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Copies a specific commit from another branch without merging the entire branch.


27. Remove Stale Remote Branches

git fetch --prune
Enter fullscreen mode Exit fullscreen mode

Deletes local references to remote branches that no longer exist.


28. Clean Untracked Files

git clean -df
Enter fullscreen mode Exit fullscreen mode

Removes untracked files and folders from your working directory.

Use this carefully because deleted files cannot be recovered through Git.


29. Initialize Git Submodules

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

Downloads and initializes all project submodules.

This is commonly required after cloning repositories that depend on external projects.


30. Clone with Submodules

git clone --recurse-submodules <repository-url>
Enter fullscreen mode Exit fullscreen mode

Clones the repository and automatically downloads all associated submodules in one step.


Final Thoughts

Learning Git isn't about memorizing every available command. It's about becoming comfortable with the commands you'll use repeatedly throughout development.

If you can confidently work with branching, committing, merging, fetching, rebasing, and resolving conflicts, you'll be prepared for most real world Git workflows.

Which Git command do you use the most? Share it in the comments, and if you think I missed one that deserves a place in this list, I'd love to hear your suggestions.

Happy coding! πŸš€

Top comments (0)