3

I am tasked with splitting/extracting a very big file (which has already been committed) into many smaller ones. Using Hg I would add the extracted files as copies of the existing big file (hg cp or hg cp -A) such that when I merge my branch onto master, other changes made to the big file while I'm working will be tracked/conflict in my extracte files.

Is there an equivalent in Git?

Carlo V. Dango
  • 13,322
  • 16
  • 71
  • 114

2 Answers2

5

You are supposed to just copy your files and than add and commit via git. git uses hashes to detect same files and has no functionality to copy files like mercurial, subversion and others.

git detects origin thru same hashes of files or by comparison. It's just guessing and you're not able to do this explicit (what's sometimes a pain...)

If you want to split a file to different parts you could copy the original files to the destination files and commit the new files. Then work on the individual files.

This SO question will give you more information: How does git track source code moved between files?

Community
  • 1
  • 1
Günther Jena
  • 3,706
  • 3
  • 34
  • 49
  • I'm not copying the file I'm extracting portions of the file. And the portions are too small for git to detect it being a copy – Carlo V. Dango Apr 25 '16 at 18:55
  • 1
    Nonetheless, @Günther Jena's answer is correct. Unlike Mercurial, Git does not record directory operations (copies and renames); it only guesses at them later. – torek Apr 25 '16 at 19:29
2

You can do the same in git with the git add -p.

Its called patch mode or interactive mode. Once you execute git add -p you will get list of options to choose from:

 Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]?

If i understood you correctly you have a big file which you want to use (add) only parts of the file. In this case you will need to split it with the s and then choose y/n if to use this hunk or not.


And here the option you will have to choose from:

  • y stage this hunk for the next commit
  • n do not stage this hunk for the next commit
  • q quit; do not stage this hunk or any of the remaining ones
  • a stage this hunk and all later hunks in the file
  • d do not stage this hunk or any of the later hunks in the file
  • g select a hunk to go to
  • / search for a hunk matching the given regex
  • j leave this hunk undecided, see next undecided hunk
  • J leave this hunk undecided, see next hunk
  • k leave this hunk undecided, see previous undecided hunk
  • K leave this hunk undecided, see previous hunk
  • s split the current hunk into smaller hunks
  • e manually edit the current hunk
  • ? print help

enter image description here

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • Thanks for the answer, although you have misunderstood slightly. I have already committed a big file a long time ago. This file contains a lot of classes that now should reside in their separate files. – Carlo V. Dango Apr 25 '16 at 20:40
  • Ha ok, you need to split it to smaller ones. You will have to do it manually. – CodeWizard Apr 25 '16 at 20:48
  • 1
    yes I dont mind splitting manually, but I'd like to get the changes tracked across the files – Carlo V. Dango Apr 26 '16 at 06:00