0

Is there any way to push an empty commit to a (remote) git repository without cloning the git repository first?

I.e., can I achieve the effect of

git clone https://github.com/example-org/example-repo.git
cd example-repo
git commit -m "empty commit" --allow-empty
git push

without first cloning the repository in the first line?

Ideally, I'd like to do this with "pure" git, but using the github API would be fine as well if there no easy way for pure git.

Background: I need the empty commit as part of the automatic deploy mechanism in Heroku which automatically deploys on github commits.

3
  • Using pure git — not possible as git works only on a local repository. Using Github API — possible, see stackoverflow.com/a/52941620/7976758 and stackoverflow.com/search?q=%5Bgithub-api%5D+create+commit Commented Feb 11, 2020 at 13:38
  • I managed to create an empty commit as a child of the latest one, however it doesn't show in the corresponding branch. :( Commented Feb 11, 2020 at 14:06
  • You do actually need a clone, because what Git calls an "empty" commit is not actually empty at all: it just matches its parent commit. So you need to populate the index with each file from the commit you're duplicating, and you need to make the new commit with the parent hash ID set to the hash ID of the commit you're duplicating. This means you need the commit you're duplicating. Depth 1 (and --single-branch) suffices since you don't need the commits before the commit you're duplicating. Commented Feb 11, 2020 at 17:35

1 Answer 1

2

Assuming that your concern is time and resources spent downloading the repository content you can use a shallow clone with git clone --depth 1 option.

As per clone docs:

--depth <depth>

Create a shallow clone with a history truncated to the specified number of commits. Implies --single-branch unless --no-single-branch is given to fetch the histories near the tips of all branches. If you want to clone submodules shallowly, also pass --shallow-submodules.

There is also git clone --bare approach as explained in this answer but you would have to setup origin manually before pushing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.