Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script to permit to rcheckin a git repository to a newly created TFVC repository with git-tfs
#Script to permit to rcheckin a git repository to a newly created TFVC repository with git-tfs
#adaptation from my answer:
#usage: pass to the script (1) the url of the tfs server, (2) the tfs path in the repository, (3) the changeset id
#../ "https://urlOfYourTfs/DefaultCollection" "$/EmptyTfs/Trunk" 21
gitTfsMetadata="git-tfs-id: [$tfsServer]$tfsPath;C$tfsChangesetId"
echo "Creating a root commit with git-tfs metadatas:"
echo $gitTfsMetadata
root_commit_sha=$(git rev-list --max-parents=0 HEAD)
git checkout --force --orphan new-root
find . -path ./.git -prune -o -exec rm -rf {} \; 2> /dev/null
git add -A
GIT_COMMITTER_DATE="2000-01-01T12:00:00" git commit --date==2000-01-01T12:00:00 --allow-empty -m $gitTfsMetadata
new_root_commit_sha=$(git rev-parse HEAD)
echo "The commit '$new_root_commit_sha' will be added before existing root commit '$root_commit_sha'..."
parent="parent $new_root_commit_sha"
git cat-file commit $root_commit_sha | sed "s/author/$parent\nauthor/" |
git hash-object -t commit -w --stdin
) || return 3
git replace "$root_commit_sha" "$replacement_commit"
git filter-branch -- --all
rm -rf ./.git/refs/original
rm -rf ./.git/refs/replace
git checkout -f master
git branch -D new-root
git tfs bootstrap

This comment has been minimized.

Copy link

sawpresto commented Oct 16, 2015

Thanks for creating this, pmiossec! I'm running into a couple of issues when trying to run this script from the Git repo (not the TFVC git-tfs repo). When I put in the only changeset in the TFVC repo (which would normally have matched with the first commit from git-tfs), after running and switching to a new branch, I got the error: pathspec '[{repo}]{project};C289' did not match any file(s) known to git. fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree. Any ideas? I suspect the error is happening at line 16. I'm using a Windows and it looks like the script did start running. Thanks!


This comment has been minimized.

Copy link

mikesigs commented Mar 24, 2016

@sawpresto - Checkout my fork. I got it working on Windows.

One problem was the find command in this script is intended to use the bash version of that command, but the Windows one is used instead. To fix that I reference the find command by its full path. Then on the next line (starting with GIT_COMMITTER_DATE) I had to enclose $gitTfsMetadata in double quotes. I also wrapped the last 6 commands in a conditional, prompting the user to continue or not. Reason being, up to that point everything is reversable by just deleting .git/refs/replace and the new-root branch. In fact, I preferred to run those last 6 commands manually. Also, make a backup before running this!

My script isn't perfect either. I'm a bash script newb. But it worked on Windows, and I hope that can help you/someone else.

But thanks most of all to @pmiossec for the original!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.