Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Copy source code to new branch without history

Tags:

git

branch

I have 2 branches in git repository: common and myown.

I want to copy the source from one branch to another branch but I want to see in history of common branch only commits related to feature migration (from myown to common).

Is it possible to copy the source without the history or is a new repository required?

In addition it can be necessary to merge from common to myown and after some changes copy source back as new commit (and only as that commit - without history of all other commits as I said previously).

like image 894
Maxim Avatar asked Dec 26 '13 19:12

Maxim


People also ask

How do you copy a repository without history?

Here is the basic command. Replace “Branch Name” with the name of your branch, “Git Repo” with the url to your Git repository, and “Folder Name” to the directory that you want to clone the branch into.

Does git clone copy history?

No it does get the full history of the remote repository.

Can I push code without commit?

No, you must make a commit before you can push. What is being pushed is the commit (or commits).


2 Answers

I found a faster way that can be used:

git checkout <branch> -- .

This will replace the content of the currently checkouted branch by the content of the <branch>.

This doesn't copy <branch> commit history.

If you just want to checkout only some files just replace the . with the file paths from <branch>.

EDIT: I found out that this will only replace current files and it will not delete files that are not on the <branch>.

It is then better to run rm -rf before doing the checkout.

like image 152
Zlopez Avatar answered Sep 28 '22 13:09

Zlopez


It looks that I have figured out how to do it. For example we have branch myown. And we need to create new one empty (without history):

git checkout --orphan common

Now we need to add files:

git add .

And commit all:

git commit -m "Initial"

You can see in log only this commit (not all made in myown branch). Now we can checkout myown branch and continue work:

git checkout myown

We can do multiple commits in own branch (similar to regular work):

git commit -m "feature1"
...
git commit -m "feature2"

Now we have committed both and log contains both. We need to copy that to common branch and name it "release1"

git checkout common
git merge --squash myown
git commit -m "release1"

That's all for my first part of question. Also it is easy to commit to common repository (small change/fix for example related to release1 itself). You can make regular merge to put code back to myown branch.

like image 31
Maxim Avatar answered Sep 29 '22 13:09

Maxim