Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "split" files with git

Tags:

git

If I had to following file on my development branch:

# file.rb

class Code
 def methodA
  'aA1'
 end
 def methodB
  'bB2'
 end
end

but on my master branch I wanted to separate the methods into different files:

# in file.rb
class Code
  def methodA
    'aA1'
  end
end

# in extra.rb
class Code
  def methodB
    'bB2'
  end
end

I could simply create the file on development, then checkout to master, rebase and separate the file manually, but is it possible set things up so that when I make changes to development it would be carried over to the right "part" of the file on master?

like image 339
Stefan Avatar asked May 22 '09 13:05

Stefan


People also ask

How do I split a commit in Git?

Simply replace the pick word with edit on the line of the commit you want to split. When you save and close this “file”, you will be placed at that commit in the command line. If you do a git status or a git diff , you will see that git places you right after the commit.

How do I split a git repository into two repositories?

We will start off by installing git filter-repo, which we will use to split our initial repositories. This can be a bit tricky, so we will guide you through it: Use scoop to install git-filter-repo in an elevated powershell: scoop install git-filter-repo. Run git filter-repo to test if it works.

Why doesn't Git recognize a rename when splitting a file?

If the similarity of the "renamed" files is below what Git detects, or can be configured to detect, then there is no way for Git to consider it a rename. When splitting the file, nearly all the contents change.

How do I split a file into separate files?

Files can be split by specifying line address or pattern. Running the split command without any options will split a file into 1 or more separate files containing up to 1000 lines each. This will create files named xaa, xab, xac, etc, each containing up to 1000 lines.


1 Answers

To have a split "carried over to the right "part" of the file on 'master'" would imply Git being able to detect the split.

And as illustrate by this recent thread (this month: May 2009), Git is not quite there yet.
That might work for big files with a small part in it split to another file, but for small files, the content appears "too different" for git rename detector to pick up the changes.

So rebase 'master' on top of 'development' might work, provided you did not publish (push) 'master' since 'development' has been made, because such an operation (rebase) will rewrite the SHA-1 of commits on 'master' branch (since they are replayed on top of commits of the 'development' branch)

As I described in rebase vs.merge, if your development branch is not "too far" (in term of modifications from master, another strategy would be to rebase development on top of master, make the split, and then merge development back to master (fast-forward merge at this point)

like image 131
VonC Avatar answered Oct 13 '22 20:10

VonC