Git Status, Add, Commit and Log

Git Status, Add, Commit and Log

After git initialization, we reach the stage where we really start using git. The git add command is used to add changes to the next commit in Git. It is typically the first step in the process of committing changes to a Git repository. 
General steps involved in using git for file version control:
  1.  Make some changes to a file in your Git repository, for example, "example.txt"
  2. Run the command git status to see the status of the files in the repository. You should see "example.txt" listed as "modified"
  3. Run the command git add example.txt to stage the changes you made to "example.txt" for the next commit.
  4. Run the command git status again. You should now see "example.txt" listed as "Changes to be committed"
  5. Run the command git commit -m "update example file" to save the changes to the repository.
Here are the details with an example:

Git Status:
The git status command is a useful tool in Git that allows you to see the current status of your repository. When you run this command, it will display information about which files in your repository have been modified, which files have been added but not yet committed, and which files are being tracked by Git. Here, I used git status after modifying file.

git status



Above shows that file is in modified state. moved from unmodified after `git init` changed from untracked to unmodified.

Git Add:
The git add command is used to stage changes in a Git repository, preparing them to be committed. It tells Git to track changes made to files, so that they can be included in the next commit. This command is usually the first step in committing changes to the repository, it is used to select which modifications should be part of the next commit. It helps organizing and grouping changes, before they are committed. Here, I used git add in two ways, one by writing specific file name and another using `--a` which select all files in that repository.

git add



Above the file has changed from modified to the staging area by using `git add filename` or `git add --a`. 

Git Commit:
The git commit command is used to save changes to a Git repository. It records changes made to files and directories, and creates a new snapshot of the repository. The git commit command is used to create a new commit, which is a point in time snapshot of all the files in the repository, this command is usually the last step in committing changes. It allows you to provide a message summarizing the changes made, this message is useful to understand the context of the changes when revisiting the commit later on. The git commit command makes the changes made to the files permanent and allows you to navigate, revert and compare different versions of your code. Here, I used `git commit -m "message" ` to write git commit without any diaglouge popup and if I write only `git commit ` a dialogue pops up as shown below:

git commit

git commit



note in editor:
press I : then only you can write commit after you write press esc
press wq: before wq press `:` ie. ` :wq ` for save and exit the editor/popup

` git commit --amend `: use this to change the recent commit names or message as you like

Git Log:
The git log command allows you to view the history of commits in a Git repository. It displays a list of all commits in chronological order, including the commit message, author, and date. It gives you a detailed overview of the repository's history, it's useful for understanding how the codebase has evolved over time, who made the changes and when. The git log command can be used to find a specific commit, check the history of a file, or check the merge history. It is a powerful command that allows you to understand the context of the changes made to the codebase. here I used git log to check all the commits we made:

git log


there are various git log short forms and use cases:
` git log ` : simply prints full git log {all commits with full details}
` git log  -3 ` : prints only recent 3 logs, you can use any number you like
` git log  -p ` : shows difference between previous and new commits
` git log --stat ` : shows difference on commits
` git log --pretty=one line ` : shows all commits in single line
` git log --pretty=short ` : shows author and message i.e. shorter form of git log
` git log --since=2.days ` : shows only recent two days you can change number, also day, months, ...


Post a Comment

Previous Post Next Post