Simply put,
Git is a distributed version control system.
Breaking this down shows the essence of Git:
Control System : Git can be used to track and store content stored in repositories.
Version Control System : A history of all changes made to the content is stored. Also, Git allows multiple users to change content parallelly and keep things clean by separating work into branches
Distributed Version Control System : Machines using a Git repository have a local copy of the repository stored and the repository is stored by remotely on a server by Git as well.
To sum up, Git can be used anywhere content will be changed constantly and where it will be helpful to keep track of changes made so that content can be reverted to how it was at a certain point in its history.
These features along with other features such as branching, to run multiple projects with the same base, parallelly, makes Git a very useful tool for storing and updating code.
Creating a Git repository
A repository is where all your code (or any other content) will live. It can start off as an ordinary folder or folder structure and then be converted into a smart Git repository.
For this example, we will assume that an empty folder called ‘my-code-base’ has been created and a terminal window is opened up and the ‘cd’ command is used to navigate into the folder.
cd my-code-base
git init
The git init
command turns the folder into a local repository.
Staging and Committing
Assuming that a plain text file called ‘dummy-file.txt’ is added to the folder, let’s stage it, and then commit it.
Staging is preparing a file (or a change in a file) to be put into a change called a commit.
Staging is required before every commit, this is how the file 'dummy-file.txt' will be staged.
git add dummy-file.txt
To stage multiple files at once:
git add dummy-file.txt dummy-file2.txt
Or to stage all files into staging area:
git add .
Committing involves registering all files currently in the staging area as a change to the repository, including a commit message describing the changes made.
git commit -m "Dummy data added to the file"
The -m
indicates that the following string wrapped in " "
is a commit message. If this is omitted the user is prompted to enter a commit message after running this command.
Another commit option is -a
which automatically stages all modified files to be committed. However, this will only automatically stage files that have already been committed to the repository. For example, if file1
which has been committed in a previous commit is modified and file2
is a newly added file which has not been committed, the -a
option will only stage and commit file1
because Git is not aware of file2
.
git commit -am “New changes”
Status
Status of files including which files have been modified and what files are in the staging area can be revealed with
git status
Branches
Branches are useful for editing the same codebase parallelly without affecting another developer editing the same code. Also, if a mistake is made when editing, this will be kept separated from other branches and the main branch.
A branch can be created with
git branch test-branch
and switch from the master branch (or current branch) to the created branch with
git checkout test-branch
Also, a list of branches created can be shown with
git branch
Now, new changes can be staged and committed to this branch using the previous commit and stage commands.
Merge
This is used to join a separate branch back into the active branch.
First, checkout into the branch that the other branch needs to be merged into.
git checkout master
Then run
git merge test-branch
and the branch test-branch will be merged into the branch you are currently on.
Managing a remote repository
The repository made so far exists in the local scope, this repository can be stored in remote server that can allow other developers to access and clone the repository.
GitHub is a popular repository hosting service, and for this example the remote repository will be hosted in GitHub.
After logging into a GitHub account, a new repository can be created via the menu on the top-righthand corner.
Next, a name and privacy setting for the repository can be chosen.
After setting up the repository, locate the repository’s URL on its page.
Next, this URL can be used to form the command that points the local repository to the remote repository.
git remote add origin [repository-URL]
Git Push
A push involves sending changes from a local repository to its remote repository.
To push changes, the following command is used
git push -u origin master
This pushes the committed changes in the master branch of the local repository to the master branch of the remote repository.
Git Pull
A pull denotes updating the local repository with latest changes in the remote repository.
Latest code of a certain branch (the master branch, in this example) of a remote repository can be pulled by
git pull origin master
Create a working copy of a repository ( Checkout repository )
A locally stored repository repository can be cloned by running the command:
git clone path/to/local/repo
And, for a repository stored in a remote server:
git clone <<repository-url>>
And that is how Git can be used to make versioning easy and clean and to make collaboration a breeze!
Till next time, happy coding!
Top comments (0)