Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 2Naam in de cursus: Undoing things

Undo changes safely

Mistakes happen: a wrong file staged, a bad commit, a broken change already pushed. Git can undo almost everything — if you choose the right command. The rule: rewrite only what you have not shared; revert what others already have.

  • 5 Lessen
  • 1 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

2.1

Undo changes in files

git restore throws away changes in the working tree, or takes a file out of the staging area.

git restore main.tf
git restore --staged main.tf
git restore --source=HEAD~2 variables.tf
git clean -n
git clean -fd
CommandEffect
git restore FILEBack to the last staged/committed version (changes lost)
git restore --staged FILEUnstage, keep the changes
git restore --source=COMMIT FILEGet the file from any commit
git clean -n / -fdShow / delete untracked files and folders

Let op

git restore and git clean -f delete work that was never committed — there is no undo for that.

Commando's in deze les

2.2

Fix the last commit

Forgot a file or wrote a bad message? --amend replaces the last commit — only if it is not pushed yet.

git add forgotten.tf
git commit --amend --no-edit
git commit --amend -m "Better message"

Commando's in deze les

2.3

Move a branch back with reset

git reset moves the branch pointer to an older commit. The mode decides what happens to your files.

git reset --soft HEAD~1
git reset HEAD~1
git reset --hard HEAD~1
git reset --hard origin/main
ModeCommits after the targetStaging areaWorking tree
--softRemovedKeeps the changes (staged)Kept
--mixed (default)RemovedClearedKept (changes unstaged)
--hardRemovedClearedReset — changes lost

Commando's in deze les

2.4

Undo a pushed commit with revert

git revert makes a new commit that does the opposite of an old one. History stays intact — safe on shared branches.

git log --oneline -5
git revert 3f2a9c1
git revert --no-edit HEAD
git revert -m 1 MERGE_COMMIT
git push

Onthoud

Pushed = shared. On shared branches use revert, never reset + force push.

Commando's in deze les

2.5

Find "lost" commits with reflog

The reflog records where HEAD was. After a bad reset or rebase, the old commits are still there for some weeks.

git reflog
3f2a9c1 HEAD@{0}: reset: moving to HEAD~3
8b7c6d5 HEAD@{1}: commit: Add monitoring module
git reset --hard HEAD@{1}
git branch rescue 8b7c6d5

Op het werk

Before a risky operation, make a backup branch: git branch backup-before-rebase. It costs nothing.

Commando's in deze les

Alle commando's in dit hoofdstuk