git2go icon indicating copy to clipboard operation
git2go copied to clipboard

Clean reset hard, git pull

Open amlwwalker opened this issue 9 years ago • 5 comments

If I am wanting to do the git equivelent of:

git clean -fd
git reset --hard
git pull origin master

Whats the git2go equivelent? i cant seem to find how to set those flags in the documentation. I think I might not be able to do a pull, but a fetch and then a merge, but not sure about the resetting the current state

amlwwalker avatar May 12 '15 09:05 amlwwalker

this seems overly hard to do? If I want to make sure that the git repository is always matching the github repository, then the above commands would be what I would do manually. I understand that each of the above don't have mapped commands in git2go, but I can't work out what the equivelent would be.

amlwwalker avatar May 26 '15 16:05 amlwwalker

Could you give me a bit of a hand with this. I have still not worked it out entirely. Ive got to here:

func Pull(source string, dest string) error {
    repo, err:= git.OpenRepository(dest)
    remote, err:= repo.LookupRemote("origin")
    log.Println(repo)

    cbs := &git.RemoteCallbacks{
        CredentialsCallback:      credentialsCallback,
        CertificateCheckCallback: certificateCheckCallback,
    }

    err = remote.SetCallbacks(cbs)
    err = remote.Fetch([]string{}, "")
    log.Println("fetch error: ", err)
    remote_master, err := repo.LookupReference("refs/remotes/origin/master")
    mergeRemoteHead, err := repo.AnnotatedCommitFromRef(remote_master)
    mergeHeads := make([]*git.AnnotatedCommit, 1)
    mergeHeads[0] = mergeRemoteHead
    err = repo.Merge(mergeHeads, nil, nil)
    log.Println("err: ", err)
    repo.StateCleanup()
    return err
}

What I am trying to do is the git2go equivelent of

git checkout master
git clean -fd
git reset --hard
git pull

I dont want any local changes to be kept. I think i am not too far. Everything seems to work, except that the files dont change to that on the server....

amlwwalker avatar Jun 08 '15 23:06 amlwwalker

Where does a server come into this? Git operations are local, you're only ever doing local operations. Furthermore, what you're asking git2go to do here is perform a merge, which is very different from what you say you want to do.

If you want to checkout the files as the are on origin's master, then you can fetch and then use CheckoutTree() and pass it the tree of refs/remotes/origin/master's commit. You can then set the index to that with its ReadTree() and Write(), and then end with setting the current branch via SetHead().

This are the steps of git fetch origin && git reset --hard origin/master which is what your intent suggets. Your git commands use git pull which does not guarantee that you're overwriting anything, but is rather explicitly asking to merge in whatever local changes you have.

carlosmn avatar Jun 12 '15 04:06 carlosmn

if it was me I prefer ‘git rebase —preserve=merges origin/master’ in case your side and remote has an commit you dont have on your side that you changed or did not change at or before the time you made the commit. Allowing to rewind back to common commits from origin/master, pull, and then reapplies your commit or commits individually. and avoids git push conflicts later on and seemingly works 99.9% of cases involving merge conflicts (also I hate the annoying ‘merge branch ‘%s’ into branch ‘%s’’ noise and wasteful commit history that makes it not linear and preserves branch names forever which is not what I want or like).

AraHaan avatar Jan 01 '18 07:01 AraHaan

In my case, I'm trying to deploy code across servers using git where accident file changes needs to be ignored and not merged.

Manit-Chanthavong avatar Feb 14 '21 06:02 Manit-Chanthavong