开发者

completely remove deleted file from being pushed to master in a git repo

I has downloaded a large avi file. I had downloaded it to a git repo on my开发者_如何学运维 local system by mistake. after doing some commits I realized the file was in there and deleted the file not thinking about what would happen later when I pushed to my master on the server. Needless to say the git repo wants to push the "deleted" file that has been versioned to the server.

I can't seem to remember the name of the file. All I know is that it has a .avi extension. What I would like is some suggestion as to how I would track down the file in the git repo and how I could remove it from being pushed to master.

thanks, Matthew


did a little more searching around and found that if you run this command it should search for all deleted files and rm them.

git ls-files --deleted | xargs git rm


to see what's missing..

git status

then to remove

git rm filename.avi


git filter branch to remove the file. Note, you will have different SHA-1s from the point at which the file was added.


Use git log --stat to identify the commit hash of the commit that added the file. The --stat will help you see what file was added/removed in each commit and identify the path of the avi file. Stash this in $FILENAME. Then

git filter-branch --index-filter 'git rm --cached --ignore-unmatch $FILENAME' HEAD


Matt,

Have you pushed your repo yet and has anyone consumed it? If not... more importantly if your history is still your own then you need to rebase to rewrite your history. Let's say I have a repo for example with the following simple history:

$ git log --summary --stat
001d888 Yet some more work (Fred - 71 seconds ago)

 file2.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
3f74e79 Doing some more work (Fred - 2 minutes ago)

 file1.txt |    1 +
 file3.txt |    1 +
 2 files changed, 2 insertions(+), 0 deletions(-)
8acc3d8 Adding some more files (Fred - 3 minutes ago)

 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1.txt
 create mode 100644 file2.txt
 create mode 100644 file3.txt
fb3a2e1 Just added a big ole file (Fred - 4 minutes ago)

 BigOleFile.blob |  Bin 0 -> 33214460 bytes
 1 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 BigOleFile.blob
6d0ab2b first commit (Fred - 8 minutes ago)

 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README

Imagine that the BigOleFile.blob is your big file...

To rewrite the history get the sha of the commit right before the one you want to remove. In this case it's:

6d0ab2b

And if your git log is crazy big do something like the following to filter it down and still give you context around the match:

git log --summary --stat |  grep avi --context=10

Now rebase your history using the interactive option.

$ git rebase --interactive 6d0ab2b

You'll get something like so:

pick fb3a2e1 Just added a big ole file
pick 8acc3d8 Adding some more files
pick 3f74e79 Doing some more work
pick 001d888 Yet some more work

# Rebase 6d0ab2b..001d888 onto 6d0ab2b
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x <cmd>, exec <cmd> = Run a shell command <cmd>, and stop if it fails
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

Delete the line having the sha where the file was added... In this case it's the first line. You will have to most likely go through several prompts afterwards during the rebasing session, but it might be fairly clean and you'll get away with it. After your done you will get the message:

Successfully rebased and updated refs/heads/master.

Looking at the git log again to make sure:

$ git log --summary --shortstat
7c499bb Yet some more work (Fred - 12 minutes ago)

 1 files changed, 1 insertions(+), 0 deletions(-)
4dfa303 Doing some more work (Fred - 13 minutes ago)

 2 files changed, 2 insertions(+), 0 deletions(-)
2f27c1b Adding some more files (Fred - 14 minutes ago)

 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1.txt
 create mode 100644 file2.txt
 create mode 100644 file3.txt
6d0ab2b first commit (Fred - 19 minutes ago)

 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README

Pushing to master afterwards:

$ git push origin master
Counting objects: 9, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (8/8), 751 bytes, done.
Total 8 (delta 2), reused 0 (delta 0)
To git@github.com:xxxxxxxxxxx/temp.git
   6d0ab2b..7c499bb  master -> master

As you can see ... file all gone bye - bye.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜