Master Git with These Essential Commands! 🖥️
1. Basic Git Commands
📌 git init – Initialize a Git repository.
📌 git clone <repo_url> – Clone a repository to your local machine.
📌 git status – Check the current state of your repository.
📌 git add <file> – Stage changes for commit.
📌 git commit -m "message" – Save changes with a commit message.
📌 git push – Upload local commits to a remote repository.
📌 git pull – Download and merge changes from a remote repository.
2. Branching & Merging
📌 git branch – List branches.
📌 git branch <branch_name> – Create a new branch.
📌 git checkout <branch_name> – Switch to another branch.
📌 git checkout -b <branch_name> – Create and switch to a new branch.
📌 git merge <branch_name> – Merge changes from another branch.
3. Remote Repository Management
📌 git remote -v – View remote repositories.
📌 git remote add <name> <url> – Add a remote repository.
📌 git push origin <branch> – Push changes to a remote branch.
📌 git pull origin <branch> – Pull changes from a remote branch.
4. Viewing & Comparing Changes
📌 git log – View commit history.
📌 git log --oneline – View a simplified commit history.
📌 git diff – Show changes between commits or branches.
📌 git blame <file> – Show who changed each line of a file.
5. Undoing Changes & Resetting
📌 git reset --soft HEAD~1 – Undo last commit but keep changes staged.
📌 git reset --hard HEAD~1 – Undo last commit and remove changes.
📌 git revert <commit_id> – Revert changes without losing history.
📌 git restore <file> – Undo changes in a file.
6. Stashing Changes
📌 git stash – Temporarily save changes without committing.
📌 git stash list – View saved stashes.
📌 git stash pop – Apply the last stash and remove it.
7. Tags & Releases
📌 git tag <tag_name> – Create a tag for a specific commit.
📌 git tag -a <tag_name> -m "message" – Annotated tag with description.
📌 git push origin --tags – Push all tags to the remote repository.
8. Git Hooks & Automation
📌 git pre-commit – Runs scripts before a commit.
📌 git post-commit – Runs scripts after a commit.
📌 git pre-push – Runs checks before pushing to remote.
9. Handling Merge Conflicts
📌 git mergetool – Open a merge conflict resolution tool.
📌 git merge --abort – Cancel a merge.
📌 git diff <branch_1> <branch_2> – Compare differences between branches.
10. Git & DevOps
📌 git fetch --all – Fetch changes from all remotes.
📌 git rebase <branch> – Reapply commits on top of another branch.
📌 git cherry-pick <commit_id> – Apply a specific commit from another branch.