Thursday, 25 July 2019

Git-Part 4: Undoing and Discarding Changes

post

Undoing: Discarding Unstaged Changes

● Scenario:

You edited file(s) but you have not yet staged them ( i.e. git add ).

How do I discard the changes?

● Solution:

Command: git checkout

● Tips:

◆ This command is an overloaded term. "Checking out" means that

you are putting your working directory into an already "known" state

(e.g. HEAD, commit, tag, branch).

◆ Run git diff beforehand to ensure you aren't throwing

away wanted changes

◆ You can checkout several files at once.

git checkout .

This will checkout all files under the current working directory

● Example:

[anandg anandg@ /home/anandg/testrepo/lib (master)]$ git status

On branch master

Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:

(use "git add ..." to update what will be committed)

(use "git checkout -- ..." to discard changes in working directory)

modified: file1.py

no changes added to commit (use "git add" and/or "git commit -a")

[anandg anandg@ /home/anandg/testrepo/lib (master)]$ git checkout file1.py

[anandg anandg@ /home/anandg/testrepo/lib (master)]$ git status

On branch master

Your branch is up-to-date with 'origin/master'.

nothing to commit, working tree clean

[anandg anandg@ /home/anandg/testrepo/lib (master)]$

 

Undoing: Discarding Staged Changes

● Scenario:

You edited file(s) and have already staged them ( i.e. git add ).

How do I discard the changes?

● Solution:

Command: git reset HEAD

● Tips:

◆ HEAD is a reference to the last commit of the current checked out branch

◆ After running git reset HEAD , your file(s) will merely be

unstaged but the changes will still be present.

◆ This provides another opportunity to review the changes

(to either checkout or re-add).

● Example:

[anandg anandg@ /home/anandg/testrepo/lib (master)]$ git status

On branch master

Your branch is up-to-date with 'origin/master'.

Changes to be committed:

(use "git reset HEAD ..." to unstage)

modified: file1.py

[anandg anandg@ /home/anandg/testrepo/lib (master)]$ git reset HEAD file1.py

Unstaged changes after reset:

M lib/file1.py

[anandg anandg@ /home/anandg/testrepo/lib (master)]$ git status

On branch master

Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:

(use "git add ..." to update what will be committed)

(use "git checkout -- ..." to discard changes in working directory)

modified: file1.py

no changes added to commit (use "git add" and/or "git commit -a")

[anandg anandg@ /home/anandg/testrepo/lib (master)]$

 

Undoing: Discarding Published Changes

● Scenario:

You want to undo a change that you actually committed.

How do I discard the changes?

● Solution:

Command: git revert

● Tips:

◆ git revert undoes a change by undoing all of the diffs from the

original commit

◆ git revert actually does this with a new commit. It does NOT remove

the original commit from history. You may have to revert a revert!

◆ This is why it is important to make small, unit-sized commits!!!

● Example:

[anandg anandg@ /home/anandg/testrepo (master)]$ git log

commit d307e42e37dfc1845ab606c287596afc31da4aea (HEAD -> master, origin/master,

origin/HEAD)

Author: Anand G anandg@google.com

Date: Tue Oct 3 14:42:17 2017 -0400

Added --continue flag to file2.py to continue on failure. Fixes #261

[anandg anandg@ /home/anandg/testrepo (master)]$ git revert d307

[master 3b06595] Revert "Added --continue flag to file2.py to continue on

failure. Fixes #261"

1 file changed, 21 insertions(+), 29 deletions(-)

[anandg anandg@ /home/anandg/testrepo (master)]$ git status

On branch master

Your branch is ahead of 'origin/master' by 1 commit.

(use "git push" to publish your local commits)

nothing to commit, working tree clean

 

[anandg anandg@ /home/anandg/testrepo (master)]$ git log

commit 3b065957d07e9fb82b2d902c352dde0799db44e4 (HEAD -> master)

Author: Anand G anandg@google.com

Date: Tue Oct 3 16:13:27 2017 -0400

Revert "Added --continue flag to file2.py to continue on failure. Fixes

#261"

This reverts commit d307e42e37dfc1845ab606c287596afc31da4aea.

commit d307e42e37dfc1845ab606c287596afc31da4aea (origin/master, origin/HEAD)

Author: Anand G anandg@google.com

Date: Tue Oct 3 14:42:17 2017 -0400

Added --continue flag to file2.py to continue on failure. Fixes #261

 

Undoing: Interactively Changing History

● Scenario:

You made some silly commit messages that you want to correct.

Or, you made some commits that you want to combine (partial unit)

● Solution:

Command: git rebase -i/--interactive

● Tips:

◆ git rebase -i brings up an interactive interface that provides one

method to modify GIT history.

◆ You specify a JUST PRIOR to your set of commits.

This allows you to alter the history back to that moment in time.

◆ Because history has been rewritten, you most likely will need to overwrite

history with git push origin --force

* Always use --force with EXTREME caution !!! *

If you've never done this before, ask. Great reference here:

http://willi.am/blog/2014/08/12/the-dark-side-of-the-force-push

● More Tips:

◆ A list of all commits are displayed from oldest to newest and

"replayed" top-down based on COMMAND directives.

◆ Do NOT modify the commit messages at this time.

You merely modify the COMMAND directives.

◆ After saving the COMMAND file, the "replay" will start and you

will be prompted for new commit messages.

◆ The "replay" may require you to resolve merge conflicts.

Simply run git status and follow the instructions.

Commands:

# p, pick = use commit —> Use commit as-is

# r, reword = use commit, but edit the commit message ——> Change commit message

# e, edit = use commit, but stop for amending ——> Pauses interaction and drops one

back to shell to do anything

# s, squash = use commit, but meld into previous commit ——> Combine commits and

commit messages

# f, fixup = like "squash", but discard this commit's log message ——> Combine commits but only use previous commit message

# x, exec = run command (the rest of the line) using shell ——> Invoke a shell command

(e.g. interim unit testing)

# d, drop = remove commit ——> Discard commit

 

Command Directives File:

All commits are shown from oldest-to-newest.

● When you start git rebase -i, all directives are set to "pick". It is your job to change the directives as needed.

● Once you change the directives, save and exit the file and the replay will begin.

● ONLY MODIFY THE DIRECTIVES HERE. NOT THE COMMENTS!!!

They are here as a reference.

[anandg anandg@ /home/anandg/testrepo (master)]$ git rebase -i

pick fc044bb Added initial functionality for FOO

pick f256fd7 Added initial functionality for BARRRR

pick e5ce044 Mistake commit for BAR

pick fb5faca Forgot to add some stuff for BAR

pick db0ea89 Added initial functionality for WIDGET

pick ea1b67f Added advanced functionality for WIDGET

 

Update directives. Saving will start interactive "replay"

[anandg anandg@ /home/anandg/testrepo (master)]$ git rebase -i

pick fc044bb Added initial functionality for FOO

reword f256fd7 Added initial functionality for BARRRR

drop e5ce044 Mistake commit for BAR

fixup fb5faca Forgot to add some stuff for BAR

pick db0ea89 Added initial functionality for WIDGET

squash ea1b67f Added advanced functionality for WIDGET

 

Playback Oldest-to-Newest Top-Down

 

● New History (after interaction):

fc044bb Added initial functionality for FOO

8adbe0f Added initial functionality for BAR

e4b886a Added initial functionality for WIDGET

Added advanced functionality for WIDGET

No comments:

Post a Comment