Git is a version control system (SCR) that allows you to track changes in a project over time. Git records all the changes that you made in the project, stores information about these changes and allows you to use this information.
From this article, you will learn about the standard workflow using Git, as well as the basic commands in Git for daily use.
Standard Workflow with Git
The standard workflow using Git looks like this:
- After creating the Git Repository, all the work is done in the Working Directory
- When your work reaches an important mark (for example, you fixed a bug or ended the workday), you add your changes to the Index (the so-called ‘prepared files area’ or ‘staging area’)
- Once the index contains all the changes you intend to save, you make a commit that moves files from the index to the Git Repository for permanent storage.
The project in Git can be considered as consisting of 3 parts:
|Working Directory||Where you do all the work: create, edit, delete and organize files|
|Index||Where you mark what changes you made in the working directory, you should save|
|Git Repository||Where Git permanently stores these changes in the form of different versions of your project|
Create New Git Repository
Open the folder with the existing project or create a new folder and run the following command inside it to create a new Git repository:
$ git init
This command creates a sub-directory
.gitin the project root that contains the metadata required for Git to work.
Check Git Status Repository
Check the current state of the Git repository:
$ git status
This command is the main tool that helps to determine which files are in what state (which are already in the index and which are not added and therefore are not tracked).
What, in fact, means the status of “Untracked”? Not traceable – means that Git sees the file, but has not yet started to track any changes in it. To Git began to track changes, it is necessary that the file was added to the index.
Add File to Index in Git
Run the following command so that Git begins to track changes to the file (added to the index):
$ git add <filename>
To add all the files to the index, run:
$ git add.
Show Changes in Git
When the state of the files is tracked, we can view the changes between the working directory and the index:
To show the changes, run:
$ git diff
To see how a specific file was modified, follow these steps:
$ git diff <filename>
Save Changes to Git
A commit is the final step in a workflow using Git .
Save all changes from the index:
$ git commit -m "Short description"
Only commit a specific file:
$ git commit -m "Short Description" <filename>
The standard rules for compiling a description of a commit in Git are:
- Must be quoted
- Must be written in the present tense
- Must be short (not more than 50 characters)
View Commit History
Show the history of commits made in the Git repository:
$ git log
Basic Commands in Git
Summarizing the above, here are the basic commands in Git for daily use :
|git init||Create a new Git repository|
|git status||Show the status of the contents of the working directory and index|
|git add||Add files from the working directory to the index|
|git diff||Show changes between working directory and index|
|git commit||Permanently save all changes from the index to the repository|
|git log||Show a list of all previous commits|
Join our reader's list to start receiving latest updates.
Get free Tech, Linux tips and resources delivered directly to your inbox.