How to discard changes using repo

Solution 1:

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

Solution 2:

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

Solution 3:

You can use the repo command as the following to revert all your changes:

repo sync -d

This will revert all your changes back to original revision.

Edited:

The command above is working only with the version at that time.

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