58

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?

Johnny
  • 6,239
  • 7
  • 29
  • 36

5 Answers5

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

stdcall
  • 27,613
  • 18
  • 81
  • 125
  • 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 04 '14 at 18:43
  • `-v` does not appear to be needed anymore as of v1.12.37. – Ciro Santilli OurBigBook.com Jan 13 '18 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 02 '22 at 09: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"` – Blindleistung Aug 23 '23 at 10:38
33

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

pmod
  • 10,450
  • 1
  • 37
  • 50
  • Cool, I like this method: fast and clean, exactly like "rm -rf *; git checkout -f" provided the ".git" exists. – Peter Teoh Aug 03 '15 at 05: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 02 '22 at 09: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 '22 at 09:19
  • What would be the command to have multiple threads with `repo sync -l`. Will it make it faster? – zeitgeist Jun 29 '22 at 07:11
16

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

Shantha Kumara
  • 3,272
  • 4
  • 40
  • 52
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.

MDMower
  • 808
  • 8
  • 27
rans
  • 91
  • 1
  • 2
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
h0tw1r3
  • 6,618
  • 1
  • 28
  • 34