Vscode / GIT - Put only a few file changes into Stage?

Asked

Viewed 115 times

0

Using the VSCODE + GIT, made several changes in a same file, on a necessary implementation.

In order for these implementations to be made, it was first necessary to adjust/correct a pre-existing code (already committed at another time), and then to apply the implementation. However, I did it all at once, and when I went to generate the implementation commit, I noticed that I could split into several commits to get more detailed in small commits the changes/fixes made and other with the implementation.

For example, imagining this is the code below in the same file:

// parte do código adaptado/ajustado independente da implementação
function corrigindoMetodoAnterior(params)
{
    // executando alguma ação pertinente
    // ...
}


// referente a uma implementação feita
function implementandoNovoMetodo(params)
{
    // executando alguma ação pertinente
    // ...
}

// conteúdo temporário
function naoCommitarEste(params)
{
    // informações de teste que não é pra commitar
    // ...
}

Supposing that all these acima lines are as "edited", being that:

  • the first function corrigindoMetodoAnterior existed but has undergone changes;
  • the second function implementandoNovoMetodo was added because it did not exist;
  • the third function naoCommitarEste is a developing method that will not enter any commit;

So, every function of this, I want it to be in separate commits:

  • "Adjustments of the 'correcting' function All Material for better performance"
  • "Adding new 'implementationNovoMetodo' function to meet new demand"

And don’t have in any commit above the 'naoCommitarThis'

There is how to do this graphically straight on VSCODE using the Source Control of GIT, or only by command line?


Equivalent to what I want with a command line would be:

$ git add -p

All file changes will be displayed, with an interactive menu with some options.

Choosing the option s (split), it will present each amendment separately.

(...)
Stage this hunk [y,n,q,a,d,s,e,?]? s

So just give y (yes) or n (no) to add or not to commit.

(...)
Stage this hunk [y,n,q,a,d,K,j,J,g,/,e,?]? y / n

1 answer

0


Accessing the Stackoverflow in English I found the answer to doubt :)

Follow the steps below:

  • After all changes made to the file
  • Open the Source Control
  • Select the file I want commit the amendments
  • In the change comparison window, on the altered post side (by default on the right) select only the lines you want to add in the commit
  • At the top of the window, click (...) (3 dots), and click on Stage Selected Ranges.

Okay, only the selected part will go to the Stage, keeping the rest as Untrack.
Now just put the commit message, and move on to the next one commit.

Below is a GIF I copied from the original answer (English version)

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.