Essential Git Commands Cheat Sheet

Essential Git Commands

Q1. Initializing a Repository and Adding a File

mkdir your_project_directory || cd your_project_directory || git init || touch your_new_file.txt || git add . || git commit -m "Initial commit - adding a new file"

Q2. Creating and Merging a Feature Branch

git branch feature-branch || git checkout -b feature-branch || git checkout master || git merge feature-branch || git add . || git commit -m "Merge feature-branch into master" || git branch -d feature-branch

Q3. Stashing Changes

git stash save "Your stash message" || git checkout your-desired-branch || git stash apply || git stash apply stash@{1}

Q4. Cloning a Repository

git clone git@github.com:example/repo.git

Q5. Rebasing a Branch

git fetch || git rebase origin/your-branch || git rebase -i origin/your-branch || git rebase --continue || git rebase --abort || git push origin your-branch --force

Q6. Merging with a Custom Commit Message

git merge feature-branch -m "Your custom commit message" || vi text.txt || git add text.txt || git commit -m "edited file in the feature branch" || git checkout master || git merge feature-branch | git log

Q7. Tagging a Commit

git tag v1.0 <commit_hash> || git tag v1.0 HEAD || git push origin v1.0

Q8. Cherry-Picking a Commit

git branch source-branch || git checkout source-branch || vi text.txt || git add text.txt || git commit -m "first commit in the source branch" || vi text.txt (Again make some changes to it) || git add text.txt || git commit -m "second commit in the source branch" || git log || git checkout master || git cherry-pick (commit-id) || vi text.txt

Q9. Showing Commit Details

git show <commit-id>

Q10. Filtering Commit History

git log --author="JohnDoe" --since="2023-01-01" --until="2023-12-31"

Q11. Viewing Recent Commits

git log -n 5

Q12. Reverting a Commit

git revert abc123