8

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?

Caltor
  • 2,538
  • 1
  • 27
  • 55

3 Answers3

4

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.

Caltor
  • 2,538
  • 1
  • 27
  • 55
Luca Cappa
  • 1,925
  • 1
  • 13
  • 23
  • OK So is Git capable of having multiple stages within a single commit please? – Caltor Jan 05 '17 at 17:17
  • 1
    The answer should be 'no', unless there is a misunderstanding: i would like to understand more about what a stage is for you, and what is the scenario you would use a multiple stage feature? – Luca Cappa Jan 05 '17 at 17:33
  • E.g. I've done 3 separate areas of work since last commit. I would like to create 3 stages which I can commit as separate entities but in a single action. In other words I want the workflow to be Edit > Stage > Stage > Stage > Commit. Whereas I currently have Edit > Stage> Commit > Stage > Commit > [Stage] > Commit. Does that make sense? – Caltor Jan 05 '17 at 18:17
  • 1
    in that case you should divide your files in three sets; for each set of files, put them in the stage, then commit the stage. Do this three times, i.e. for each set. Eventually you get three separate commits, each one with a specific message and specific set of files. Please take a look to the history of the _master_ to realize that you create the separation of your modifications by creating three different commits. – Luca Cappa Jan 05 '17 at 18:21
  • So it HAS to be the second workflow I describe rather than the first? – Caltor Jan 05 '17 at 18:24
  • 1
    yes, the second one is to obtain separated commits, one for each modification (where modification is a bunch of files). – Luca Cappa Jan 05 '17 at 18:26
1

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.

mit
  • 11,083
  • 11
  • 50
  • 74
-1

if you want a specific file or dir to add in git use

git add folder/file-name

then commit the staged file using

git commit -m "message" -- folder/file-name
                          /\
                          ||
           space after `--` is necessary
Boken
  • 4,825
  • 10
  • 32
  • 42
Sifat Amin
  • 1,091
  • 6
  • 15