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

1. Checkout

The git checkout  moves the HEAD ref pointer to a specified commit. To demonstrate this, consider the following example:


Now you can change the HEAD with:

or

you can find all commits again by:

For instance:

1.2 Detached HEAD

Any checkout of a commit that is not the name of one of your branches will get you a detached HEAD. When HEAD is detached, commits work like normal, except no named branch, gets updated. (You can think of this as an anonymous branch.)

1.3 Cleaning up git environment and rollbacking local changes

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

to rollback changes for a particular file:

to reset or revert a file to a specific revision

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:

-f:  clean untracked files

-d: also remove directories

-x: remove ignored as well as non-ignored files

 

2. Git reset

How to undo a commit in Git when you haven’t pushed it yet? The answer is git reset. The usage of git reset is to undo changes that haven’t been shared with anyone else, for instance when you’ve started working on a feature. reset 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 on:

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

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.

Refs: [1]

2.1 A Complete Example

Now if we run the followings:

Git permanently deletes such commits (D Added) after 30 days. You can recover that by:

  • HEAD~2 : 2 commits older than HEAD
  • HEAD^2 : the second parent of HEAD, if HEAD was a merge, otherwise illegal
  • HEAD@{2} : refers to the 3rd listing in the overview of git reflog
  • HEAD~~ : 2 commits older than HEAD
  • HEAD^^ : 2 commits older than HEAD

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 their status:

Finally, lets’ push changes by:

3. git revert

How to undo a push in Git when you have pushed your commits?  The answer is Git revert.This command has the ‘undo’ type (not a traditional undo operation). It determines how to reverse the modifications made by the commit and adds a new commit with the resulting inverted content, rather than removing the commit from the project history. Due to the integrity of your revision history and the need for trustworthy collaboration, this keeps Git from losing its history.

 

Examples:

3.1 A Complete Example

Now, let’s revert the commit that added the B1, B2. As you can see, we have modified B1 in the last commit, so we would have a conflict:

to resolve the conflict:

4. amend

This will fix your most recent commit message

Example

some important notes:

git add -A is equivalent to

Refs: [1], [2]

Searching for a file in all branches

Un-stage a file:

Clean everything that has been added to the repository and has not been staged:

 

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
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x