Git: How to ignore/specify files for *checkout*

前端 未结 5 1164
日久生厌
日久生厌 2021-02-13 02:56

If I don\'t want .html files tracked I can add the pattern to .gitignore and they\'ll be ignored. I\'d like to know how I can do the converse - at checkout, how could I ask git

5条回答
  •  清歌不尽
    2021-02-13 03:31

    You cannot retrieve part of an individual commit in Git. You either pull a commit or you don't - there's no half-way step.

    This means that if there is a commit that adds a bunch of files then you can get all of those files (by pulling that commit into your local repo) or none of them (by not pulling that commit).

    You can choose which commits you want with git cherry-pick, but I suspect it's not going to help you much in this case.

    Generally a file should either be in version control or not - I can't think of a reason why you'd want a file in Git only sometimes. If there's something in your .gitignore then it's probably a good candidate for removing from the repository altogether. .gitignore basically means "don't look at this file, it's not under version control".

    (If you don't mind rewriting your repository history you could cherry-pick the add commit, then do a git reset --mixed HEAD^1 and create new commits that leave out the files you don't want...but don't do this unless you really know what you're doing. Better to just remove the files and push a new commit that records the removal.)

    EDIT: Just saw your comment about wanting specific parts for a deployment. In that case I'd suggest you create a deployment branch that removes the irrelevant files. You can rebase this branch from mainline whenever you need to and it should magically remove the files for you each time.

提交回复
热议问题