Git: Status Stage Commit

This is the first article in my series "Can You Dig git?!. If you are new to git and are trying to understand the work flow, then you have come to the right place. In this article we will create a new git repository and master the primary work flow. Note this is the first bite sized piece and will not address branching or merging, those will be addressed in the next articles. Here we will discuss status, stage, and commit. With these commands you will be able to create a git repository, check its status, stage files and commit them for future recovery. Lets git started!

Status, stage(add), and commit are the primary work flow actions for using git to manage your code. After you create your project folder and initialize it with git init, The first thing to do is check the status of your project to see if all is working as expected.

1. git status checks the status of your project and tells you if you have any untracked, uncommitted files or "nothing to commit, working tree clean". After we create some content(index.html in this tutorial) we will check the status again to see that git sees the file has been modified and requires staging.

2. git add stages your files. It tells git to record the current staged files in the next commit.

3. git commit records all the staged files into a snapshot, which will allow you to recover them should they be corrupted or accidentally deleted.

References:
www.w3schools.com/git/
git-scm.com/docs/gittutorial
Can You Dig git?

git init

git init initializes the git project. We will first create a folder and then initialize it using git init. For more details regarding git init see the references above.


$mkdir newgit
$ cd newgit
$ ls
$ git init
Initialized empty Git repository in /home/fil/newgit/.git/
$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)


git status

git status gets the status of your project on the current branch(we will address branching in another article). When you first initialize an empty folder you will get the following response and also when there is nothing to add or to commit.


$ git status
On branch master
nothing to commit, working tree clean

Now if you edit one of your files(index.html in this example) you will see the following message: modified: index.html in red emphasizing that it needs to be added.

$ git status
On branch master
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:   index.html

no changes added to commit (use "git add" and/or "git commit -a")

git add

Now we will stage the file with git add and then use git status to see the changes. The text modified: index.html will be in green indicating it is staged and ready to commit. If you have multiple files to stage you can use git add --all or git add -A for short to add all untracked files.

$ git add index.html

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   index.html

git commit

Now we will commit the file with git commit -m "message describing changes". The -m allows you to add a message describing what ever changes happened in the files of the commit. After we commit we follow with another "git status" to check all is well and nothing is left to commit.


$ git commit -m "updated index.html status, stage, commit"
[master 3266446] updated index.html status, stage, commit
 1 file changed, 7 insertions(+)

$ git status
On branch master
nothing to commit, working tree clean


And so we've completed a full cycle in the primary git work flow. Status, add, commit and back again to status. You may have noticed that the flow was more like status, add, status, commit, status. Hope this tutorial helps simplify understanding git and will help make it a powerful tool in your development toolkit. Cheers.


References:
www.w3schools.com/git/
git-scm.com/docs/gittutorial
Can You Dig git?