GIT

Undoing changes and rewriting history

1. BASICS

GIT INIT

Text

 $ ls .git
         HEAD
         config
         description
         hooks
         info
         objects
         refs

GIT objects

1. Commit

2. Tree

3. Blob

4. Annotated tag

Refs

 Ref = Reference

 'user-friendly alias' for a hash

 use                           to see the SHA-1 commit hashes

 $ git log

HEAD

 Git uses HEAD file to store latest commit info

⇒ HEAD file is a symbolic ref for the current branch

2. UNDO

DOn't amend public coMMITS

REWRITING HISTORY

 Ammended commits are new commits

⇒ Old commit is completely removed from history

examples

CRAP, I forgot to branch!

 $ git reset --soft HEAD^

* Changes are unstaged but

still there

 

Urgh, I need to undo last 3 commits!

 $ git reset HEAD~2

* Changes are unstaged but

still there, check git reflog HEAD starts at 0

 

OOps, I did not mean to Do that!

 $ git reset path/to/filename

* Restores the file to the latest version

in HEAD, changes are preserved

 

OOps I did not mean to DELETE that!

 $ git checkout path/to/filename

* Restores the file to the latest version

in HEAD, changes are dropped

 

Aha, I found what introduced the bug!

 $ git revert commit-hash

* Reverts a commit with another commit,

doesn't amend history.

 

OOPS, I didn't mean to commit yet!

 $ git commit --amend

OOPS, I forgot to add a file!

 $ git add filename

 $ git commit --amend --no-edit

1

2

4. rebase

 Moving a branch onto a new base commit 

⇒ Rebasing onto master will take all commits from master and apply your branch commits on top one by one

WHAT IS A REBASE?

 $ git rebase -i master

 $ git rebase -i master

 Let's squash commit 2 into 1

⇒ Let's edit commit 3

Pick, Squash & Edit

Squashing

Squashing: Amending the message

Squashing: save

Edited message 3

Continue rebase

Done!

Enable RERERE

 $ git config --global rerere.enabled true

 reuse recorded solution

⇒ no need to deal with the same rebase conflicts again!

Git reflog

 $ git reflog

⇒ reflog = referene log

⇒ records when the tips of branches and other references were updated in the local repository.

Takeaways

⇒ never rebase after merge

⇒ after code is shared use merge not rebase

⇒ only rebase before pushing

⇒ amend and undo things locally only before pushing

 

Thanks!

Copy of Git: Undoing changes and rewriting history

By Maju Ansari

Copy of Git: Undoing changes and rewriting history

  • 698