Is there a way to add a specific version of a file to the git index?

The reason I ask is because I accidentally did git commit -athat included a file that I did not want to commit yet. My solution was to do the following:

git reset --soft HEAD^
git reset -- file/with/changes_not_to_committed
git commit -C HEAD@{1}

Here Ive rewinds the branch by one commit, saving the index and working tree, then pulling file/with/changes_not_to_committedfrom an older version to the index, and then I passed the index using the commit message from the previous commit of the branch, Neither the call git-resetaffects the working copy, so my changes file/with/changes_not_to_committedare saved but are no longer recorded in the revision HEAD.

However, it would be easier if I could pull file/with/changes_not_to_committedfrom the version HEAD^directly to the index without touching the working copy. Since the index otherwise already represents the state I want, I could just make changes to the commit HEADby creating a sequence like this:

git magic-pony HEAD^ file/with/changes_not_to_committed
git commit --amend -C HEAD

This is almost what I would get by replacing git-magic-ponywith git-checkout, except that the working copy will remain untouched and only the index will be updated. There seems to be no way to force git-checkoutboth to not be touched.

My question is: does it exist git-magic-pony, and if so, under what name?

+3
source share
3 answers

Right. HEAD , "git reset REVISION-file" - 'git commit -amend' . , , , , A B, .

, "w22 > -gui", " ", gui, ".

+4

, - , git update-index:

git update-index --cacheinfo 100644 5be7e154c284fb8de8ddca0bb18b72a73089df9b filename

(100644 ) sha1 , ( git ls-tree). git commit --amend, .

+2

So, based on Greg Huglills answer, git-magic-ponythis is a shell script that I would write that looks like this:

#!/bin/sh
git ls-tree "$@" | while read mode type hash path ; do
    git update-index --cacheinfo $mode $hash "$path"
done
+2
source

Source: https://habr.com/ru/post/1699020/


All Articles