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: Reset local repository branch to be just like remote repository 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.
Any ideas on how to do this?
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
(Source: How to remove local (untracked) files from the current Git working tree?)
Be aware, as the files you delete are untracked, they will be lost forever.
I think that you're mixing two things up:
To update your repository (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
is usually origin
). After running this command, all commits and objects that are present in the remote repository origin
will be present in your repository.
Now, the working directory - 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
branch from origin
) - here @oznerol256's answer gives you what you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With