3

我在开发一个新站点时一直在使用 Git/Beanstalk,它在将更改从本地服务器推送到暂存服务器方面创造了奇迹。我现在正处于想要使用生产服务器“直播”的地步,并且想知道正常的程序是什么(许多其他链接都没有涵盖这个......)。

I would like to start over "fresh" with this new Production Server instead of pushing the thousands of changes I've made over the past 6 months from the Staging server. Is there a way to do this? Do I simply create a new repo, get rid of the old one, etc.? How can I clear the history so that it simply takes what is on the staging server now and pushes that (or do I push from the local server). I use Tower so I know just enough about Git to almost not get in trouble (command line isn't my thing). Can you wrap everything up and say "This is Version 1.0, let's start from here" sort of thing?

Also, I was kind of lazy when it came to ignoring the cache files and images, etc. Can I easily add those .gitignore files after the fact?

4

3 回答 3

4

请注意,Beanstalk 只会部署您的文件的最新版本(或者如果您想要任何其他版本)。因此,当您添加生产服务器时,Beanstalk 会将当前存储库中的所有文件上传到生产服务器。

它不会通过变化来部署它,因此任何“历史”都不会减慢它的速度或使用任何空间。

于 2012-12-20T11:25:40.910 回答
3

As Adrian says this isn't really an issue unless you've got large numbers of binary files in Git that shouldn't be (eg image uploads etc). Personally I wouldn't nuke my history as it's invaluable for debugging, though I'm pretty disciplined about what I commit and how often.

If you want to remove files from your repo that you have previously added, but want to keep a local copy do:

git rm --cached /path/to/remove

This will remove them from the current state of the index and but keep the actual file, which you can then add to your .gitignore. This is handy for files you just added to git by mistake, or for small files that aren't a concern for repo size or security.

However if you've previously committed the files, they will still be in your history, so your total repo size may be inflated (for example if you added a PSD or some large images in the past. If you want to completely remove a file from your history (also handy if sensitive data crept in somehow) you can do:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch <NAME OF FILE>' --prune-empty --tag-name-filter cat -- --all

This command will run the entire history of every branch and tag, changing any commit that involved the file you specify, and any commits afterwards. Commits that are empty afterwards (because they only changed the Rakefile) are removed entirely. Warning: It will overwrite your existing tags as well.

Once you've removed the file you may need to force pushing your changes with a:

git push <remote> <branch> --force

But you want to be pretty sure you're doing the right thing at that point though, as you will overwrite the history on the remote as well, and at that point it really will be gone. There is an excellent, more detailed tutorial here if you go down this route.

但是,当您使用 Beanstalk 时,这不是一个主要问题,因为它只会部署您的文件的最新版本,而不是您的整个 repo/history。

于 2012-12-20T10:43:16.813 回答
2

Unless your git repository is really large (because you added a bunch of media files to it accidentally or something), there is pretty much no point doing this. The whole point of version control is to keep a history of changes, so you can look back and see when and why something was added. The reason why no tutorials cover this is because it's simply not how you would normally use git.

The only reason I could see you wanting to do this is if you added a whole bunch of files which don't belong in git (large media files, caches etc), which have since been deleted and are taking a long time to push to your production server (since you mentioned you forgot to start with a .gitignore file). All text files are compressed in git, so there is no need to worry about how many commits you have made (for comparison, the PHP source code has over 70,000 commits, dating back 14 years, the linux kernel has over 300,000 commits, and many other successful open source projects are like this too - so even if you have made 1000 commits, you are barely scratching the surface of what git can handle).

In that case, what you are talking about doing is wiping your entire history and just creating a new blank git repository. All you need to do is delete the /.git directory, and initialize a new one (from Tower, or the command line with git init).

Note if you create a fresh repository, then try to push to your staging server, you will need to use the --force option, otherwise it will complain that you are trying to rewrite history (which you are). Tower has a similar force checkbox when you click Push.

You can certainly add .gitignore files after the fact. However, keep in mind that this doesn't remove any files which you previously added to the repository. So it's always better to avoid committing files in the first place, otherwise they end up permanently in your repository history (there are ways to rewrite history and properly remove all traces of files even from old commits, but they are difficult and beyond the scope of this answer).

于 2012-12-20T03:39:46.120 回答