Skip to content

Latest commit

 

History

History
98 lines (78 loc) · 2.78 KB

GIT_CONFLICTS.md

File metadata and controls

98 lines (78 loc) · 2.78 KB

Common QnA

How can I know if I’m in a bad situation?
  • Run gitk or gitk --all to visualize the commit tree.
  • Check if git log shows any merge commits, indicating a lack of proper synchronization with the upstream branch.
How to Hard Sync Master with Upstream

To hard sync your local master branch with the upstream master branch, follow these steps in your main branch:

  • git remote add upstream /url-to-original-repo
  • git fetch upstream
  • git reset --hard upstream/main
  • git push origin main --force

Better commits

  • Avoid committing unnecessary files:
    • Avoid using git commit -a, which commits everything.
  • Use targeted commit commands:
    • Use git add to stage specific files.
    • Use git add -p for a patch mode to review changes before staging.
  • Ensure changes are correct before committing:
    • Use git diff --cached to display staged changes.
    • Use git commit -v to view diffs while writing the commit message.

If you want to make sure about what you’re committing, use git diff --cached to display the ready-to-commit changes. Also, I like to use git commit -v, which displays the diff as a comment, so that I can view it while writing the commit message.

Edit a commit
  • git reset --soft HEAD^: Reset to the previous commit without changing files.
  • Edit the necessary changes.
  • git commit -a -v -c ORIG_HEAD: Recommit with the same message and verify changes.
  • After pushing, use force push: git push -f.
What should I do if I’m in a bad situation?
  • Rebase:
    • Fetch the latest changes: git fetch main
    • Rebase onto the upstream branch: git rebase upstream/main
  • Remove merge commits:
    • Fetch latest changes: git fetch upstream
    • Discard all changes and reset: git reset --hard upstream/main
  • Keep your changes:
    • Create a backup branch: git branch branchname
    • Safely reset your branch
    • Pull changes back to master: git cherry-pick branchname 1234567890abcdef1234567890abcdef12345678
Recovering from a bad merge or accidental deletion
  • git reflog
  • git reset HEAD@{index}
Amending the last commit
  • git commit --amend --no-edit
Changing the last commit message
  • git commit --amend
Undoing a commit from several commits ago
  • git log
  • git revert [saved hash]