Select part of git command

Every time I want to rewrite the commit in order to save only the diff part, I look through this (from the top of my head, something might be turned off):

git rebase -i COMMIT~1

then select eto change.

git reset HEAD~1  
git add -p # select the part I want
git checkout HEAD -- :/ # discard changes not selected
git stash
git reset COMMIT
git checkout HEAD~1 -- :/
git commit -u --amend
git stash pop
git commit --amend
git rebase --continue

Obviously, this is a terrible workflow. The problem is that reset allows me to select the changes I want, but by moving HEAD back, I stop making corrections to the corresponding commit.

I tried to do git checkout HEAD~1 -- :/when I am in the commit that I want to change and select back diff, but it just got confused.

What is the best alternative?

+4
source share
2 answers

reset , :

git reset head~1 --soft

.

, , , . , .. head~2, . - , , , .

+2

( " " ), . , .

, . , , !

Commits, HEAD,

-, , Git - . : . 1 , , 30 , , Git , , git reset. : , Git. ( , , Mac Time Machine .) , (aka staging-area, aka cache), - , .

, () . 2 Git -only. :

  • , git show HEAD:README ( README);
  • index/staging, git show :0:README;
  • , , , , Git -only.

, . /: git show, git add. git add README, , : .

(aka HEAD) . . , ( , ).


1 , - , , . , - , Git ... ? 🤔 , , .

2 , . - , ( , ), . , , , . .


( , ), , . , , - master feature/tall, , HEAD. . - , , , , , , -, :

... <-parent <-tip <--branch(HEAD)

o , , commits , , , , , - :

...--o--o--o   <-- branch (HEAD)

, .

git commit

, git commit, Git:

  • ( );
  • ( , .., ), Git ;
  • ( ).

, , master, :

A--B--C   <-- master (HEAD)

commit D, C, master , D:

A--B--C--D   <-- master (HEAD)

, , , , . . master , , , HEAD , . , HEAD.

git reset, . git reset, git reset HEAD~1.

git reset HEAD

git reset - -, . HEAD : , , HEAD, , . ~1 . , HEAD master master D, D C.

, , master, . ? : (). :

        D   [no name - abandoned]
       /
A--B--C   <-- master (HEAD)

master, commit C, B, A ( , , ). D, ; 30 , , - , , - .

git reset

git reset , master. reset. , git reset HEAD~1, commit D, commit D . git reset HEAD~1 commit C. , git reset ! Git Git - HEAD (, , Git -only).

git reset , --soft. , commit D ( , C - , , , ).

git reset ,

, git reset . : git reset .

git reset , --hard. , Git , . Git - , commit C, .

: git reset ;

git reset ( !), ) , , :

  • . --soft, .
  • . --mixed , .
  • -, --hard, ( ) 2.

, , , 1, --soft: ; 2, --mixed/ : .

git add -p git reset -p

, git add . , , ? , , , , , , :

$ cp README README.save
$ vim README
$ git add README

, , :

$ mv README.save README
$ git commit

. git add -p .

, git add -p , , . , "diff hunk", Git hunk , . , Git , ( ) . , -, . . ( , .

git reset -p, diff, HEAD. , HEAD.

-

Git : - - , . git show , .

, : , , Git . , , .

, git cherry-pick, , , , HEAD . 3 , : Git .


3 Git , , , . , , , --theirs commit HEAD --ours.


Rebase = reset +

Rebasing in Git, , git reset --hard, . 4 Git " HEAD" ", () , , , rebase :

  • :

                 D--E--F   <-- feature (HEAD)
                /
    ...--o--A--B--C   <-- mainline
    

    D F. ( , Git , rebase .)

  • Reset ( ) , , , C. reset git reset --hard: .

  • - , . , .

  • , , HEAD . ( rebase ORIG_HEAD, ):

                 D--E--F   [ORIG_HEAD]
                /
    ...--o--A--B--C   <-- mainline
                   \
                    D'-E'-F'  <-- feature (HEAD)
    

    ' , .

2. pick Git , . pick edit, Git , . pick reword, Git --edit, . ( ), ; , pick. , - - .


4 rebase git cherry-pick, - . . , git apply -3 , . .


git commit --amend

:

A--B--C   <-- master (HEAD)

, git add git commit, Git commit D C master D:

A--B--C   <-- master (HEAD)

, , git commit , C, C , git commit --amend. , , commit D, commit B:

     C   [abandoned]
    /
A--B--D   <-- master (HEAD)

Git , , , , D, B, A. - commit C commit D, .

, , , rebase.

rebase

, :

...--F--G--H--I   <-- branch (HEAD)

- commit G, , - . commit F , commit F, git rebase -i.

, F, HEAD~3, ~3 : I (0), H (1), G (2), F (3). , :

git rebase -i HEAD~3

pick, G, H, I F, :

       F--G--H   [abandoned]
      /
...--E--F'-G'-H'  <-- branch (HEAD)

, , pick edit, Git , , :

       F--G--H   <-- branch
      /
...--E--F'  <-- HEAD

( " HEAD" ). commit F'.

- - , , F', git reset -p . git reset --mixed HEAD~1, F' , git add -p. , , F', .

, F' , git reset -p. . git commit --amend, , F'', F F', , . --amend git commit, F'' parent, E, F':

       F--G--H   <-- branch
      /
...--E--F''  <-- HEAD
      \
       F'  [abandoned]

, , F', git reset --mixed:

       F--G--H   <-- branch
      /
...--E   <-- HEAD
      \
       F'  [abandoned]

git add -p . , git commit ( --amend) commit F'':

       F--G--H   <-- branch
      /
...--E--F''  <-- HEAD
      \
       F'  [abandoned]

git add, . , commit I, :

       F--G--H   <-- branch
      /
...--E--F''-I  <-- HEAD
      \
       F'  [abandoned]

git rebase --continue, Git - G G' H H':

       F--G--H   <-- branch
      /
...--E--F''-I-G'-H'  <-- HEAD
      \
       F'  [abandoned]

, , Git , :

       F--G--H   [ORIG_HEAD]
      /
...--E--F''-I-G'-H'  <-- branch (HEAD)
      \
       F'  [abandoned]

.

+1

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


All Articles