Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create multiple stages in Git

Tags:

git

When using Git it is possible to stage files as per https://githowto.com/staging_changes. So typically

git add file1
git commit
git add file2
git commit

What I can't see however is a way to create multiple stages (so I can split up a large editing session) that can be saved with a single commit. Is this even possible with Git?

like image 315
Caltor Avatar asked Mar 01 '26 01:03

Caltor


2 Answers

The way Git is used is usually like this:

  • edit files;
  • stage the files, either some of them or all of them;
  • when the stage looks good, you commit it;
  • the files not previously staged, could then be staged and committed later;
  • you reiterate this process creating a new stage (another set of files), and committing it, and so forth;

Hence if you want to commit a subset of the modification resulting by a long editing session, just stage only some files and commit those. As usual, the advice is to keep your changes in a small related set inside a commit, avoiding at all costs the huge meaningless commit of unrelated changes.

An update regarding branches: If what you need is to put aside some changes you created on the master branch and work on something else, you could store temporarily your changes on another local branch, such as:

 - create a new branch called "temp_changes" (new branch);
 - switch to it (checkout);
 - stage the editing you want to store in this branch
 - commit the stage;
 - switch back to master branch (checkout);

Thereafter you could work on master branch unaffected by those modification that now are stored in _temp_changes_ branch only. You could also push this branch remotely if you need to share it with anyone else or to store it safely.

like image 152
Luca Cappa Avatar answered Mar 02 '26 14:03

Luca Cappa


The simple answer is 'no'.

Git only allows only one single staging area.

Although it could help to have them in many situations, because work is not always linear.

There do exist solutions to rewrite history after the changes have been commited, like rebasing or 'fixups', but this is not the same.

like image 25
mit Avatar answered Mar 02 '26 14:03

mit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!