Search code examples
gitgithubmergeclonegithub-desktop

Git clone into existing branch (overwrite existing branch)


I am not a git-savvy user. I've been using it for a while but apart from one project it's just been for myself as an individual.

I have a project that I'm working on with a group now though and one of my team-mates just asked me (he hasn't been working on this project for a while) how he can essentially clone the most up to date version of the dev branch into the branch he was using before and just overwrite everything and start afresh.

Would discarding his changes and merging dev into his branch do that? or is there another way to essentially clone the project into an already existing branch? (Also we are using github desktop so might not have access to all git features apparently - this is something I only realized recently, was using sourcetree before).


Solution

  • clone is a repo level action - not a branch level action and would reset the entire repo. And you're talking about just pulling the current version of a branch, right?

    If this developer really wants to throw out what he has on his local copy of the branch and make that branch match the remote (from github or bitbucket or wherever), then he could do

    git fetch
    git switch the-branch-name
    git reset --hard origin/the-branch-name
    

    This will put his branch pointer on the same commit as the origin version of that branch.

    If he is at all concerned with losing work, he can make a checkpoint branch which includes his (throwaway) work and then you at least have a way out in case that doesn't work.

    For that safety, you can do something like

    git fetch
    git switch the-branch-name
    git switch -c my-branch-checkpoint-for-safety
    git switch the-branch-name
    git reset --hard origin/the-branch-name
    

    This will make a new branch called my-branch-checkpoint-for-safety which you could go back to or later delete when you know everything is squared up.

    As you can probably tell, this is all using git cli. I'm not certain how this maps to Github Desktop.