The code is written and the tests pass. I just have to commit this last round of changes to my branch. Wait, why does that say committed to main? Did I commit all those changes to main? Arghh! I can’t redo all of this!
Committing changes to the wrong branch, forgetting files, misspelling the comm...
The code is written and the tests pass. I just have to commit this last round of changes to my branch. Wait, why does that say committed to main? Did I commit all those changes to main? Arghh! I can’t redo all of this!
Committing changes to the wrong branch, forgetting files, misspelling the commit message, and needing to undo commits are some of the “advanced” features of Git that we normal people run into way too often and need help with. The fixes are often easy – once you know what they are. But in the heat of the moment, with the deadline (or Friday afternoon) approaching, it isn’t always easy to figure out what magic spell to cast to get Git to do what you need.
We’ll spend some time looking at typical Git situations people get themselves into, and then we’ll demonstrate how to get out of them. This isn’t about Git internals or a Git master’s class – this real-world Git when things aren’t going right. And there will be plenty of time for questions, so bring your “best” Git nightmare scenarios so we can figure out how to recover.
Size: 334.57 KB
Language: en
Added: Jul 18, 2024
Slides: 25 pages
Slide Content
Fixing Git Catastrophes
Gene Gotimer
Principal DevOps Engineer at Praeses, LLC
@OtherDevOpsGene
Misspelled the commit message
•git commit --amend (could add -m 'message')
To change the editor:
•set GIT_EDITOR=vim or
•git config core.editor vim
@OtherDevOpsGene #NebraskaCode2024 3
Forgot to commit a file
•git commit --amend --no-edit
@OtherDevOpsGene #NebraskaCode2024 4
Added extra files
•git status (says how right there)
•git restore --staged filename
@OtherDevOpsGene #NebraskaCode2024 5
Undo changes to file before commit
•git status (says how right there)
•git restore filename or
•git checkout -- filename
@OtherDevOpsGene #NebraskaCode2024 6
Previous commits on a branch, not main
•git checkout -b branchname
•git checkout - (back to the previous branch)
•git reset --hard HEAD~2 (or however many)
•git checkout -
@OtherDevOpsGene #NebraskaCode2024 7
Referring to commits
8
main
HEADHEAD^
HEAD~1
HEAD~2too easy to make a mistake
just use SHA references
@OtherDevOpsGene #NebraskaCode2024
Rename a branch
•git branch -M new_branchname
If you’ve already pushed:
•git push origin --delete old_branchname
@OtherDevOpsGene #NebraskaCode2024 9
Find the branches a commit is on
•git branch --contains SHA (7 characters of the SHA is enough)
•git branch --all --contains SHA (even remotes)
@OtherDevOpsGene #NebraskaCode2024 12
See the branching history
•git log --graph --oneline
•git log --graph --oneline --all (shows branches we aren’t on)
@OtherDevOpsGene #NebraskaCode2024 13
Undo commits
•git revert SHA (creates and commits a change to undo)
•git reset HEAD^ --hard (undoes a commit, --hard loses the changes)
@OtherDevOpsGene #NebraskaCode2024 14
Undo changes to a file
•git checkout SHA filename
@OtherDevOpsGene #NebraskaCode2024 15
I committed a password
•Revoke the password immediately (see https:--howtorotate.com/)
•Relax, knowing it doesn’t matter anymore
If you hate the easy way:
•git reset HEAD~ --soft
•Make the needed changes
•git push origin --force
•Listen to the screams of your teammates that can no longer push since you
rewrote the commit history with --force
@OtherDevOpsGene #NebraskaCode2024 16
Someone rewrote commit history
•git fetch origin
•git checkout main
•git merge
•git checkout -
•git rebase main
@OtherDevOpsGene #NebraskaCode2024 17
Rebase
18
main
@OtherDevOpsGene #NebraskaCode2024
A B C A' B' C'
Redo commit from another branch
•git cherry-pick SHA
@OtherDevOpsGene #NebraskaCode2024 19
Recover a deleted file
•git log --follow -- filename (searches all changes through renames)
•git restore --source SHA filename
@OtherDevOpsGene #NebraskaCode2024 20
Resurrect a deleted branch
•git reflog (find the reference to the tip of the deleted branch)
•git checkout SHA (now in detached state)
•git checkout -b branchname
or
•git checkout -b branchname SHA
@OtherDevOpsGene #NebraskaCode2024 21
I should have branched last week
•git checkout -b branchname SHA
@OtherDevOpsGene #NebraskaCode2024 22
Save changes for later
•git stash push -m "message"
•git stash list (to see stashes)
•git stash pop (bring back the last stash)
•git stash pop stash@{n} (bring back an older stash)
•git stash branch newbranchname stash@{n}
(I should have branched, not stashed)
@OtherDevOpsGene #NebraskaCode2024 23
Undo work-in-progress
•git clean -df (gets rid of all untracked files)
•git reset --hard origin/branch
(reverts all changes so it looks like the remote branch)
@OtherDevOpsGene #NebraskaCode2024 24
Other catastrophes?
Gene Gotimer
Principal DevOps Engineer at Praeses, LLC
@OtherDevOpsGene