Git Staging Commandem Style
Staging in git is when you gather all the files you want together, new, deleted, and unchanged — for a commit. Once you commit them they are tracked as a commit with a message and a number, and then available to push to a central repository or where ever.
I use the Tortoise Git GUI before I check in that can add/stage in one commit action, then a separate push action. It’s because I need a graphical view of everything to eyeball before committing. The command line doesn’t really do it for me, especially if I have a large commit.
But the question often comes up – how to stage manually in git?
Here’s staging from the command line as I do it. There are two methods that I know of right now.
Navigate to your directory – I usually do this from “parent pom” directory so I can also maven build (for us java developers). But somewhere in your local repository.
Git’s “add” is a bit dual-meaninged, like many git commands (merge for example which is different for branches versus clones). It can mean “track me now and stage me” and it can mean “just stage me.” This idea can be confusing — for instance with a new file why not “add” then “stage”? But it’s all one execution in that case.
Generally there are three states to “add” to a stage —
- you need to ADD any files to the stage that the repository does not know about that are new files. This will stage new files for commit.
- you need to ADD any files to the stage that git already knows about that have changes. This will stage changed files for commit.
- you need to ADD any files to the stage that git already knows about that have been deleted . This will stage changed files for commit.
Atomic Staging
First, I use “git status” to see what’s up. It lists everything changed and everything not tracked by the repository yet.
What I usually do is this for the atomic method:
git status //see what’s up
git add <new file>
git add <changed/deleted file>
To unstage your changes use
git reset
Recover a deleted file with
git checkout — <file>
Be sure to use the full path from git status
After staging –> commit, push.
Interactive Staging
You can also do this same thing interactively, which is what I use – git add interactive.
git add -i
Command 4 let’s you stage the new file by its number, and command 2 let’s you stage a changed file by its number in a list that is presented to you in both cases. Command 3 let’s you revert from stage very easily.
You can check it out more in depth here:
http://git-scm.com/book/en/Git-Tools-Interactive-Staging
https://www.kernel.org/pub/software/scm/git/docs/git-add.html
After staging –> commit, push.
I do not do blanket adds, like git add –all because one never knows what file is NOT ignored that may pop up into the directory! Like a new .project file or some funky log file.
I *always* diff all files before I check in.
I *always* eyeball my file collection before I stage.
I *always* eyeball my commit before committing.