Fixing Git Catastrophes - Nebraska.Code()

ggotimer 27 views 25 slides Jul 18, 2024
Slide 1
Slide 1 of 25
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25

About This Presentation

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...


Slide Content

Fixing Git Catastrophes
Gene Gotimer
Principal DevOps Engineer at Praeses, LLC
@OtherDevOpsGene

Git Config
•git config --global --list
https:--gist.github.com/OtherDevOpsGene/8be4f41d99128eb018bf9f04d44b225c
@OtherDevOpsGene #NebraskaCode2024 2

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

Searching commit messages
•git log --grep "JUnit"
•git log -i --grep "junit" (case-insensitive)
@OtherDevOpsGene #NebraskaCode2024 10

Searching source code/files
•git grep -i "junit" (case-insensitive)
@OtherDevOpsGene #NebraskaCode2024 11

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
Tags