Git Gud
Git, the golden child of version control, is touted as a developer’s best friend. But let’s keep it real. When things go south in Git-land, it’s not a leisurely stroll—it’s more like navigating an obstacle course blindfolded. You’ve got merge conflicts chucking grenades, accidental commits blowing up in your face, and a detached HEAD state leaving you wandering in the dark.
Git is a breeze until it’s not, and these scenarios are the nitty-gritty, real-life moments that turn your coding utopia into a war zone. So, gear up because we’re diving into the situations that can either mold you into a Git maestro or have you dropping some choice words at your screen. Welcome to the battlefield, where Git’s simplicity is just the calm before the storm.
Merge Conflicts
Git’s merge conflicts can disrupt your peaceful coding session, marked by lines decorated with “«««< HEAD” and “»»»> branch.” No need to panic; resolving conflicts is an art. Identify conflicted files with git status
, open them, manually resolve conflicts, and run git add
before committing.
1
2
3
4
5
git status
# Identify conflicted files and open them
# Resolve conflicts manually
git add .
git commit
Accidental Commits
Oops! You added changes that weren’t meant for the latest commit. Fear not; the magical git reset
command is here. Use git reset HEAD~1
to uncommit the last commit while keeping your changes staged.
1
git reset HEAD~1
Forgotten Stash
Stashed changes for a quick branch switch but lost track? No worries; list your stashes with git stash list
and apply the correct one using git stash apply
.
1
2
git stash list
git stash apply stash@{n}
Lost Commit
Lost a commit and frantically scrolling through git log
? Find solace in git reflog
, your time-traveling companion. Recover lost commits using git cherry-pick
or create a new branch to resurrect them.
1
2
3
4
git reflog
git cherry-pick commit-hash
# or
git checkout -b new-branch commit-hash
Detached HEAD State
Wandering in the Git wilderness with a detached HEAD state? Reattach to a branch with git checkout -b new-branch
to create a new branch or git checkout branch-name
to switch to an existing branch.
1
2
3
git checkout -b new-branch
# or
git checkout branch-name
Remote Repository Woes
Facing rejection while pushing changes? Pull changes with git pull
, resolve conflicts, and then push again. For solo contributors, use git push -f
cautiously for force pushing.
1
2
3
4
5
git pull origin branch-name
# Resolve conflicts
git push
# or force push if necessary
git push -f
Unwanted Files in Repository
Oops, sensitive files got committed! Undo the mistake with git rm --cached file.txt
to untrack the file, add it to your .gitignore
, and commit the changes.
1
2
3
4
git rm --cached file.txt
# Add to .gitignore
git add .
git commit -m "Remove sensitive file"
I committed and realized I needed to fix something before pushing!
1
2
git add .
git commit --amend
Amend the last commit to include your changes without creating a new commit.
I committed and pushed, but forgot to pull remote first!
1
2
git pull --rebase origin branch-name
git push
Rebase your changes on top of the remote changes to avoid unnecessary merge commits.
I realized I have way too many commits and I want to reduce the number of commits!
1
git rebase -i HEAD~n
Interactively rebase the last ‘n’ commits to squash, edit, or reorder them.
I committed and pushed, but forgot to pull remote first and now I have merge conflicts!
1
2
3
4
5
git pull origin branch-name
# Resolve conflicts
git add .
git rebase --continue
git push
Pull changes, resolve conflicts, and continue with the rebase.
I committed and pushed, but forgot to add a file!
1
2
3
git add missed-file
git commit --amend
git push --force
Amend the last commit to include the missed file and force push.
I just realized I deleted a file I shouldn’t have!
1
2
3
git checkout HEAD^ -- deleted-file
git commit --amend
git push --force
Recover the deleted file from the last commit and amend the commit.
I just realized I made a mistake in a commit message!
1
git commit --amend
Amend the last commit to modify the commit message.
I just realized I committed to the wrong branch!
1
2
git branch new-branch
git reset --hard HEAD~1
Create a new branch and reset to the previous commit.
I accidentally committed something to master that should have been on a brand new branch!
1
2
3
git branch new-branch
git reset --hard HEAD~1
git checkout new-branch
Create a new branch, reset master, and switch to the new branch.
I need to see all who have committed, the date they committed, and the commit message!
1
git log --pretty=format:"%h - %an, %ar : %s"
Display a condensed log with commit hash, author, date, and message.
I need to see what commits were made on a branch and for what reason!
1
git log branch-name --pretty=format:"%h - %s"
Show the log for a specific branch with commit hashes and messages.
I need to see what changes were made between two commits!
1
git diff commit-hash1 commit-hash2
View the differences between two specific commits.
I need to see all commits made by a specific author!
1
git log --author="Author Name"
Filter the log to display commits by a specific author.
I need to see all commits made on a specific date!
1
git log --since="YYYY-MM-DD" --until="YYYY-MM-DD"
Retrieve commits made within a specific date range.
I need to see all commits made on a specific file!
1
git log -- filename
Display the log for a specific file.
I need to see all commits made on a specific line of a file!
1
git log -L start, end:filename
Show the log for changes made to a specific range of lines in a file.
I tried to run a diff but nothing happened!
1
git diff
Ensure you’re in the correct branch and have uncommitted changes to see the diff.
I tried to merge conflicts and broke everything!
1
git merge --abort
Abort the merge, resolve conflicts again, and proceed.
I want to merge a branch, but I want to use my changes, not the other branch’s changes.
1
2
git checkout your-branch
git merge -s ours branch-to-merge
Merge, favoring changes from your branch.
I made a lot of major changes to a file and now need something from an older version!
1
git checkout commit-hash -- path/to/file
Retrieve a specific version of a file from a previous commit.
Fuck it, I’m over it…
1
git reset --hard HEAD
Reset to the last commit, discarding all changes. Use with caution!
Conclusion
So, there you have it—the unfiltered reality of navigating Git’s terrain. It’s a rollercoaster of ease and complexity, where you dance with the commands until shit hits the fan. But fear not, fellow coder, because it’s in these chaotic scenarios that you truly earn your stripes. Git may throw curveballs, but armed with the knowledge to tackle merge conflicts, salvage from accidental commits, and find your way out of detached HEAD states, you’ll emerge from the battlefield not just unscathed, but as a bona fide Git badass. Cheers to the unpredictable journey of version control, where every hiccup is a chance to level up.
Git gud and hack the planet, my friends!