user3595632 user3595632 - 1 month ago 8
Git Question

Git: is it possible to use number to add(or diff) file on stage?

What I want to do is,

(spacegraphy) Chois@Chois-MacPro spacegraphy-project $ (feature/carts)git status
On branch feature/carts
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified: spacegraphy/carts/tests/test_views.py
modified: spacegraphy/carts/views/cart.py
modified: spacegraphy/functional_tests/test_cartitem_at_product_detail.py
modified: spacegraphy/products/static/js/product_detail.js

no changes added to commit (use "git add" and/or "git commit -a")


When I want to see difference of
spacegraphy/carts/views/cart.py
, I want to type like this:

>> git diff 2


(
2
because
spacegraphy/carts/views/cart.py
is second in the unstaged file list)

Likewise, When I want to add
spacegraphy/products/static/js/product_detail.js
, I want to type like this:

>> git add 4


Would it be possible? Any idea please?

Answer

Yes there actually is. Check out git's interactive staging You will get a menu which is based on numbers.

$ git add -i
           staged     unstaged path
  1:    unchanged        +0/-1 TODO
  2:    unchanged        +1/-1 index.html
  3:    unchanged        +5/-1 lib/simplegit.rb

*** Commands ***
  1: status     2: update      3: revert     4: add untracked
  5: patch      6: diff        7: quit       8: help
What now>

So to diff a file. You would first write 6 to enter diff. Then a list of files will appear that have changes, each with a number. Type the number of the file you want to diff, for example 1. This will output the diff, and you'll get back to the first menu.

Comments