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

5

19

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?

Ecbatana answered 25/10, 2018 at 18:3 Comment(6)
There's a git clone command....Maryrose
Hi, and welcome to StackOverflow. What is the reason you want to do this? I ask because if you've gotten your repository into a bad state there's usually a better way to fix it than wiping everything out.Politician
Ok removing the directory, looking vor the correct url...and then git clone works but it would be convinient to do this without removing and recloning..Ecbatana
@schwern : 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 remoteEcbatana
@Ecbatana Oh, there's much better ways to bring your repo up to date. git pull is the primary mechanism.Politician
@Schwerm, I know but this can still leave me with a lot of unnecessary branches, tags and stuff which can be confusing.Ecbatana
M
13

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.

Maryrose answered 25/10, 2018 at 18:32 Comment(3)
Okay yeah I was afraid this was the case but maybe there was some secret single git command way :-) Ok so rm -Rfv & git clone is the way :-)Ecbatana
this answer makes some assumptions ... the upstream you want to track is origin ( which granted is often true), and the default branch or the branch you care about is master ( less often true )Logical
@Dan Those are examples, not assumptions. The assumption is that OP will use this code with understanding, instead of literally. I assumed that they (and future readers) read carefully enough to understand that origin is an example of <name-of-my-upstream>, and master is a sample stand-in for <name-of-my-branch>. I appreciate you making it explicit though.Maryrose
G
5

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
Gavage answered 25/10, 2018 at 22:33 Comment(0)
P
2

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.

Politician answered 25/10, 2018 at 18:56 Comment(0)
R
2

Simply use clean. (docs) And maybe git reset --hard before if you want to reset changes.

git clean -dfx
  • -d Also remove untracked directories.
  • -f Force delete all files and directories without confirmation.
  • -x Removes all untracked files, even ignored files.
Rawboned answered 18/10, 2023 at 9:5 Comment(0)
T
0

I am unsure wheather this is really setting back everything, but at least for me it cleans everything and makes it ready for clean reusage, just like cloning it:

git reset --hard
git clean -dxf
git checkout master
git pull
git submodule update --init --recursive --force

Caution: You loose all files not already in the repo ...

  • The "reset", takes back all obstacles to upgrade to latest changes (you want to clone fresh, so you WANT the latest changes).
  • the "clean" removes all superflouus files, which are not tracked or elsewhere used
  • the "checkout" sets you on the right path [but not at the topmost position]
  • the "pull" has now free way to get all changes up to the top of the repo
  • the "submodules", if any, are updated to the pinned versions.

I must say that the "submodule"-part is the most weak portion of git here. In my personal script, I go (in addition to the commands above) into every submodule path and do a reset/clean/checkout/pull ...

Trull answered 28/12, 2023 at 0:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.