2

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.

Community
  • 1
  • 1
Enrico
  • 325
  • 1
  • 2
  • 15

3 Answers3

6

Since the binaries generated by your build aren't tracked by git, they are not considered to be local changes.

git clean -xdf

should eradicate them for you nicely.

Matt Alioto
  • 403
  • 2
  • 10
  • What I don't understand is: why the binaries aren't tracked by git? – Enrico Mar 18 '17 at 18:02
  • 2
    Untracked files are files that haven't been added to your Git repository yet. Since those binary files haven't been committed yet, Git isn't tracking changes to them. Hence, why they can be removed with [git clean](https://git-scm.com/docs/git-clean) – Stevoisiak Mar 18 '17 at 18:18
  • 1
    More conceptually, your binaries generally *shouldn't* be tracked by Git (and should be covered by your `.gitignore` file, if you have one). There are a couple of different ways to track the binaries generated by your builds - binary storage repositories like Artifactory are a favorite of mine - but since binaries aren't source code, they usually shouldn't be committed to a Git repository. – Matt Alioto Mar 18 '17 at 18:38
  • In fact I found out a `.gitignore` and had to specify the `-x` option. – Enrico Mar 18 '17 at 19:11
5

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
Community
  • 1
  • 1
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
  • I am comparing this answer to the one by Matt Alioto: he proposes just one of these steps, the third. Do I really need all three steps? Note that I content myself to revert back to the rep *as it was when I cloned it* - possible changes made afterwards are of no relevance. – Enrico Mar 18 '17 at 18:06
  • 1
    `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. – Stevoisiak Mar 18 '17 at 18:09
  • Ok, so to my aim I don't need `git fetch`. `git clean` will remove the binaries. But `git reset`? If I force the checkout as in my proposed command, doesn't it alredy discard as a consequence the local changes to tracked files? – Enrico Mar 18 '17 at 18:13
  • I'm personally more familiar with `git reset` than I am forced checkouts, but if it works for you you could probably use `git checkout` instead of `git reset`. Your mileage may vary though. – Stevoisiak Mar 18 '17 at 18:16
  • One last question. I had a look at [Git clean documentation](https://git-scm.com/docs/git-clean). The -d option cleans directories as well. The -f option may be required under certain circumstances in order to force the cleanup. But what about the -x option? To be honest I don't understand whether I need it or not. – Enrico Mar 18 '17 at 18:25
  • 1
    `-x` cleans files ignored via .gitignore. If you don't have a .gitignore you can ignore it. ([Source](http://stackoverflow.com/a/675797/3357935)) – Stevoisiak Mar 18 '17 at 18:29
0

Reset your current branch. Checkout to local/master then create a new branch with remote/branch history.

$ git add .
$ git reset --hard HEAD

$ git checkout master
$ git fetch

$ git checkout -b <branch-name> origin/<remote-branch-name>

# replace <branch-name> = new local branch, <remote-branch-name> = remote desired branch name 

Now bulild again your binaries.

Sajib Khan
  • 22,878
  • 9
  • 63
  • 73