Take an arbitrary working copy of git and do exactly the same as the remote branch

Let's say I have an arbitrary local git working directory that has been cloned from some remote one. I want the local git repository to be EXACTLY the same as the remote one, regardless of what happened to the local directory as it was cloned. I do not want to make another full clone.

Say the local working directory has:

  • additional files without a trace
  • deleted files
  • phased changes.
  • maybe on some arbitrary branch.
  • have a bunch of commits since cloning from remote

Now I want this local repository to be accurately reflected. Here is my current solution, but I'm not sure if it covers all cases and if there is a simpler solution:

git stash git clean -f -x -d git checkout master git fetch origin git reset --hard origin/master git pull origin master 

Is there a better way to do this?

+4
source share
2 answers
 git reset --hard 

discards any changes that you have, including everything that you put.

 git clean -xdf 

will get rid of anything in your working directory that git does not track due to ignore or exception.

 git checkout HEAD 

will put you without a branch so you can clear everything.

 git branch | xargs git branch -D 

will delete all your local branches

 git tag -l | xargs git tag -d 

will delete all your tags

 git prune origin 

get rid of any tracking branches that are no longer on the remote

 git fetch 

will receive any branches and tags from a remote computer that you don’t have. You can stop here and check out any remote branches you want to work on when creating a local branch using

 git checkout origin/master 

or you can create local branches indicating where all existing remote branches point to your selection with

 git branch -r | xargs -n 1 git checkout 
+3
source

The following set of commands should bring you to an identical state with the remote branch:

 git checkout -f master # Check out the local 'master' branch git fetch origin # Fetch the 'origin' remote git reset --hard origin/master # Reset all tracked files to remote state git clean -dxff # Remove all non-tracked files 

Please note that (for obvious reasons) these commands are potentially destructive - that is, any local changes that will be overwritten by this and not saved in a commit accessible in some other way will be lost.

+2
source

Source: https://habr.com/ru/post/1436084/


All Articles