user1934146
user1934146

Reputation: 3071

How to delete some files from my commit

I have uploaded a gerrit and now I want to delete some files from my commit, how can I do that?

Upvotes: 2

Views: 21013

Answers (2)

Sankaran Srinivasan
Sankaran Srinivasan

Reputation: 856

Say you have commited three files a.java, b.java, c.java to gerrit and you want to remove b.java from the commit. Follow the below steps.

  1. If you are in a different branch, cherry-pick the particular commit.
  2. Use the below command to remove the file from the commit. (Do this for as many files you want to remove.)

    git reset HEAD^ path/to/file/b.java
    
  3. Amend the commit with the below command.

    git  commit --amend
    

Reference: https://superuser.com/questions/229290/how-to-amend-the-last-commit-to-un-add-a-file/229296#229296?newreg=011d1b3234a8444295fbdd12b6d513b4

Upvotes: 10

Kalle Pokki
Kalle Pokki

Reputation: 5059

So you need to generate a new patch set that will replace the old one. Assuming you haven't commited anything else since the commit you are trying to edit, do

git rm <files>
git commit --amend

You have installed the gerrit commit hook, haven't you? If you have, you are good to go and are ready to push. You you don't have it, you need to copy the Change-id line from the gerrit web interface to the end of your commit message, or gerrit won't be able to replace the previous patch set with the new one.

When you committed the file (and you have the same Change-id line there as you had in patch set 1), push the fix to gerrit

git push origin HEAD:refs/for/master

or whatever repository and branch you are pushing to.

In the future, you should install the commit hook as soon as you clone the repository from gerrit. If you clone with e.g.

git clone ssh://firstname.lastname@gerrit/project

you can get the commit hook with

cd project
scp firstname.lastname@gerrit:hooks/commit-msg .git/hooks

Substitute paths and machine names that apply to your case.

Upvotes: 5

Related Questions