2017年12月14日 星期四

[ Git 常見問題 ] How can I remove a commit on GitHub?

Source From Here 
Question 
I "accidentally" pushed a commit to GitHub. Is it possible to remove this commit? 
I want to revert my GitHub repository as it was before this commit

How-To 
First, remove the commit on your local repository. You can do this using git rebase -i. (More about git rebase
For example, if it's your last commit, you can do git rebase -i HEAD~2 and delete the second line within the editor window that pops up. Below is a simple example: 
# echo 'Test' > test.txt
# git add test.txt
# git commit -m 'testing' // Commit the changeset
[master a966c3b] testing
1 file changed, 1 insertion(+), 3 deletions(-)

# git push
# git log -2 // Show last 2 commitment
commit a966c3bba37c0c9f253e94732d828dfcf62d635b
Author: johnklee
Date: Fri Dec 15 14:49:15 2017 +0800

testing

commit 26052add670e7dc2e2ed635fdd17e9e953ec48b2
Author: jkclee
Date: Mon Aug 28 15:15:28 2017 +0800

Merge from test_pr

Now we are going to rollback the commitment 'testing': 
# git rebase -i HEAD~2 // delete the second line within the editor window that pops up.
# git log -1 // Check the latest commitment is no longer 'testing'
commit 26052add670e7dc2e2ed635fdd17e9e953ec48b2
Author: jkclee
Date: Mon Aug 28 15:15:28 2017 +0800

Merge from test_pr

# git push -f // Force push to GitHub

If your code is not yet pushed to master, I prefer to use git reset to undo commits, and you can use git reset --soft to undo commits, but leave the files untouched, which usually happens if you want to change order of commits, or fix an older commit (before pushing it to master). Below is a simple example: 
# echo `date` > test.txt
# git add test.txt
# git commit -m 'For rollback'
# git log -2
commit 86183b608fd0a61f9924b6a09a9be3e3b376541e
Author: johnklee
Date: Fri Dec 15 15:09:55 2017 +0800

For rollback

commit 5185e647cec6de4ec1e60e7d89bbbcd35f281b7e
Author: johnklee
Date: Fri Dec 15 15:05:42 2017 +0800

Update test.txt for testing

Above we made a commitment and want to rollback it: 
# git reset --soft HEAD~1 // Rollback the last commitment
# git log -2 // Confirm that the rollback is working
commit 5185e647cec6de4ec1e60e7d89bbbcd35f281b7e
Author: johnklee
Date: Fri Dec 15 15:05:42 2017 +0800

Update test.txt for testing

commit 26052add670e7dc2e2ed635fdd17e9e953ec48b2
Author: jkclee
Date: Mon Aug 28 15:15:28 2017 +0800

Merge from test_pr


# git status // The reset in soft will keep the change
# On branch master
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
# modified: test.txt


Supplement 
Git 基礎 - 檢視提交的歷史記錄

沒有留言:

張貼留言

[Linux 常見問題] What's the best way to send a signal to all members of a process group?

Source From  Here   Question   I want to  kill a whole process tree.  What is the best way to do this using any common scripting languages? ...