Git FAQ — Frequently Asked Questions

2 minute read

Git FAQ


Q: What’s the difference between git pull and git fetch?

git fetch downloads changes from the remote but does NOT change your local branches. git pull fetches AND merges (or rebases) the remote changes into your current branch. When in doubt, fetch first to inspect before merging.


Q: How do I undo a git pull?

git reset --hard ORIG_HEAD

ORIG_HEAD is automatically set to where HEAD was before the pull.


Q: What’s the difference between origin and upstream?

origin is the conventional name for your fork’s remote. upstream is the conventional name for the original repository you forked from. You can name them anything, but these conventions are universal.


Q: Can I have multiple remotes?

Yes. A single local repository can have as many remotes as you want:

git remote add production git@prod-server:repo.git
git push production main

Q: Why does Git say “refusing to merge unrelated histories”?

This happens when two repos were initialised independently. Fix:

git pull origin main --allow-unrelated-histories

Q: What is a “detached HEAD” and is it dangerous?

Detached HEAD means HEAD points directly to a commit rather than a branch. It’s fine for browsing old code, but any commits you make won’t belong to a branch and will be lost when you switch branches. Fix: git switch -c new-branch-name before committing.


Q: Should I use git merge or git rebase to update my feature branch?

Either works. git rebase main gives a cleaner linear history. git merge main is simpler and preserves the true timeline. Many teams prefer rebase for feature branches — just never rebase commits that others have pulled.


Q: How do I see all commits that changed a specific file?

git log --follow -- path/to/file.py
# --follow tracks the file through renames

Q: How do I clone only one branch (shallow clone)?

git clone --branch feature/login --single-branch --depth 1 <url>

Q: What does HEAD~1 vs HEAD^1 mean?

HEAD~1 means one commit before HEAD (following the first parent). HEAD^1 means the first parent of HEAD. They’re equivalent for normal commits. They differ for merge commits: HEAD^2 means the second parent (the merged branch’s tip), while HEAD~2 means two commits back following the first parent.


Q: How do I fix “error: failed to push some refs to…”?

The remote has commits your local branch doesn’t have. Usually:

git pull --rebase origin main   # fetch + rebase
git push origin main

Q: Is there a way to undo a git push?

You can revert (add a counter-commit) or, on your own branches, force-push after resetting. Never force-push to shared branches.

# Safe: add a revert commit
git revert HEAD
git push origin main

# Risky: force-push (own branch only)
git reset --hard HEAD~1
git push --force-with-lease origin feature/my-branch

← Next Steps ← Tutorial Home

Updated: