Nicolai
Nicolai

Reputation: 62

Git LFS alteration: what does this odd sequence of commands really do

could someone explain to me what this series of commands actually do?

git rm --cached -r .
git reset --hard
git rm .gitattributes
git reset .
git checkout .

I took it from an answer to Git error: Encountered 7 file(s) that should have been pointers, but weren't. It does what it's supposed to do but I would like to know why it works and what it does. It usually feels super scary to blindly follow instructions containing --hard, reset and rm...

Upvotes: 0

Views: 647

Answers (1)

Schwern
Schwern

Reputation: 164919

could someone explain to me what this series of commands actually do?

It seems to be a long winded way of writing git reset --hard with most of it effectively doing nothing, but I could be wrong. See the end of this answer.


I can explain git reset --hard.

When a Git checkout is dirty there are two areas to clean up.

  • The working tree (ie. the files themselves)
  • The staging area (ie. what you've Git added)

For basic Git, without git-lfs, you need two commands.

  • git reset --hard (the default is HEAD, the current commit)
  • git clean

git-reset is kind of like git-checkout but on steroids. It changes which commit you have checked out, but you can control whether the staging area and working tree change with it.

git reset --soft only changes to the new commit, your files and staged changes are left alone. git reset --soft HEAD^ will move you to the previous commit but all the changes in the commit will be staged, and all the unstaged changes will remain. This is useful if you want to redo a commit, and I have it aliased to redo.

git reset --mixed changes the commit and resets the staging area. git reset --mixed HEAD^ is like above, but all the previous commit's changes will be unstaged.

git reset --hard changes the commit and resets both the staging area and your files. This wipes all your changes and puts you back into a clean state. git reset --hard HEAD wipes out all your changes and restores you to the current commit. git reset --hard HEAD^ wipes out all your changes and restores you to the previous commit; this is useful if you want to undo a commit, and I have it aliased to undo.

Last commit you had checked out is always ORIG_HEAD so you can always return to that with git reset --hard ORIG_HEAD.

Finally, if you have any untracked files git clean will delete them.

See Reset Demystified for more.


What is that answer doing?

  • git rm --cached -r .

This clears the staging area. It is not necessary because...

  • git reset --hard

...this also clears the staging area and moves you to a pristine version of your current commit, as discussed above.

  • git rm .gitattributes

gitattributes let you define attributes on paths in your repository. Tools such as git-lfs use it to mark which files are using LFS. Blowing the whole thing away will reset all attributes, so depending on what other tools you're using this might not be a good idea.

  • git reset .

This is git reset --mixed HEAD. This would unstage the git rm they just did. I don't know why you'd want that.

  • git checkout .

This is git checkout HEAD which restores .gitattributes making the whole exercise moot.

So, unless I'm missing something, all that is just git reset --hard HEAD.

Upvotes: 1

Related Questions