当前位置: 动力学知识库 > 问答 > 编程问答 >

version control - How to make Git ignore changes or elimination of files at a branch?

问题描述:

Having both typical branches, master with A and B files and later create dev branch:

  • I might create new files at dev and maybe modify B but,
  • lets say that either I change or even delete A
  • When I decide to merge them so master might look like dev
  • I want the A file (or any other file or folders) to remain the SAME, no matter how the merged branch looks like.

How can I achieve this?

Would git reset or git revert help me solve it?

网友答案:

Do the merge with --no-commit. Once the automatic part of the merge is finished, git stops (the same way it would if it cannot complete the merge itself) and you may now alter the merge result in whatever way you like, including checking out the desired version of file A:

git checkout branch -- A

or:

git checkout --ours -- A

(etc).

Once the tree has been adjusted to your satisfaction (if you have automated tests, use them!), git add if needed, and git commit the result to complete the merge.

(You can automate some or all of this, if you like.)


Edit to add example. Let's say you want to merge dev into master, but keep master's version of xyz.html. (I removed xyz.html in branch dev. I also added file foo, and made no other changes.) Let's also use --no-ff to force a real merge. (Otherwise git may be able to do a fast-forward merge, if branch dev is exclusively ahead of master. In this case our --no-commit has no effect, since git won't attempt a commit anyway.)

$ git checkout master
Switched to branch 'master'
$ git merge --no-ff --no-commit dev
Removing xyz.html
Automatic merge went well; stopped before committing as requested
$ git checkout master -- xyz.html

(You can check out any particular version of xyz.html: the above just gets the version from the tip of branch master. Since you are actually on branch master there are many other ways to spell this; that's just the most explicit I could think of.)

$ git status
On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

    new file:   foo

$ git commit

(at this point the editor opens, as with any other commit; writing out the file and exiting produces)

[master 9746054] Merge branch 'dev'
$ 
网友答案:

Several ways how to do this:

I. Assuming the file version you wish to keep on master is commited.

git checkout dev
git add -u                           <--- add tracked files to the commit
git commit -m "my commit"            <--- the new commit
git push                             <--- by default: to origin, but you may explicitly choose

git checkout master         
git log                              <--- check the last commit's hash
git pull dev                         <--- either clean, or merge required
git checkout <hash> file/to/restore  <--- get the master's version of the file

II. Create patches of the dev's commits and manually edit them.

git format-patch -<how_many> <sha>   <--- note: <sha> is literally there,
// send the patches to server, e.g. scp
// edit the patches, e.g. gedit 0001blabla.patch
git am <patch>                       <-- apply the patch

III. Create commits on dev excluding the change to the file, push the commits to master. Then create a commit on dev actually changing the file. As a result, commits on master are a subset of dev's.

There are probably tons of other too.

分享给朋友:
您可能感兴趣的文章:
随机阅读: