Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo local changes in Git

Tags:

git

My situation: I cloned a repository (source files for a kernel). Then checked out a branch, and built the kernel. The binaries have been stored in a subdirectory of the working tree. Now, I want to discard all local changes, included the built binaries, and revert back to a different branch of the cloned repository.

In particular, I think this answer is useful. It seemed to me that the best method would have been

git checkout -f <desired branch of the original cloned repository>

If I understand it correctly, this should force the checkout, discarding local changes (if I am wrong, please correct me). Git successfully checked out, but I still find the previously built binaries in the same subfolder of the working tree.

like image 372
Enrico Avatar asked Jan 29 '26 02:01

Enrico


1 Answers

Adapted from answers by Dan Moulding and knittl

Setting your branch to exactly match a remote branch, including clearing untracked files, can be done in three steps:

git fetch origin
git reset --hard origin/<branch name>
git clean -f -d

Explanation: git fetch grabs the latest version of the repository. git reset discards any local changes on your branch to tracked files. git clean removes any untracked binary files from your local copy.

For reference, if you want to keep Git from tracking your built binary files, you should probably create a .gitignore file. See Ignoring Files for detailed explanation, but for a Java program you could use the following.

# Compiled files
bin/
*.class
*.jar
like image 198
Stevoisiak Avatar answered Jan 31 '26 18:01

Stevoisiak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!