59

repo status shows me a lot of un-wanted changes.

It would be duplicated if I enter every project and use git reset --hard.

Is there a way to reset all the changes using repo, something like repo reset --hard?

5 Answers 5

119

This is the command I use for this kind of things, very useful

repo forall -vc "git reset --hard"

What everything mean here ?

the repo forall will execute for all repos.

the -v is verbose, so it will print the output of the command

the -c "COMMAND TO EXECUTE" is the actual command you want

4
  • This is the correct answer. Just to add, if you want to suppress the output, omitting the -v argument didn't work for me, it still printed the output. I had to add &>/dev/null to suppress the output.
    – Anton
    Nov 4, 2014 at 18:43
  • -v does not appear to be needed anymore as of v1.12.37. Jan 13, 2018 at 10:03
  • 1
    repo forall -c 'git reset --hard ; git clean -fdx' is better as git reset --hard will not remove untracked files, where as git clean -fdx will remove any files from the tracked root directory that are not under Git tracking along with any ignored files.
    – zeitgeist
    May 2, 2022 at 9:31
  • Working on some strange repo with gits placed one below the other, I found that the repo forall processes the tree bottom-up. for such screwed up repos that fails. Can check order with repo forall -c "pwd" Aug 23 at 10:38
34

If there is a need to revert working folder to the clean state where you don't have local modifications and no untracked files (i.e. where repo status shows nothing), I found these two approaches useful on repo sync and deleted/modified files

repo forall -c 'git reset --hard ; git clean -fdx' 

or

rm -rf * ; repo sync -l
// note that .repo is preserved after that

Note, that this is not equivalent to initializing a new local repo and syncing (e.g. stash is preserved)

If you are not sure what is going on, please read the full thread repo sync and deleted/modified files

4
  • Cool, I like this method: fast and clean, exactly like "rm -rf *; git checkout -f" provided the ".git" exists.
    – Peter Teoh
    Aug 3, 2015 at 5:33
  • I think it will not remove the committed changes in local repository (will remove only the changes in the working directory and staging area) which can lead to conflicts if it is followed by repo sync on a newer version for example. git reset --hard origin might remove local commits. Can anyone confirm?
    – zeitgeist
    May 2, 2022 at 9:36
  • I tried rm -rf * ; repo sync -l on my local workspace and it removed the local commits in my projects. So, I think it is a very good way to reset your workspace and make a fresh build.
    – zeitgeist
    May 12, 2022 at 9:19
  • What would be the command to have multiple threads with repo sync -l. Will it make it faster?
    – zeitgeist
    Jun 29, 2022 at 7:11
17

The working command is :

repo forall -vc "git reset --hard"

The command and options description

  • forall

Executes the given shell command in each project

Options (that can be used with the forall command)

-c: command and arguments to execute. The command is evaluated through /bin/sh and any arguments after it are passed through as shell positional parameters.

-p: show project headers before output of the specified command. This is achieved by binding pipes to the command's stdin, stdout, and sterr streams, and piping all output into a continuous stream that is displayed in a single pager session.

-v: show messages the command writes to stderr.

For more information please refer the repo document

2
  • 4
    repo sync -d did not work for me. repo status still showed all my unwanted changes.
    – Amanda S
    Jul 30, 2012 at 19:47
  • 1
    Actually you should have made this another answer. Will confuse people this way Nov 22, 2017 at 18:27
9

I use the repo forall command with the below syntax and it helps me to reset the tracking files.

repo forall -p -c 'git checkout -f foo'

where foo should be replaced with a legitimate branch name.

7

repo forall does not cover all of the git tree

#!/bin/bash
for gitdir in $(find . -type d -name .git -prune); do
    pushd $(dirname $gitdir)
    git reset --hard
    popd
done
4
  • repo forall as in rans comment
    – sherpya
    Nov 2, 2012 at 13:38
  • Sometimes in my working copy, repo forall doesn't cover all the git tree. This approach work greatly.
    – alijandro
    May 27, 2016 at 0:22
  • Thanks! I just needed to use this command as repo forall didn't seem to fix every git repo. Nov 1, 2016 at 6:41
  • 1
    I'd add -type d and -prune to find command to not descend into repo dirs once .git dir is found.
    – Max_Payne
    Dec 20, 2021 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.