Lucas Freitas
Lucas Freitas

Reputation: 194

Fetching only head of a single branch and pushing to an empty repo on Git

I have a repo with my codebase, and each project inherits this codebase. It has it's submodules on it and everything works fine.

When I start a new project from scratch, how is supposed to be cloning this repo (only the master branch) and only the HEAD (since I don't want each project have a lot of commits of the base).

I tried:

    git init
    git remote add -t origin URLtoProjectRepo
    git remote add -t codebase URLtoMainCodeBase
    git pull codebase master --depth=1

When I try to push this to the project repo I get:

    [remote rejected] master -> master (missing necessary objects)
    error: failed to push some refs to 'ProjectURL'

What I'm missing?

Upvotes: 0

Views: 606

Answers (2)

jthill
jthill

Reputation: 60275

You cannot push an incomplete history, but making a no-history commit is easy:

git fetch --depth=1 git://wherever master
git checkout -B master $(git cat-file -p FETCH_HEAD|git commit-tree FETCH_HEAD^{tree})

will give you a fairly ugly commit message that includes the original history links as text; plopping |sed 1,/^$/d in there will strip even the documentary links to the past.

Upvotes: 3

m79lkm
m79lkm

Reputation: 3070

I had a similar problem to solve. I have a base repository which is essentially the core code for many other projects. The other projects first pull the core repository then check that into the new project repository. By doing that, any time changes are made to the core repo we can easily merge the changes into our projects. Being new to git, this is the result of what I learned while researching this. I took this out of a perl script I use to automate the process. It works great for me. Hope it helps you.

git init
git remote add origin new_repo
git add .
git commit -m 'initial commit'
git push origin master
git remote add base base_repo
git checkout -b base
git pull base master
git checkout master
git merge base
git push origin master
git branch -d base

Upvotes: 0

Related Questions