Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How can I modify server's commit?

Tags:

git

I have already push some commits on my git server on EC2 , not on github.

How can I modify those commits on the git server?

Operation like remove commit, like rebasing, change commit message

Is it possible?

Thanks so much.

like image 265
TheOneTeam Avatar asked Oct 19 '11 01:10

TheOneTeam


People also ask

How do you modify an existing commit?

Changing the Last Commit: git commit --amend. The git commit --amend command is a convenient way to modify the most recent commit. It lets you combine staged changes with the previous commit instead of creating an entirely new commit.

Can I amend already pushed commit?

To change the most recent commit message, use the git commit --amend command. To change older or multiple commit messages, use git rebase -i HEAD~N . Don't amend pushed commits as it may potentially cause a lot of problems to your colleagues.


2 Answers

You can do just about everything with a forced push. Change your local tree to exactly what you want and git push -f and it will replace the tree on the server with what you have locally. It's worth noting that this will cause trouble with any other repos that have pulled the tree you just replaced.

like image 169
Jim Mitchener Avatar answered Oct 21 '22 05:10

Jim Mitchener


If you want to edit a remote branch's history destructively (e.g. rebase/amend), make your changes locally and then do git push --force. Sometimes it does not work (repository administrators can disable this feature); in that case, you can try deleting the remote branch with git push origin :my_branch and then pushing it again with git push origin my_branch. Otherwise you can use git revert, which is the recommended way if you work in a team (the rule of thumb is that published history should not be modified).

Here's an example (using this Github repository):

$ touch SOMETHING
$ emacs SOMETHING 
$ git add SOMETHING 
$ git ci -m SOMETHING
[master d14aaa0] SOMETHING
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 SOMETHING
$ git push
Counting objects: 8, done.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 611 bytes, done.
Total 6 (delta 3), reused 0 (delta 0)
To [email protected]:23Skidoo/ghc-parmake.git
   53f836a..d14aaa0  master -> master
$ git reset --hard "HEAD~"
HEAD is now at 7b2dc96 TODO update.
$ git push --force
Total 0 (delta 0), reused 0 (delta 0)
To [email protected]:23Skidoo/ghc-parmake.git
 + d14aaa0...7b2dc96 master -> master (forced update)

Looking at the commit history, you can see that commit d14aaa0 is absent.

like image 28
Mikhail Glushenkov Avatar answered Oct 21 '22 05:10

Mikhail Glushenkov