6 Staging Changes to Commit

Git uses a two-step process to get changes into the repository. The first step is staging changes through git add. Staging a change adds it to the index, or staging area. This sits between the working tree—your view of the repository—and the actual repository.

Through the staging area, you can control what is staged from the most coarse-grained—adding everything within the repository—down to editing the changes, line by line.

First you can select individual files or paths to add by calling git add and passing the filename or path as the parameter. Git adds everything under a path if you provide that. It uses standard shell-style wildcards, so wildcards work: base.* matches base.rb and base.py.

Another quick way to add all files is the -A parameter. This adds all the files inside the repository that are not explicitly ignored (see Task 8, Ignoring Files). Closely related, you can add files that have changed using the -u parameter. It doesn’t add any new files, though, only files that have already been tracked and have modifications in them.

You can control which parts of a file you commit using the -p parameter. Running this, you’re presented with each section of the file that has changed, and you’re given the opportunity to add or skip it. You can stage the change by pressing y or skip a change with n. s lets you break the change into smaller pieces. This and a few other options aren’t always available. You can press ? inside patch mode to get a list of all the commands and what they do.

Taking the control a step further, you can directly edit the changes that are being staged by using the -e parameter. This opens the diff in your configured editor (we talked about that in Task 2, Configuring Git). Your editor has the file in a diff format—additions are prefixed with +, and removals are prefixed with -.

One quirk of Git is that it can’t track empty directories (at least as of version 1.7.2.1). There’s a reason for this in the underlying architecture and the way Git tracks data in the repository, but that’s a bigger topic than this page allows for. To track an “empty” directory, you can add an empty dot file (a file beginning with a dot). An empty .gitignore works (see Task 8, Ignoring Files). I use .include_in_git.

What To Do...
  • Stage an entire file to commit.
     
    prompt>​ git add path/to/file
     
    ...​ or ...
     
    prompt>​ git add path/
     
    ...​ or everything under the current directory ...
     
    prompt>​ git add .
     
    prompt>
  • Add all files in the current repository.
     
    prompt>​ git add -A|--all
     
    prompt>
  • Add all tracked files that have been changed.
     
    prompt>​ git add -u|--update
     
    prompt>
  • Choose which changes to commit.
     
    prompt>​ git add -p|--patch
     
    ...​ or a specific file ...
     
    prompt>​ git add -p path/to/file
     
    prompt>
  • Open the current diff in the editor.
     
    prompt>​ git add -e
     
    ...​ or a specific file ...
     
    prompt>​ git add -e path/to/file
     
    prompt>

Related Tasks

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.73.207