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 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 Example: git clone https://github.com/user/repo.git 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 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 # Add a specific file git add . # Add all changes Example: git add index.html 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" Example: git commit -m "Add homepage" 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 Example: git log --oneline 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 # Create a new branch Example: git branch feature-login Scenario: Create a new branch for a feature to work independently. 8. git checkout Switches between branches or restores files. git checkout Example: git checkout main 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 Example: git switch feature-login Scenario: Use this to switch branches more intuitively. 10. git merge Combines the changes from one branch into the current branch. git merge Example: git merge feature-login Scenario: Integrate a completed feature branch into the main branch. 11. git fetch Downloads updates from a remote repository without merging them. git fetch Example: git fetch origin 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 Example: git pull origin main Scenario: Use this to synchronize your local repository with the remote. 13. git push Uploads your local changes to a remote repository. git push Example: git push origin main 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 # Compare branches Example: git diff HEAD Scenario: Inspect what changes were made before committing. 15. git reset Unstages files or reverts commits. git reset # Unstage a file git reset --hard # Reset to a specific commit Example: git reset --hard HEAD~1 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 Example: git stash 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 Example: git rebase main 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 git tag -a -m "Message" Example: git tag v1.0 Scenario: Mark specific commits, such as software releases. 19. git remote Manages connections to remote repositories. git remote add git remote -v # List remotes Example: git remote add origin https://github.com/user/repo.git Scenario: Set up or view connections to remote repositories. 20. git cherry-pick Appli
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
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
Example:
git clone https://github.com/user/repo.git
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
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 # Add a specific file
git add . # Add all changes
Example:
git add index.html
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"
Example:
git commit -m "Add homepage"
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
Example:
git log --oneline
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 # Create a new branch
Example:
git branch feature-login
Scenario: Create a new branch for a feature to work independently.
8. git checkout
Switches between branches or restores files.
git checkout
Example:
git checkout main
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
Example:
git switch feature-login
Scenario: Use this to switch branches more intuitively.
10. git merge
Combines the changes from one branch into the current branch.
git merge
Example:
git merge feature-login
Scenario: Integrate a completed feature branch into the main branch.
11. git fetch
Downloads updates from a remote repository without merging them.
git fetch
Example:
git fetch origin
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
Example:
git pull origin main
Scenario: Use this to synchronize your local repository with the remote.
13. git push
Uploads your local changes to a remote repository.
git push
Example:
git push origin main
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 # Compare branches
Example:
git diff HEAD
Scenario: Inspect what changes were made before committing.
15. git reset
Unstages files or reverts commits.
git reset # Unstage a file
git reset --hard # Reset to a specific commit
Example:
git reset --hard HEAD~1
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
Example:
git stash
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
Example:
git rebase main
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
git tag -a -m "Message"
Example:
git tag v1.0
Scenario: Mark specific commits, such as software releases.
19. git remote
Manages connections to remote repositories.
git remote add
git remote -v # List remotes
Example:
git remote add origin https://github.com/user/repo.git
Scenario: Set up or view connections to remote repositories.
20. git cherry-pick
Applies a specific commit from another branch.
git cherry-pick
Example:
git cherry-pick abc123
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
Example:
git blame app.js
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
Example:
git archive --format=zip HEAD > project.zip
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
Example:
git clean -fd
Scenario: Clean up unwanted files in the working directory.
24. git show
Displays detailed information about a specific commit.
git show
Example:
git show abc123
Scenario: Inspect details of a particular commit.
25. git revert
Creates a new commit that undoes changes introduced by a previous commit.
git revert
Example:
git revert abc123
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! :)
What's Your Reaction?