DEV Community

Cover image for 25 Git Commands Every Developer Should Know
sachinkg12
sachinkg12

Posted on

25 Git Commands Every Developer Should Know

Git is an indispensable tool for modern developers. Whether you're collaborating on a team project or managing your own codebase, understanding essential Git commands can make your workflow more efficient and error-free.

Here's a list of 25 Git commands that every developer should know, along with examples and scenarios where they are useful:


1. git init

Initializes a new Git repository in your project directory.

git init
Enter fullscreen mode Exit fullscreen mode

Example: Run this command in a folder to start version control for your project.

Scenario: Useful when starting a new project and you want to track changes from the beginning.


2. git clone

Clones an existing repository to your local machine.

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

Example:

git clone https://github.com/user/repo.git
Enter fullscreen mode Exit fullscreen mode

Scenario: Use this to work on a project hosted on a platform like GitHub.


3. git status

Displays the current state of the working directory and staging area.

git status
Enter fullscreen mode Exit fullscreen mode

Example: Shows if files have been modified, added, or deleted.

Scenario: Check which files need to be committed or staged.


4. git add

Adds files to the staging area for the next commit.

git add <file>  # Add a specific file
git add .       # Add all changes
Enter fullscreen mode Exit fullscreen mode

Example:

git add index.html
Enter fullscreen mode Exit fullscreen mode

Scenario: Use this before committing changes to ensure they're tracked.


5. git commit

Records changes in the repository with a message.

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

Example:

git commit -m "Add homepage"
Enter fullscreen mode Exit fullscreen mode

Scenario: Every time you make a set of changes that are ready to save in history.


6. git log

Shows the commit history of the repository.

git log
Enter fullscreen mode Exit fullscreen mode

Example:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

Scenario: Review the history of changes and who made them.


7. git branch

Lists, creates, renames, or deletes branches.

git branch         # List branches
git branch <name>  # Create a new branch
Enter fullscreen mode Exit fullscreen mode

Example:

git branch feature-login
Enter fullscreen mode Exit fullscreen mode

Scenario: Create a new branch for a feature to work independently.


8. git checkout

Switches between branches or restores files.

git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

Example:

git checkout main
Enter fullscreen mode Exit fullscreen mode

Scenario: Switch to a different branch to work on another feature.


9. git switch

A modern alternative to git checkout for switching branches.

git switch <branch-name>
Enter fullscreen mode Exit fullscreen mode

Example:

git switch feature-login
Enter fullscreen mode Exit fullscreen mode

Scenario: Use this to switch branches more intuitively.


10. git merge

Combines the changes from one branch into the current branch.

git merge <branch-name>
Enter fullscreen mode Exit fullscreen mode

Example:

git merge feature-login
Enter fullscreen mode Exit fullscreen mode

Scenario: Integrate a completed feature branch into the main branch.


11. git fetch

Downloads updates from a remote repository without merging them.

git fetch <remote>
Enter fullscreen mode Exit fullscreen mode

Example:

git fetch origin
Enter fullscreen mode Exit fullscreen mode

Scenario: See the latest updates on the remote without affecting your local branch.


12. git pull

Fetches and integrates changes from a remote repository.

git pull <remote> <branch>
Enter fullscreen mode Exit fullscreen mode

Example:

git pull origin main
Enter fullscreen mode Exit fullscreen mode

Scenario: Use this to synchronize your local repository with the remote.


13. git push

Uploads your local changes to a remote repository.

git push <remote> <branch>
Enter fullscreen mode Exit fullscreen mode

Example:

git push origin main
Enter fullscreen mode Exit fullscreen mode

Scenario: Push your changes to share with your team.


14. git diff

Shows differences between commits, branches, or your working directory.

git diff           # Show unstaged changes
git diff <branch>  # Compare branches
Enter fullscreen mode Exit fullscreen mode

Example:

git diff HEAD
Enter fullscreen mode Exit fullscreen mode

Scenario: Inspect what changes were made before committing.


15. git reset

Unstages files or reverts commits.

git reset <file>                # Unstage a file
git reset --hard <commit-hash>  # Reset to a specific commit
Enter fullscreen mode Exit fullscreen mode

Example:

git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode

Scenario: Undo commits or remove files from staging.

Note: Using git reset --hard cannot be undone and should be avoided in production environments to prevent accidental loss of work.


16. git stash

Temporarily saves changes you're not ready to commit.

git stash      # Stash changes
git stash pop  # Apply stashed changes
Enter fullscreen mode Exit fullscreen mode

Example:

git stash
Enter fullscreen mode Exit fullscreen mode

Scenario: Pause work on a feature and switch to another task without committing.


17. git rebase

Reapplies commits on top of another base branch.

git rebase <branch-name>
Enter fullscreen mode Exit fullscreen mode

Example:

git rebase main
Enter fullscreen mode Exit fullscreen mode

Scenario: Keep a clean commit history by replaying commits.


18. git tag

Creates a tag for marking specific commits (e.g., for releases).

git tag <tag-name>
git tag -a <tag-name> -m "Message"
Enter fullscreen mode Exit fullscreen mode

Example:

git tag v1.0
Enter fullscreen mode Exit fullscreen mode

Scenario: Mark specific commits, such as software releases.


19. git remote

Manages connections to remote repositories.

git remote add <name> <url>
git remote -v  # List remotes
Enter fullscreen mode Exit fullscreen mode

Example:

git remote add origin https://github.com/user/repo.git
Enter fullscreen mode Exit fullscreen mode

Scenario: Set up or view connections to remote repositories.


20. git cherry-pick

Applies a specific commit from another branch.

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

Example:

git cherry-pick abc123
Enter fullscreen mode Exit fullscreen mode

Scenario: Bring a specific fix or feature from one branch to another.


21. git blame

Shows who made changes to each line in a file.

git blame <file>
Enter fullscreen mode Exit fullscreen mode

Example:

git blame app.js
Enter fullscreen mode Exit fullscreen mode

Scenario: Identify who wrote or modified a specific piece of code.


22. git archive

Creates a compressed archive of the repository.

git archive --format=zip HEAD > archive.zip
Enter fullscreen mode Exit fullscreen mode

Example:

git archive --format=zip HEAD > project.zip
Enter fullscreen mode Exit fullscreen mode

Scenario: Share a snapshot of the codebase without version control data.


23. git clean

Removes untracked files and directories.

git clean -f   # Force delete files
git clean -fd  # Delete files and directories
Enter fullscreen mode Exit fullscreen mode

Example:

git clean -fd
Enter fullscreen mode Exit fullscreen mode

Scenario: Clean up unwanted files in the working directory.


24. git show

Displays detailed information about a specific commit.

git show <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Example:

git show abc123
Enter fullscreen mode Exit fullscreen mode

Scenario: Inspect details of a particular commit.


25. git revert

Creates a new commit that undoes changes introduced by a previous commit.

git revert <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Example:

git revert abc123
Enter fullscreen mode Exit fullscreen mode

Scenario: Undo a commit without modifying history.


These commands cover most of the day-to-day tasks you'll encounter when using Git. Master these commands, and you'll be well-equipped to handle any Git-related challenge.


Feel free to share your thoughts or insights. I'm excited to learn and grow together with you.

Happy Learning! :)

Top comments (4)

Collapse
 
davidisnotnull profile image
David Johnson

A really great collection of git commands, thanks for putting this together!

Collapse
 
sachinkg12 profile image
sachinkg12

Thanks david

Collapse
 
sunilfgh profile image
Sunil kumar Dubey

Great collection

Collapse
 
sachinkg12 profile image
sachinkg12

Thanks Sunil