GitHub for Dummies: Practical Use Cases
3 min readNov 6, 2024
- Reverting a Mistake in the Last Commit
- Scenario: You accidentally committed incorrect changes in your last commit. How would you undo the last commit while keeping the changes in your working directory?
- Solution: Use
git reset --soft HEAD~1
to undo the commit but keep the changes staged, orgit reset --mixed HEAD~1
to undo the commit and unstage the changes.
2. Resolving Merge Conflicts
- Scenario: You are merging a feature branch into
main
, but a merge conflict arises. How would you resolve it? - Solution: Run
git status
to locate conflicting files. Manually edit these files to resolve conflicts, then usegit add [file]
to mark them as resolved, and complete the merge withgit commit
.
3. Rebasing to Clean Up Commit History
- Scenario: You want to clean up your commit history by squashing some commits together before merging into
main
. What would you do? - Solution: Use
git rebase -i HEAD~[number]
to start an interactive rebase on the last few commits. Choosesquash
for the commits you want to combine.
4. Creating and Pushing a New Branch
- Scenario: You want to create a new branch for a new feature and push it to the remote repository. What commands would you use?
- Solution: Use
git branch [new-branch]
to create the branch,git checkout [new-branch]
to switch to it, andgit push -u origin [new-branch]
to push it to the remote repository.
5. Undoing Changes in a File
- Scenario: You have made changes to a file that you don’t want to keep. How would you discard these changes in Git?
- Solution: Use
git checkout -- [file]
to revert the file to the last committed state.
6. Cherry-Picking Specific Commits
- Scenario: You need a specific commit from another branch but don’t want to merge the entire branch. What would you do?
- Solution: Use
git cherry-pick [commit-hash]
to apply that specific commit to your current branch.
7. Stashing and Applying Changes
- Scenario: You need to switch branches but have uncommitted changes you don’t want to commit yet. How would you handle this?
- Solution: Use
git stash
to save your changes temporarily, then switch branches. When you’re ready to retrieve your changes, usegit stash pop
.
8. Finding a Commit That Introduced a Bug
- Scenario: You want to identify the commit that introduced a bug in the code. How would you locate it?
- Solution: Use
git bisect start
to begin a binary search. Mark the current (buggy) commit withgit bisect bad
and a known good commit withgit bisect good
, and follow Git's instructions to test and identify the exact commit.
9. Resetting to a Previous Commit
- Scenario: You realize that recent commits are incorrect, and you want to reset the branch to a previous commit. How would you do this?
- Solution: Use
git reset --hard [commit-hash]
to move the branch to the specified commit, discarding all changes after it.
10. Pulling Without Overwriting Local Changes
- Scenario: You have local changes that you don’t want to commit, but you need to pull updates from the remote branch. How would you do this?
- Solution: Use
git stash
to save your changes temporarily, rungit pull
, then usegit stash pop
to restore your local changes.
11. Recovering a Deleted Branch
- Scenario: You accidentally deleted a branch locally. How can you recover it?
- Solution: Use
git reflog
to locate the branch’s last commit hash, then rungit checkout -b [branch-name] [commit-hash]
to restore it.
12. Forking and Syncing with Upstream
- Scenario: You’ve forked a repository and want to keep your fork in sync with the original (upstream) repository. How would you do it?
- Solution: Add the original repo as a remote with
git remote add upstream [upstream-URL]
, then fetch the latest changes withgit fetch upstream
and merge or rebase as needed.
13. Fixing an Accidental Push to the Wrong Branch
- Scenario: You mistakenly pushed changes to the wrong branch in the remote repository. How do you fix this?
- Solution: First, make sure you have the correct branch locally, then remove the wrong commit using
git reset HEAD~1
or another similar method, and push the changes withgit push --force
.