What is Git?
- Content tracker — most likely used to store code, but can be used to store other files as well
- Version control system — different developers can add code in parallel. It maintains a history of changes.
Why Git is needed?
In real life, it is almost always certain that there are multiple developers working on the same project, therefore a version control system is required to ensure there are no conflicts between the code written by different developers.
In addition, changes in projects do occur frequently, a version control system allows the developers to roll back to previous version easily.
How does Git work?
Installing Git
- Go to https://git-scm.com/download to download Git.
- Choose the default settings.
How to use Git?
Pushing content to the remote repository
Open Git Bash at the project directory
Initialise local git repository by typing
git init
Add your name and email to Git
Add my project file to the local Git repository
You can check what is in the staging area using the status command
If you want to untrack any files, you can do so by using the following command:
git rm --cached filename
Until now, my file has not been pushed to the local repository yet because it is still in the staging area. The change needs to be committed by using the following command:
git commit
A vim editor will pop up. At this stage, you will not be able to type anything on the editor, pressing ‘i’ will allow you to go into edit mode. You must enter a commit message to commit the change on the local repository. After you finish, press esc and type :wq to commit the change.
So far, we have only worked on the local repository only. There are many remote repositories available. I am using Github for demonstration.
Sign in to Github and create a new repository.
Paste these two lines of command into Git bash, this will push the content to Github.
The file is now under your designated repository. You can retrieve previous versions by clicking the highlighted area.
That’s all for this post, thanks for reading.