Git has many shortcuts that you can use to make yourself more productive. One of the little known (and simple!) features is regarding restaging files that have already been staged.
Let’s create a simple repository with three files to setup an example:
All right. With the initial commit done, let’s make changes to these files, and add the changes to the index:
file2 have been changed, and added to the index. Let’s say we now make changes to
file2, with their initials changes, are in the index. New changes to
file1 are to be added, along with changes to
So with our contrived example all setup, we can actually come to the main material of the post. Suppose you want to add only files that have already been staged, but have new changes in the working directory now. Files like
file1. Git has an handy shortcut for this, but you have to look beyond
git add. All that is needed to “restage” files in git is the following command:
From the docs, this
Runs git update-index itself on the paths whose index entries are different from those from the HEAD commit.
Which is exactly what we want in this scenario. Hope this nice little command makes you more productive. Git has lots of these unused little gems. Git is a complex beast, yes, but some of these need to be discovered and used.
For those who add aliases for these things, here you go: