Sometimes you need to copy another repository into your project, especially boilerplate codes. However, you may not want to import the commit history associated with that boilerplate code repository while doing so. In this article, I will show you how to do it with an example of cloning the electron-react-boilerplate code inside my own project repo.
So at first, let’s create our own project repository.
Then clone it to the local PC.
At this stage, our git history looks like this-
Our final goal is to import the electron-react-boilerplate code in our repository so that we don’t have to set up the electron project with react from scratch. At the same time, we don’t want all the 987 commits of the original repository appearing in our project.
So at first, let’s pull the codes in our repository with the following command –
git pull https://github.com/electron-react-boilerplate/electron-react-boilerplate main
What this will do is to pull the codes from the electron-react-boilerplate project in our main branch.
Here I have used the –allow-unrelated-histories flag to allow both commits from my own repository and the boilerplate repository to co-exists without conflicting.
Though all codes are pulled in my repository, you can see one merge conflict exists in README.md file, which can happen. You can accept your preferred version and save it.
At this stage, if we resolve the conflicts and commit; our git history will look like this –
But we don’t want these boilerplate development commits appearing in our own repository’s git history. For that purpose, we want to soft reset our HEAD with the following command –
git reset HEAD
This command will cancel those two repositories from merging and unstage the code changes. So git is not merging your pull request but at the same time keeping the file changes.
Now you have to manually stage those changes and commit them with the following commands –
git add --all git commit -m 'Your Message'
If you check your git history now, it will show this boilerplate initialization changes as your own commit like below –
That’s how we can simply initialize our project with another repository without worrying about importing foreign Git Commits.