Git is a powerful distributed version control system that has become a standard tool for developers worldwide. Understanding Git’s core concepts and mastering its most commonly used commands can greatly improve your development workflow, collaboration efficiency, and code management. This guide provides a concise yet comprehensive overview of essential Git commands covering basics, branch management, remote operations, and advanced features.
⚙️ Git Basic Commands #
| Command | Description | Example |
|---|---|---|
git init |
Initializes a new Git repository in the current directory. | git init |
git add |
Adds files to the staging area in preparation for commit. | git add . |
git commit |
Commits staged files with a descriptive message. | git commit -m "Initial commit" |
git status |
Shows the current state of the working directory and staging area. | git status |
git diff |
Shows file differences between working directory, staged area, and commits. | git diff |
git show |
Displays details of a specific commit. | git show <commit_id> |
git log |
Displays the commit history. | git log |
🌿 Git Branch Management #
Create, Switch, and Delete Branches #
- Create a new branch
git branch new_branch - Switch to a branch
git checkout new_branch - Delete a branch
git branch -d branch_name
Restore Files with git checkout
#
- Switch to another branch
git checkout new_branch - Restore all files to last commit
git checkout .
Merge Branches #
- Merge a branch into the current one
git merge new_branch
🌎 Git Remote Repository Operations #
Manage Remote Repositories #
git remote add origin https://github.com/user/repo.git
# Adds a remote repository named 'origin'
Pull and Push Changes #
- Fetch and merge remote changes
git pull - Push commits to a remote branch
git push origin master
🛠️ Git Advanced Operations #
git stash
#
Temporarily saves modifications in the working directory and restores the last committed state. Useful when switching tasks.
- Save changes:
git stash - Restore saved changes:
git stash pop
git revert
#
Creates a new commit that reverses a previous commit without altering history.
- Undo commit:
git revert <commit_id>
git reset
#
Moves the HEAD pointer to a specific commit.
Warning: --hard will discard commits after the reset point.
- Hard reset:
git reset --hard <commit_id>
git cherry-pick
#
Applies a specific commit from one branch onto another.
- Cherry-pick commit:
git cherry-pick <commit_id>
📌 Summary #
Git provides powerful, flexible tools for version control—from basic commands to advanced operations. By mastering the essential commands covered here, you can work more efficiently, collaborate more effectively, and manage code with confidence. With continued practice, you’ll unlock even more of Git’s capabilities and become highly proficient in modern software development workflows.