admin管理员组

文章数量:1406949

I need a solution to snapshot a dirty Git repo and make it available remotely. I want to use Git instead of rsync since we need versioning, deduplication and speed and the users are already using Git.

I need a non-intrusive script that creates a commit and pushes it to the upstream, all without any disruption of the current state of the repo/workdir/index.

Requirements:

  • Script returns a commit SHA that's available remotely (e.g. GitHub).
  • Another user or system can checkout that commit SHA and get the same working copy as the first user.
  • The state of the user's local repo is not disrupted in any way (working copy files, active branch, index etc)

Implementation idea: The script creates a separate index, stages all working copy files, commits the files, pushes the commit upstream and returns the SHA.

How can I implement this? The hard part for me is no disrupting the active branch and index.

Can git stash be used to accomplish this? Or maybe something like this:

git_dir=$(git rev-parse --git-dir)
tmp_index=$(mktemp)
cp "$gitdir/index" "$tmp_index"
GIT_INDEX_FILE="$tmp_index" git add --all  # Is this enough to capture the current state of the working directory?
GIT_INDEX_FILE="$tmp_index" git commit --message stash ??? How to tell git comit to not change any branch and return new SHA?
GIT_INDEX_FILE="$tmp_index" git push origin ???:??? How to push commit to remote without creating a remote branch?

Update: It looks like git stash create does almost everything I need: Non-intrusive commit from a current working directory state.

Is t true that git does not allow me to just push a commit to remote, and I must push it to a remote branch?

Update2

It looks like the ability to do git stash create --include-untracked was lost during the transition of git stash from shell to builtin.

Last version of stash that was shell-only: .sh

Last version of stash.sh with full save support without helper: .sh

A non-destructive do_stash_create with support for untracked files still exists in C, but all functions that expose it are either destructive or don't expose --include-untracked.

本文标签: