GIT is a widely used, distributed version control system. GIT documentations are quite descriptive, but one has to go through many related documents to get started. This document intends to get one started with GIT by understanding its basic concepts with a walk through how to start with basic commands.
Repository: A repository contains the history, different versions over time and all different branches and tags. In GIT each copy of the repository is a complete repository.
Branch: A branch is a named index to a commit. Selecting a branch in GIT is commonly referred to as to checkout a branch. If you are working in a certain branch, the creation of a new commit advances this index to the newly created commit. You can create a new branch from an existing one and change the code independently from other branches. The master branch is the default branch, for which a local branch is automatically created when cloning the repository.
Commit: When you commit your changes against a repository, this creates a new commit object in the GIT repository. This commit object uniquely identifies any revision of content from the repository, which can be retrieved later, for example, if you want to see the source code of an older version. Each commit object contains committer details. This makes it possible to identify who made the change (or to find the culprit, who tampered with the source code).
Head: It is a reference pointing to the currently checked out branch. If you switch branches, the HEAD index points to the branch index, which in turn points to a commit. If you check out a specific commit, the HEAD points to this commit directly.
Revisions:These are commit objects, which represent a version of the source code. These are identified by an (Secure Hash Algorithm) SHA-1 hash
Staging Block: It is the place to store changes in the working tree before the commit. It contains a snapshot of the changes in the working tree.
Tag: A tag points to a commit, which uniquely identifies a version of the repository. With a tag, you can have a named index to which you can always revert to. We can revert directly to any index/commit in a repo, but tags make it easier to track or to follow release processes by tagging it against a release. For example: RELEASE-2016.2.2.1. You can add a message with a Tag. Branches are also named indexes though they move when a new commit is created while tags always point to the same commit.
Configure GIT User credentials
# Configure the username and add your full name, which will be used by GIT
git config –global user.name “Pramod Pawar”
# Configure the email address
git config –global user.email “firstname.lastname@example.org”
# Create a directory and switch into it
mkdir Rss-feedcd Rss-feed
# Initialize the GIT repository
# Create an empty file in a new directory
copy nul data.txt
# Create a few files with content
echo hello > data01
echo hello > data02
# Check current status of your repository
# Add changes to staging area
# Run the GIT status command to see the current status
# Commit your file to the local repository
git commit -m “Initial commit”
# Show the GIT log for the change
# Add a remote
git remote add origin email@example.com:tavisca-pramod/Rss-feed
Here RSS-feed is the repository name
# Push the changes in your local repository up to the remote repository you specified, in the previous step
git push -u origin master
That’s it! You have pushed a repository to the GIT server. You can check the same by browsing through your GIT account.
Now consider the case where you already have a GIT repository and you are supposed to make changes or add new files to that repository.
Create a folder structure, “D:\CODEBASE” on your machine and clone the latest revision of your repository (e.g.: RSS-feed) from GIT to your local machine
Here firstname.lastname@example.org:tavisca-pramod/Rss-feed.git is the SSH path of the repository on GIT.
You should now be able to see a folder ‘RSS-feed’<your-project-name> in your D:\CODEBASE folder.
# Browse to the ‘RSS-feed <your-project-name> directory in GIT Bash and check the list of branches available.
git branch –a
You should be able to see a ‘master’ branch in the list.
# Create a new branch where you will add your changes, say ‘headlines’
git branch headlines
You should create a branch for every work-item/feature that you want to add in the project repository. Make your changes in that branch, merge it back with the local master and then push the changes to remote.
# Create a branch to add a new feature
git branch feature-addheadlines
# Check the branches on your local machine
You can see a branch with the name ‘feature-addheadlines’ in the list.
# Move to branch feature-addheadlines
git checkout feature-addheadlines
# Change content of data.txt and add these changes to be committed,
git add data.txt
If you check GIT status, the data.txt file will be listed in green color notifying that it has been added to the list of files to be committed
# Commit the files,
git commit –m “Added new feature AddHeadline”
–m is used to add a message with your commit.
# Move to the local branch, in which you wish to merge your changes,
git checkout master
# Update it to the latest revision,
# Merge your branch with the master branch,
git merge feature-addheadlines
# Check status of master branch
It should show a message like – “Your branch is ahead of ‘origin/develop’ by 1 commit.” This shows that your changes are merged locally and are ready to be pushed to the remote repository.
# Push your changes to the remote master repository,
git push origin master
# Check status of master branch
It will show a message – “nothing to commit, working directory clean”
Verify your changes on the GIT server, and you will see your commit with the specified message.
# Now as this feature branch is merged into the master branch and pushed to remote, you should delete it.
git branch –d feature-addheadlines
Pramod Pawar is a passionate techie at Tavisca Solutions, whose passion for technology enables him to come up with the most creative technology solutions. He pens his technical knowledge via blogs, and loves to explore new facets of technology. You can directly reach Pramod at – email@example.com
For Products and Demo Queries Contact: firstname.lastname@example.org