Git is a version control system, that is used for source code management in software development. A version control, also referred to as source control, is the practice of tracking and managing changes to software code over time in the software development process. Version control helps to track any changes made in the code and in a special kind of database. Any mistake that is made, developers can turn back the clock and compare ealier versions of the code to help fix the mistake while at the same time minimizing disruption to the other developers.
There are other types of version control in existence include:
In this blog, my focus will be on Git and Github as a control version.
Before you begin to use Git, first check whether is installed in your machine. To check if Git is installed in your machine type the command git in your command prompt. If you see git usage screen then it is already installed.
If Git is not installed in your local machine, follow this link to the official website and download Git for your respective operating system Download Gitand install it.
To check if Git has been installed and its version, type the command git --version as shown in the below.
Git comes with a tool called git config that lets you get and set configuration variables such as (user email address and user name) that control all aspects of how Git looks and operates. The git config command is used to set Git configuration values on a global or local project.
Run the command below to configure your email address as follows.
To configure your user name, type the commands shown below.
If any error is made when configuring either the user name or the email address, you can rewrite the command with the correct user name or email address. The previously written name or email address will be overwritten with the correct one.
Notably, you can check the configured name or email address using the commands as follows:
Checking email address
A Git repository is a central storage location for managing and tracking changes in files and directories. Itis a crucial component of Git verson control which enable collaborative development and facilitating efficient and controlled code management. A Git repository stores all the versions of files within a project, enabling developers to track changes, collaborate, and easily revert to previous versions if needed.
Steps on how to initialize a git repository.
Create a folder in your local disk for storing project files.
Open the command prompt from the folder.
Check if the folder is already inintialized for git. Type the command git status in the command prompt as shown below.
If it show the message No commits yet, initialize enter the command git init to initialize the folder as a git repository. All the files involved in the project should be stored in this folder for tracking changes and errors made.
What is a staging area?
Git staging area is an intermediate are that stores changes to tracked files before they are committed to the repository. You can add or remove files from the staging area to control which changes are included in the next commit.
You an add or remove files to the staging area to determine which files you want to push to the repository.
You can add a single file at an instance to the staging area using the command git add filename. as demostrated below.
To add more the one file to the staging area at an instance, using the dot (.) operator in the add command as follows. git add .
Subsequently, you can remove files from the staging area using the git rm --cached filepath
For example, to remove the file textFile1.txt as shown below.
The command git commit is used to save all staged changes, along with a brief descripiton from the user, in a commit to the local repository.
The -m option stands for a message. When calling the git commit command it is crucial to include a description of the changes being committed. The message should be wrapped in quotations (" ").
Tracking commit history
The --amend option allows you to change your last commit message. Example below.
You can revert back to a previous commit message using the git revert commitID. This command figures out how to reverse the changes created by that specific commit, then append a new commit containing the new inversed content.
In Git, a branch is a new separate versionof the main repository. Branches allow working on different parts of the project, such as fixing a specific error in a file. without interfering with the main branch. When the work on the newly created branch is complete, the branch is merged with the main project to implement the changes made.
Branching means you diverge from the main line of development and continue to do work without messing with that main line.
Create a new branch using the command git branch branchName. Example.
To view all the branches locally in repository, run the command git branch. The branch that will have an asterick and fairly a different color from the rest indicated the current branch. Example
To see remote branches. Run command git branch -r
To see branches both locally and remote, run the command git branch -a
Run the command git checkout branchName. This command is used to move us from the current branch to the specified branch as shown below.
Basically, Git merge command will help to combine the changes from two or more branches into a single branch.
Note: First, you must check out the branch that you want to merge another branch into (changes will be merged into this branch).
To merge, run command git merge branchName
To delete a branch, run the command git branch -D branchName.. Example
To add on, you can delete multiple branches using the same command, and include the branch names to delete separating with a space.
I higly appreciate you for the dedication of your time to read through my blog. I hope it was informative and of help to you. If there is a concept that I might have left out, or you need some more clarification on a concept, I would recommend you to visit Git documentation for more guidance. I would also recommend you to interact with other control version I had listed above to have some deeper insights and understanding on version control and the tools used to achieve this.
Thank you.
Thank you for reading through.