Git is a distributed version control system (VCS). Simply put it is a system that tracks changes to files in a project, which enabling us to record project changes and go back to previous versions. It is especially useful when working or collaborating on group projects. This is the major reason why programmers around the world use it.
Setting Up Git
The process for setting up Git is relatively simple. You just download the setup file for the latest version of Git for your OS, run the setup and follow the prompt. You can download the official setup from their official website for free https://git-scm.com/downloads
To test check if Git installed successfully on your PC run
If everything went well the version of git will be displayed. If not you can search online the error returned and hopefully you find a solution or you can contact me.
Run the following commands to identify yourself with Git. The purpose of this is to be able to be identified when you make a change; your name and email will the displayed in the git log.
git config --global user.name "Your Name" git config --global user.email "email@example.com"
When working with git there are two types of repositories local and remote repositories.
A local repository is one stored on your system, where you can work on local versions of your project. In order to create a local repository navigate to the folder where you want it located with your preferred terminal, then run the command.
A remote repository is one stored on a remote server. It is good for working with teams and collaborating with others.
In this article I will focus on local repositories to know more about global repositories have a look at my blog post on the topic.
When working with repositories you can stage files this allows the files to be tracked for changes. You can add files individual or all at a time
git add <file-name>
git add .
This is the process of saving all the changes and files to the git repository this can be done with the command
git commit -m "an short explanation here"
Status and log
Git gives us the ability to see which files and being tracked and which are not this can be done with
We also have the ability to check your commit history with the command
The git log command displays the details of each log with who made the change, the message, time and a commit hash which is auto-generated. To go back to a previous commit run the below command
git checkout <commit-hash>
To go back to the latest commit run
git checkout master
To let git know which files you want it to ignore in your project directory create a file called ‘.gitignore’. In this file on each line write the name of the folder or file you want ignored and it would not be staged.
Simply I put a branch is a way of maintaining different versions of a project. Typical the current version is the masters branch and you can create other branches for other versions or beta tests.
You can create a new branch with
git branch <branch-name>
To switch to a different branch you use
git checkout <branch-name>
If you want to create a new branch and switch to it use
git checkout -b <new-branch-name>
To merge a branch is to bring two branches together and update the files in one branch from the other. For example merging master branch with version 2 branch will updated the code in master with version 2. To merge a branch ensure you are in the master branch or the branch you want to update then run
git merge <branch-name>
To Delete a branch use
git branch -d <branch-name>
For further learning have a look at the following links
- Official Git documentation https://git-scm.com/doc
- The free Pro Git book: https://git-scm.com/book/en/v2