Mastering git, Part 3 (Undo local changes, git revert, git reset, git amend and git clean)

Removing a file from the staging area

If you omit the --cached option, it will also delete it from the working tree.

Cleaning up git environment and rollbacking local changes

Once you started working on your local copy you might mess everything and need to clean up or roll back changes for some file, you can use git checkout and git clean

to rollback changes for a particular file:

to rollback changes for the entire local copy while keeping added files:

 

if you just want to remove the file and directories that have been added but not staged:

-d
Remove untracked directories in addition to untracked files.

–dry-run
Don’t actually remove anything, just show what would be done.

-i
Show what would be done and clean files interactively. See “Interactive mode” for details.

This will create a new branch and switch to that:

 

Undo local changes

Git reset

How to undo a commit in Git when no one has pulled your changes? The answer is git reset
git reset will do this job for you, it has three options, hard and soft and mixed.

1) Mixed: This is the default action. Resets the index1 but not the working tree2 (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated.

2) Soft: Does not touch the index file or the working tree at all but resets the head to <commit>. This leaves all your changed files “Changes to be committed”

3) Hard: This option will reset the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded.

1 The git “index” is where you place files you want to commit to the git repository. Some names you may have heard:

  • Index
  • Cache
  • Directory cache
  • Current directory cache
  • Staging area
  • Staged files

Files in the git index don’t get committed to the repository until you use the git commit command.

2 Working tree is what is actually in the files that you are currently working.

git ls-tree only works with git refs, e.g.

Commit could be in one of the following forms:
HEAD{2}
HEAD~2
commit-SHA

Examples:

You can undo your reset by

Since after your first reset your previous HEAD is at one commit before the current one.

git log will be exactly the same as before, but git relog will show you that you have reset the HEAD.

A Complete setup

some important note:

git add -A is equivalent to

Let’s continue our work:

 

The git reflog command records a chronological history of everything you have done in your local repository. Its full output might look like this:

C.txt and D.txt will remain (soft)

C.txt and D.txt will be deleted.

To see which files have been added and status:

Finally, lets’ push changes by:

Git revert

How to undo a push in Git when someone might have pulled your changes? The answer is Git revert. If you’ve already pushed your changes and someone else pulled in those changes, you should not use git reset to undo changes, use git revert instead.

Examples:

Let’s follow our work:

amend

This will fix your most recent commit message

Example

Refs: [1]

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x