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.