[Git] Understanding more about Commit and Staging Area

♠ Posted by NN TRUNG at 18:13
In creating Git repository for past you have mentioned the phrase Staging Area and a feature thatcommit (trustee), so two is what, I'll explain in more detail in this article so you know how to use properly .

What is Staging Area?

Staging Area means an area where it will be prepared for the commit. First of all, you must understand that in the version management system (Version Control System), the data will be stored in two places, one is the directory you're working on a computer (working tree, I do not 😁 reiterated anymore) and one is the source code repository (repository) after you've made ​​changes (eg repository on Github ).
But with Git, it has added an additional option that is going to add an intermediate area called Staging Area and this is a major advantage of Git. Staging Area is the area that will store your changes on the file so that it can commit, because they want to commit any file, that file must reside in the Staging Area. One file in the Staging Area when there will be a status Statged (see below).
The model explains how the Staging Area.
The model explains how the Staging Area.
And to put a file to the Staging Area, you will need to use the command git add tên_file that had examples in the previous section.

Commit is and how it works?

Understand simpler, commit an act meant to Git saved a snapshot (snapshot) of the change in the working directory, and the files and folders have been changed in the Staging Area. Every commit it will be saved history of the source code editing with names and email addresses of people commit. Also in Git you can also restore files in its history to divide commit a branching (branch) is different, this is the crux of the work you will easily restore previous changes they have introduced through this in the introduction to the series.
And of course, the command will commit in Git git commit -m "Lời nhắn" .
And if you want to put the file on the repository, you must commit it before then commandgit push origin master would have put the entire mission was commit files to the repository.

Conditions to commit a file What?

If you want to commit a file, you will need to put that file on the state tracked by the commandgit add tên_file . In git has two main kinds of conditions that is tracked and untracked, specifically:
  • Tracked - A file has been marked track in Git do you work with it. And tracked its status will be adding other side is Unmodified state (without modification), Modified (edited) and Staged (ready to commit).
  • Untracked - As the remaining files that you will not want to work with it in Git.
But you must be aware that if the file has been tracked but are falling into a state (Modified), then it will be that you can not commit to bringing it about Staged also in command git add .

Are Staging skip to commit

As I have said above is a file after changing or creating new ones, it must be added to the Staging Area with git add. However, you can put a file that has been tracked to commit without bringing it into the Staging Area with parameter -a in the command git commit . Example:git commit -a -m "Skipped Staging Are to commit" .

Learn more about the state

Round waiting status of files
Round waiting status of files

Untracked

If you create or add a new file in your working directory, it will remain untracked. Now I try to create a new file named faq.html, then use the command git status to view the status of working Git directory.
  $ Touch faq.html
 $ Git status
 On branch master
 Your branch is up-to-date with 'origin / master'.
 Untracked files:
  (Use "git add <file> ..." to include in what Will Be Committed)

  faq.html

 nothing added to commit but untracked files present (use "git add" to track) 
Note: The command touch to create an empty file.
Now you will see it listed in the file names are untracked state. To put it on TRACKED you use the command git add and review its status.
  $ Git add faq.html
 $ Git status
 On branch master
 Your branch is up-to-date with 'origin / master'.
 Changes to be Committed:
  (Use "git reset HEAD <file> ..." to unstage)

  new file: faq.html 
Now you can see your files faq.html was taken Staged state and it can be commit. Why? Because you have to know that if a state untracked files that are taken, it will be tracked in the state Staged always, unless you change the contents of this file, it will put the state and it can not be Modified commit unless you type the command git add to it.

Tracked

Once a file has been sent back tracked, it will be able to change between three different states areModified, Unmodified and Staged.
First now I have a new file has been put on Staged with the command git add as the example above.Now I proceed to change the contents of this file and see the results faq.html of command git status .
  $ Git status
 On branch master
 Your branch is up-to-date with 'origin / master'.
 Changes to be Committed:
  (Use "git reset HEAD <file> ..." to unstage)

  new file: faq.html

 Changes not staged for commit:
  (Use "git add <file> ..." to update what will be Committed)
  (Use "git checkout - <file> ..." to discard changes in working directory)

  modified: faq.html 
You can see the file exotic faq.html not? That was it shown in two states Staged (possibly commit) and Modified (can not commit), also known as Unstaged. The reason for the strange here is because you previously created files taken tracked faq.html and it also has been taken to commit Staged. But then you re-edit its content so it had a different version is located in the Modified state (can not commit). If you now type git commit to entrust it, the snapshot of the file at last faq.html you type git add up, but it will commit not contain content that you just added. And so it can commit faq.html file has been edited, you must type the command git add faq.html again.

Transfer untracked files from the TRACKED

In Git, you can take a file from the command rm tracked on ten_file untracked. Rm command will help you put the file on untracked state but not delete the hard drive.
  $ Rm faq.html
 $ Git status
 On branch master
 Your branch is up-to-date with 'origin / master'. 
  Changes not staged for commit:
  (Use "git add / rm <file> ..." to update what will be Committed)
  (Use "git checkout - <file> ..." to discard changes in working directory) 
  deleted: faq.html 
And if you want to delete it is always the command git rm -f tên_file and be careful when using this command.

Epilogue

Maybe you'll see what I say in this article is a bit long but it is very basic knowledge of Git you should know well because if you do not understand the life cycle status of a file in Git, it surely follows While doing this you will be confused when typing git commit and kept asking why not commit is a commit is because you will want to put it in proper condition, ie Staged state.

1 nhận xét:

Creating a staging environment always save time and also losing of website old data and structure, so with WordPress staging we can easily work on staging and then move it to live website.

Đăng nhận xét