Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. What is HEAD?
  2. HEAD is a reference to the last commit in the currently check-out branch.
  3.  
  4.  
  5.  
  6.  
  7. Difference Between a git reset and git revert
  8. It’s important to understand the difference between resetting vs reverting commits when using Git.
  9.  
  10. The git reset command undoes commits by removing previous commits from the repository and resetting the Git HEAD to an earlier commit. Some Git history might be lost by doing this, depending on what option is used.
  11.  
  12. The git revert command undoes commits by creating a new commit that represents an earlier state of the repository. No Git history will be lost by doing this.
  13.  
  14. Already pushed changes and someone else pulled those changes? You should not use git reset to undo changes, you should use git revert as described in the Git rollback commit guide instead.
  15.  
  16. Using the git reset Command to Undo a Commit
  17. For the following examples, assume our local Git tree looks like this:
  18.  
  19. X
  20. |
  21. A - B - C
  22.  
  23. The X represents our local, uncommitted change to our Git HEAD, indicated by C.
  24.  
  25. To undo the commit named “C “due to a mistake, we can run a git reset command.
  26.  
  27. Depending on file tracking needs, you’ll want to use the git reset command with different flags.
  28.  
  29. Case 1: Use git reset and Remove Files From the Staging Area
  30. We can use the git reflog command to see our Git history.
  31.  
  32. We can use git ls-files to see the currently staged files for our project:
  33.  
  34. $ Git reflog
  35. f326d13 HEAD@{0}: commit: Added C
  36. 358d535 HEAD@{1}: commit: Added B
  37. 00b61d5 HEAD@{2}: commit (initial): Added A
  38.  
  39. $ Git ls-files
  40. A
  41. B
  42. C
  43. X
  44. To keep any tracked files since commit C, but remove them from the Git index, you can run:
  45.  
  46. Git reset HEAD~1
  47. You can use the Git shorthand of HEAD~1 to remove the commit one commit before HEAD.
  48. If you used HEAD~5, this would remove the commit that is five commits before HEAD.
  49. Changes to the Git index, also called the “staging area,” will be lost, so the Git tree would now be:
  50.  
  51. X
  52. |
  53. A-B
  54. Which we can confirm with these commands:
  55. $ Git reflog
  56. 358d535 HEAD@{0}: reset: moving to HEAD~1
  57. f326d13 HEAD@{1}: commit: Added C
  58. 358d535 HEAD@{2}: commit: Added B
  59. 00b61d5 HEAD@{3}: commit (initial): Added A
  60.  
  61. $ Git ls-files
  62. A
  63. B
  64.  
  65. $ Git status
  66. On branch master
  67. Untracked files:
  68. (use "Git add ..." to include in what will be committed)
  69.  
  70. C
  71. X
  72.  
  73. When just using git reset with no flags, we don’t remove any files; they are just un-staged:
  74.  
  75. $ ls
  76. A B C X
  77. You do end up removing the log entry for the commit you reset, however:
  78.  
  79. $ Git log
  80. commit 358d535bc5e06730e61d272be34a6d0e568f42af
  81. Author: User <user@example.com>
  82. Date: Tue Feb 10 13:54:55 2015 -0500
  83.  
  84. Added B
  85.  
  86. commit 00b61d53fe06ee672fa0497b175fb7bd89e26b72
  87. Author: User <user@example.com>
  88. Date: Tue Feb 10 13:54:55 2015 -0500
  89.  
  90. Added A
  91. Case 2: Use git reset –soft and Preserve Changes to Files
  92. If you use the –soft flag, any changes to tracked files and the Git index are preserved:
  93.  
  94. 1
  95. Git reset --soft HEAD~1
  96. Any files that were staged remain so, and no files are physically removed:
  97.  
  98. $ Git ls-files
  99. A
  100. B
  101. C
  102. X
  103.  
  104. $ ls
  105. A B C X
  106.  
  107. $ Git status
  108. On branch master
  109. Changes to be committed:
  110. (use "Git reset HEAD ..." to unstage)
  111.  
  112. new file: C
  113. new file: X
  114. Using the –soft flag, you still end up removing the log entry for the commit you reset.
  115.  
  116. Case 2: Use git reset –hard and Remove All Changes to Files
  117. If you use the –hard flag, any changes to tracked files and the Git index are lost:
  118.  
  119. 1
  120. 2
  121. $ Git reset --hard HEAD~1
  122. HEAD is now at 3bf1b55 Added B
  123. All files after the commit you reset to are un-staged and physically removed:
  124.  
  125. $ Git ls-files
  126. A
  127. B
  128.  
  129. $ ls
  130. A B
  131.  
  132. $ Git status
  133. On branch master
  134. nothing to commit, working directory clean
  135. You again remove the Git log entry for the commit that you reset.
  136.  
  137. Try to Use git revert When Commits Are Already Pushed
  138. As stated above, 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.
  139.  
  140. However, if you really want to, you can apply the same steps as in the previous section to delete some previous Git commits.
  141.  
  142. After that, you can do a git push -f to use the force option. Again, this is not recommended, because it can create serious conflicts between the various repository states of other Git users.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement