Git reset: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 9: Line 9:
  git reset ''file-name''
  git reset ''file-name''


=Discard All Local Changes=
=<span id='Discard_All_Local_Changes'></span>Discard All Local Changes to Tracked Files=


git reset --hard HEAD
<syntaxhighlight lang='bash'>
git reset --hard HEAD
</syntaxhighlight>


Resets the index and the working tree to the specified commit, by discarding all local changes to the tracked files.
Resets the index and the working tree to the specified commit, by discarding all local changes to the tracked files. The following files are restored:
* tracked files that are locally modified
* tracked files that are locally modified and committed
* tracked files that are locally deleted
* tracked files that are locally deleted and committed
 
Note that newly added and untracked files and directories are not removed. Use [[git clean]] for this.


=<span id='Use_Cases'></span>Other Use Cases=
=<span id='Use_Cases'></span>Other Use Cases=
Line 27: Line 35:
==Drop Commits from the Local Feature Branch==
==Drop Commits from the Local Feature Branch==


This may be necessary if an unwanted merge has been performed, and we want to get rid of it.
This may be necessary if an unwanted merge has been performed, and we want to get rid of it (in the example below, we get rid of exactly one commit, which is the HEAD of the branch):


  git reset --hard HEAD~1
  git reset --hard HEAD~1
  git push --force
  git push --force
==Reverting a File to a Specific Commit==
{{Internal|Git_checkout#Reverting_Individual_File.28s.29_to_a_Specific_Past_Commit|Reverting and Individual File to a Specific Past Commit}}

Latest revision as of 04:40, 3 December 2020

Internal

Overview

git reset resets the current working state to the last commit, or the specified commit.

git reset file-name

Discard All Local Changes to Tracked Files

git reset --hard HEAD

Resets the index and the working tree to the specified commit, by discarding all local changes to the tracked files. The following files are restored:

  • tracked files that are locally modified
  • tracked files that are locally modified and committed
  • tracked files that are locally deleted
  • tracked files that are locally deleted and committed

Note that newly added and untracked files and directories are not removed. Use git clean for this.

Other Use Cases

Local and remote origin branches have diverged without activity on the local branch

Local and remote origin branches have diverged without activity on the local branch

Resync the Feature Branch with the State of its Tracking Branch

git reset --hard origin/<feature-branch>
git reset --hard origin/task/test

Drop Commits from the Local Feature Branch

This may be necessary if an unwanted merge has been performed, and we want to get rid of it (in the example below, we get rid of exactly one commit, which is the HEAD of the branch):

git reset --hard HEAD~1
git push --force

Reverting a File to a Specific Commit

Reverting and Individual File to a Specific Past Commit