Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reset a Git repository to its newly-cloned state?

Tags:

git

git-reset

Is there a git statement that has the same result as a fresh git clone?

So even if there are extra branches, extra files, other files, local tags anything... which command can accomplish this?

like image 355
byenary Avatar asked Oct 25 '18 18:10

byenary


People also ask

How do I reset a cloned repository?

(Optional if git status says no modified files) Discard any modified files on the disk (that's why reset --hard ) Checkout the remote master branch (note: you will be in a “detatched head” state) Delete the local master branch (throwing away all your local changes) Call the current head as the new master branch.

What would happen if you cloned an existing git repository?

When you clone a repository, you copy the repository from GitHub.com to your local machine. Cloning a repository pulls down a full copy of all the repository data that GitHub.com has at that point in time, including all versions of every file and folder for the project.

How do I re initialize a repository?

You can do this, and it will not affect your remote repos at all (you'd have to delete those seperately if that is what you want). delete on local, than checkout the repo again ;) @Thilo - exactly just remove the . git directory, run git init, remove all your remote repos and push again.


3 Answers

Your best option to remove all local artifacts is to make a fresh clone.

Otherwise, you have to run different commands to perform the individual components of the cleanup.

To get rid of all untracked files, use git clean, with any number of flags turned on to wipe out everything:

git clean -fd

Now reset all your branches to the remote version (git reset) or delete them if they are local-only (git branch):

git checkout master
git reset --hard origin/master

git branch -d local-branch

The same goes for tags. You can delete all local tags like this:

git tag -d $(git tag -l)

You can do a lot of these operations manually as well, by manipulating the contents of the .git folder, if you know what you are doing.

As you can see, git clone is by far the easier option.

like image 74
Mad Physicist Avatar answered Nov 08 '22 18:11

Mad Physicist


If you really want to delete all local work I also think cloning the simplest thing. If the repository is big and/or the network is slow then you can use these options:

git clone --reference existing-clone --dissociate URL new-clone
like image 43
A.H. Avatar answered Nov 08 '22 18:11

A.H.


sometimes some work was done, some things committed some not etc.. then dont't work on a project for weeks, other people keep working.. when you then come back to a project you want to start clean without figuring out where your local code is compared to the remote

If all you want is to bring your local branches up to date, you can solve this without wiping out your repository. Let's say your repository looks like this.

          D - F [feature]
         /
A - B - C [origin/master]
         \
          G - H [master]

You can get a very similar view with git log --graph --decorate --all, though it will be sideways.

You made some changes to both master and a new branch called feature. You already have clean versions of the remote work. origin/master tracks the remote work on the remote master branch.

This doesn't stay up to date automatically. You can update it, and all your other remote tracking branches, with git fetch -p (-p will prune any old remote branches).

git fetch -p

          D - F [feature]
         /
A - B - C - I - J - K [origin/master]
         \
          G - H [master]

Now that your origin/master is up to date you can look at it like any other branch to see the latest work.

You can bring any other local branch up to date with the latest changes with git rebase origin/master. This will replay any local changes on top of origin/master. For example, to update the feature branch...

git checkout feature
git rebase origin/master

                      D1 - F1 [feature]
                     /
A - B - C - I - J - K [origin/master]
         \
          G - H [master]

Or, if you're not interested in a branch anymore, you can delete it.

git branch -D feature

A - B - C - I - J - K [origin/master]
         \
          G - H [master]

Finally if you simply want to throw out your changes on master use git reset --hard to force master to move to origin/master.

git checkout master
git reset --hard origin/master

                      D1 - F1 [feature]
                     /
A - B - C - I - J - K [origin/master]
                      [master]

And now you're up to date.


To answer the question about wiping out a repository...

Git repositories are generally very small (if they aren't that will cause other issues). Assuming you have decent network connection the simplest thing to assure a truly clean reset is to clone it again.

If that's not possible, the next best option is to follow these instructions. But getting out of a jam by cloning a new repository is a bad habit to get into. Practice gardening your repository.

clone your existing clone and clear out its local branches and tags. This will guarantee things like .git/hooks and .git/config are reset.

First, clone the local dirty repository.

git clone /path/to/the/dirty/repo /path/to/the/new/new_repo
cd /path/to/the/new/new_repo

Then delete all your local branches and tags.

Git won't let you delete the current branch, so checkout origin/master, the upstream version of master.

git checkout origin/master

Get all your local branches and delete them.

git for-each-ref --format '%(refname:short)' refs/heads

Get all your local tags and delete them.

git for-each-ref --format '%(refname:short)' refs/tags | xargs git tag -d

Recreate a fresh local master from origin/master and check it out.

git checkout -b master origin/master

Set your origin to be the original repository. You can get that from git remote -v in the dirty repository.

git remote set-url origin <original origin url>

Finally do a git pull to get the latest version of the upstream repository, its branches, and update master.

git pull

As you can see, it's easier to garden an existing clone.

like image 29
Schwern Avatar answered Nov 08 '22 18:11

Schwern