How to pull one commit at a time from a remote git repository?

后端 未结 4 971
暖寄归人
暖寄归人 2021-01-12 07:40

I\'m trying to set up a darcs mirror of a git repository. I have something that works OK, but there\'s a significant problem: if I push a whole bunch of commits to the git

相关标签:
4条回答
  • 2021-01-12 08:11

    Have you tried looking at some existing solutions for moving changesets between version control systems, such as Tailor, which says that it includes support for git and darcs? (There are suggestions for similar systems on that page as well.)

    Otherwise, if you want to use your suggested approach, you could use git checkout on each commit after HEAD to origin/master to checkout that commit in "detached HEAD" mode. For example, to modify the example you give (and in bourne shell, I'm afraid, since I don't use ksh):

    # Update all remote-tracking branches from origin
    git fetch origin
    
    for c in `git log --pretty=format:"%h" HEAD..origin/master`
    do
         git checkout $c
         author=$(git log -1 --pretty=format:"%an <%ae>")
         logfile=$(mktemp)
         git log -1 --pretty=format:"%s%n%n%b%n" > $logfile
    
         darcs add -q --umask=0002 -r .
         darcs record -a -A "$author" --logfile="$logfile"
         darcs push -a
         rm -f $logfile         
    done
    
    # Now go back to master, and merge to keep your master branch up to date:
    git checkout master
    git merge origin/master
    

    Note that this will linearize the history from git, which wouldn't be what I wanted, personally. :) I think it's best to use an existing tool for this, but the above approach could be made to work.

    0 讨论(0)
  • 2021-01-12 08:13

    You could do something like this:

    #!/bin/bash
    git fetch
    count=$(git log --pretty=oneline | wc -l)
    git merge origin/master
    git reset --hard HEAD~$((count-1))
    

    I created a repository for this script and tried it out. The following is before and after merge:

    enter image description here

    enter image description here

    Now I didn't have a remote repository so I faked the git fetch and the remote branch with a local (named kalle), but you get the idea. Just do the complete merge and then back up the HEAD pointer until you reach the first commit from origin/master.

    0 讨论(0)
  • 2021-01-12 08:24

    Use this to retrieve hashes from a branch:

    git log --pretty=format:"%h" HEAD..origin/master
    

    Then use git cherry-pick -n <hash> to apply each one.

    Another option, as cited by @xenoterracide, is using githooks.

    0 讨论(0)
  • 2021-01-12 08:25

    git remote update # fetch all remotes I like it better than just fetch

    git log origin/master # can be any remote/branch

    git cherry-pick origin/master # example remote/branch you can also specify a sha1

    cherry-pick will pick the top patch by default.

    for the third part I think you'll have to write a script to do it for you. there are other ways to get the hashes and lots of options for log. Actually there might be a hook for cherry-pick or maybe just post commit... to run the darcs code. check out git hooks.

    in fact on that note each patch applied in a rebase might call a git commit hook so you might be able to write that and then do a git pull --rebase and have that code nailed on each apply...

    0 讨论(0)
提交回复
热议问题