Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 777263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:34:40+00:00 2026-05-14T19:34:40+00:00

Is there a git command to add differences within a range of line-numbers to

  • 0

Is there a git command to add differences within a range of line-numbers to the index?

I want to be able to select lines in my editor and run a macro to add any changes in the selection to the index.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:34:40+00:00Added an answer on May 14, 2026 at 7:34 pm

    If you can persuade your editor to write a version of the file that you want to be staged, you can use the plumbing-level git commands to add it to the index under the right name. You need to bypass “git add” which will always associate path X in the working tree with path X in the index (as far as I know).

    Once you have the content you want to stage written to some temporary file $tempfile, run git hash-object -w $tempfile – this will write the object to .git/objects and output the blob id. Then feed this blob id to the index using git update-index --cacheinfo 100644 $blobid $path to associate the path $path with that object.

    Here’s an example that stages a change to a script called “post_load” in my repo without overwriting the file itself (also demonstrating you can do without a temp file):

    git update-index --cacheinfo 100755 $(perl -lne 'print unless (/^#/)' post_load \
                                          | git hash-object -w --stdin) post_load
    

    You don’t mention which editor you’re planning to do this from, so it’s hard to advise you on how to integrate this. As I mentioned, you need to somehow present git with the file as you want it to be staged (remember, git doesn’t deal with storing changes). If you can write a macro to just save the file as “$file.tmp”, then use something like the above to git update-index --cacheinfo $the_mode $(git hash-object -w $file.tmp) $file (obtaining $the_mode is left as an exercise :p), delete $file.tmp and revert the editor buffer back to $file that would do basically what you’re asking for.

    For example, the following script takes three arguments: M N path. It will update the index content for the file at “path” so that lines M through N (inclusive) are replaced with the content from stdin:

    #!/bin/sh
    
    start_line=$1
    end_line=$2
    path=$3
    
    mode=$(git ls-files -s $path | awk '{print $1}')
    blob_id=$(
        (
            head -n $(expr $start_line - 1) $path
            cat
            tail -n +$(expr $end_line + 1) $path
            ) | git hash-object -w --stdin
        )
    exec git update-index --cacheinfo $mode $blob_id $path
    

    for example echo "HELLO WORLD" | ./stage-part 8 10 post_load will replace the three lines from 8-10 with just “HELLO WORLD”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to add a Subversion repository as a Git submodule in
From the man page on git-merge, there are a number of merge strategies you
I've been wondering whether there is a good "git export" solution that creates a
I'm using git for a PHP project, I think it's really handy. There is
I'm having trouble finding an open-source IDE with support for Git. Are there any
There is a conversion process that is needed when migrating Visual Studio 2005 web
There are two weird operators in C#: the true operator the false operator If
There are two popular closure styles in javascript. The first I call anonymous constructor
There is previous little on the google on this subject other than people asking
There seem to be many ways to define singletons in Python. Is there a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.