learner
learner

Reputation: 1007

Changes not staged for commit - What are the 3-states in Git? How to use them?

I am working on the main branch updating some files but not the ones on the list below, however whenever I run git status I get the message below with the files highlighted in red which suggests there might be an issue somewhere, I would be happy if someone can please explain what is going on and what I can do to resolve the issue/these messages.

It continues to come up.

On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   repo/source/file1.txt
        modified:   repo/source/file2.txt
        modified:   repo/file3.txt
        modified:   repo/file4.txt
        modified:   repo/file5.txt

Upvotes: -2

Views: 1799

Answers (2)

CodeWizard
CodeWizard

Reputation: 142632

You are facing one of the most un-understood feature for git if you are used to work with other source controls.

It will be a long explanation but it will clear things out for you. (I hope)

To understand it deeply you need to get familiar with 3-states

3-states

enter image description here


What is it?

In general when you are using git, git you are working locally.
In most other SCM (Source Control Management) you simply write code locally and then "upload" it to the server, the "server" calculated diff, do merges and more.

In Git you have 3-states.
We can write all day long what and how to use it but to make it short and focus on the answer what you need to know is that git has the following states (to be accurate in real life there a 5 actually, including remote & stash) but let's focus on the 3- states

Working directory

  • This is your file system where you write your code, modified files, build your project. The initial stage of the files is but untracked which actually mean that git not yet track changes to your files (untraced)

  • Not all your files should be tracked in git, for example logs, dependencies, and build output etc should not be managed with git.

  • Once you "know" which files you wish to manage you need to "move" them to the second stage.
    The "move" action is git is git add which actually tells git that you are going to track changes to the file from now on

  • Once you add file to the staging are git will track changes for you from this point on.

Note This is why you see in your git status untracked files, since you have them in your working directory but not in your staging area.

Staging Area

  • aka: index, cache, stage

  • Once you git add files you are adding a "copy" of those files to your staging area which "tells" git tha those files are candidates for the coming commit.

  • If you will edit any file that you added to the staging area, you will have 2 different versions* of the files, one (edited) in your Working directory, and second (the one without the extra changes) in your staging area again

  • File statuses

enter image description here

Repository

  • As mentioned above using git you are working locally and you "upload" (git push) your local changes to the remote server.
  • When you are ready to commit, meaning you reviewed the desired changes with git status

enter image description here

  • If you are happy with the list of files you can now commit your changes to git.
  • Commit means that git will take a snapshot of all your files in the staging area and will set them a commit.
  • In other words git now have a full file system which you can use at any given moment
  • When you commit (git commit) you need to supply a description to the commit known as message

enter image description here

Upvotes: 3

Schwern
Schwern

Reputation: 165576

They are your Changes not staged for commit. Those files have been changed, but you haven't told Git that you want to commit them. Git explains what to do:

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)

Git uses a two stage process to making a commit. First you "stage" your changes with git add. This tells Git to "track" the file, pay attention to it when it changes, and it also copies it into the "staging area" which will become the next commit. This allows you to build up a commit in pieces, and you don't have to commit everything you've changed.

Then git commit commits what is staged.

You can use git commit -a to commit all staged and unstaged changes.

git diff will show your unstaged changes.

If you didn't mean to make those changes, use git restore to restore the file to how it was in the previous commit.

See Git Basics - Recording Changes to the Repository.

Upvotes: 4

Related Questions