Cómo sincronizar el repositorio git local con origin/master eliminando todos los cambios

I want to sync my local repository so as to make the local an exact copy of the master. Since checking it out I have added several files to my local, not present in master, that I do not wish to commit. Nevertheless, in this procedure, I want to erase all differences of my local from master: when I am done, the additional files in local will have been deleted.

An earlier question offers a strategy to make local like master: Restablecer la rama del repositorio local para que sea como el repositorio remoto HEAD

But this does not work for me. The recommended commands, git fetch origin git reset --hard origin/master

do not erase the additional files from my local, even though git status indicates local and master are identical. And also, in one case the master's version of a file did not replace my local version.

¿Alguna idea sobre cómo hacer esto?

preguntado el 13 de septiembre de 13 a las 10:09

can you add the log of a git status and name the files in that list that you want removed? -

Now that there're 3 answers, please select the one that best fits your needs and accept it. -

3 Respuestas

Actually, the commands you tried will reset all tracked files to the state of origin/master. However, git doesn't touch untracked files (usually). In fact, the whole purpose of the "untracked file"-feature is, to be able to have file completely independent of git inside the repository.

However, you can still make git delete untracked files if you want to:

To delete all untracked files from your repository, type:

git clean -f

(Fuente: ¿Cómo eliminar archivos locales (sin seguimiento) del árbol de trabajo actual de Git?)

Be aware, as the files you delete are untracked, they will be lost forever.

contestado el 23 de mayo de 17 a las 13:05

Actually, @oznerol256's and @wei2912's solutions may produce slightly different results. git clean -f will not remove files that are ignored by git, only the ones that are listed as "untracked" when you run git status. Wei's solution will remove todo and then restore only tracked files. - Jan Warchoł

I hate that this sort of question needs to be answered. But thanks for answering it. - jorfus

I think that you're mixing two things up:

  • updating your repository to be in sync with remote repository
  • resetting your working directory to be identical to a particular commit.

Para actualizar tu repositorio (i.e. the database in which git stores all information about your project and its history), use git fetch remote_repository_name (remote_repository_name es generalmente origin). After running this command, all commits and objects that are present in the remote repository origin will be present in your repository.

Ahora, la directorio de trabajo - i.e. the files that you see in the folder containing the project - is something different from the repository database itself. Notice that you cannot make the working directory to be the same as the remote repository, because these are two different kinds of things (a directory with files vs. a git database). You can reset your working directory to reflect a particular commit (for example tip of master derivarse de origin) - here @oznerol256's answer te da lo que necesitas.

respondido 09 mar '21, 13:03

As the files are not yet committed, you can use:

rm -rf ./*
git reset --hard HEAD

This will wipe out all files, then revert the directory back to its original state.

Respondido el 13 de Septiembre de 13 a las 10:09

I don't think this is an optimal solution, so I'll be researching for a while. - wei2912

Make sure you dont delete your .git folder with that rm command! - puntero de función

@Oznerol256 * in bash won't include files/folders that start with .. necesitarás un .* specifically. Still, best to be safe. - wei2912

Good to know! I wasn't aware of that. - puntero de función

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.