Skip to content

Instantly share code, notes, and snippets.

@SheldonWangRJT
Last active March 5, 2019 20:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SheldonWangRJT/bf662783048c4c1ec28c32d5f10d9a9a to your computer and use it in GitHub Desktop.
Save SheldonWangRJT/bf662783048c4c1ec28c32d5f10d9a9a to your computer and use it in GitHub Desktop.
Git - Common Used Commands
Git - Revert a Merge
#iOSBySheldon
Reverting a commit is usually not hard, specially with the help of Source Tree, if, you are reverting a simple commit on a branch. What you need to do is just to right click the commit you want to revert and then choose "Reverse Commit".
However, if the commit you want to revert is a "Merge", it will be a little bit harder. If you do the same thing in Source Tree, right click - "Reverse Commit", it will give you an error that Source Tree cannot revert it because there are two branches involved.
Let's name the merge-from branch as "F" and "merge-to" branch as "T".
Here is the solution for it (only can be done in Terminal, at least today):
1. Open terminal and use
$ git log
you will be seeing something like:
commit 4a26b308bf105384eb6413a544184
Merge: 279c92dd3d fd6a7702a6
Author: Sheldon <Sheldon@ooxxooxx.com>
Date: Fri Dec 12 13:05:54 2020 -0600
Merge pull request ......
2. Useful info that we will be using, commit id: "4a26b308bf105384eb6413a544184", two branch ids: "279c92dd3d" and "fd6a7702a6". You can use "Ctrl + z" to quit log now.
3. Now to not mess up with these two branches, we need to create a branch off of the "T", and let's call it "T-new". Check out the new branch,
$ git checkout T-new
4. Now revert the commit,
$ git revert 4a26b308bf105384eb6413a544184 -m 1
Note: 4a26b308bf105384eb6413a544184 is the commit id, "1" here means "279c92dd3d", which is the merge-to branch
5. Now it is reverted, you can create another pull request to merge your "T-new" to "T" again.
Reference Links:
[1] Git revert doc: https://git-scm.com/docs/git-revert
[2] Git revert question: https://stackoverflow.com/questions/7099833/how-to-revert-a-merge-commit-thats-already-pushed-to-remote-branch
[2] Git revert + rebase: http://christoph.ruegg.name/blog/git-howto-revert-a-commit-already-pushed-to-a-remote-reposit.html
Git - Revert a single file within a commit
#iOSBySheldon #Git
Scenario: after I pushed a commit, I found that most of the changes are correct except for one file needing to be reverted.
Simply do:
$ git checkout <commit_hash> -- <file>
<coomit_hash> is the hash before your own commit, anything before that should work.
<file> is just the path to the file.
If you didnt even commit and want to revert one single file, just do:
$ git checkout -- <file>
Reference Links:
https://www.shellhacks.com/git-revert-file-to-previous-commit/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment