Git is one of the popular implementations of the version control system. There are a huge number of software projects that depend on Git for version control, including commercial projects and open source.
As the architecture of Git is quite distributed so it is a good example of a Distributed Version Control System(DVCS). Every developer that is working on code in Git is actually a copy of code which is known as a repository that contains all the changes made in previous times.
But now the question arises what is this Version Control System and Distributed Version Control System(DVCS)?
Version Control System
The name version control system itself means to create and manage different versions of the file system. By versions, we mean the snapshots of the current state of the file. These versions are stored in a specific place known as a repository.
Distributed Version Control System(DVCS)
In Distributed Version Control System there is a copy of a repository on the computer of each user.This process is known as cloning.Different versions of a file can be exchanged from one repository to another.
Each cloned repository contains the full history of the collection of files and a cloned repository has the same functionality as the original repository.
So Now Let’s Begin With Git…
Firstly Installing Git on Windows
1.Download the latest Git for Windows.
2.Open a Command Prompt (or Git Bash if during installation you elected not to use Git from the Windows Command Prompt).
3.Run the following commands to configure your Git username and email using the following commands. These details will be associated with any commits that you create:
git config --global user.name "your username" git config --global user.email "[email protected]"
Setting Up A Repository
A Git Repository is a virtual storage of your project. It allows you to save different versions of your code, which you can access when needed.
1.Initializing a new git repository
The command “git init” is a one-time command that we use during the initial setup of the new repository.This command will create a new subdirectory in your current working directory. This will also create a new master branch.
2.Versioning an existing project with a new git repository
cd /path/to/your/existing/code git init <project directory>
3.Cloning an existing repository
If a project has already been set up in a central repository, the clone command is the most common way for users to obtain a local development clone. The command “git clone” is used to create a copy or clone of remote repositories.
git clone <repository url>
Use of Branches
Git allows us to create branches.We can work on different branches independently from each other. The default branch is most often called master.
1.Listing available branches
The command “git branch” gives the list of branches available in the current repository.
2.Creating and switching to a new branch at the same time
git checkout -b newbranchname
3.Rename a branch
git branch -m [old_name] [new_name]
See current status of your repository
The command “git status” shows the status of the working tree, i.e. which files have changed, which are staged and which are not part of the staging area. It also shows which files have conflicts and gives an indication of what the user can do with these changes, e.g: add them to the staging area or remove them, etc.
Saving changes to the Repository
git add and git commit
“git add” command is used to add the changes to the staging area.Then after that only you can commit the changes made in your code.
cd /path/to/project git add -A git commit -m "added yourfilename.txt to the repo"
The “git push” command is used to push commits made on your local branch to the remote repository. This allows the changes you made in your code can be visible to the others as well.
Push changes of a branch to a remote repository
# push current branch to a branch called "sample" to remote repository git push origin sample # switch to the sample branch git checkout sample # some changes git commit -a -m "new feature in branch" # push current HEAD to origin git push # make new branch git branch newbranch # some changes git commit -a -m "a new commit in a feature branch" # push newbranch to the master in the origin git push origin newbranch:master # get the changes into your local master git checkout master git pull
This way you can decide which branches you want to push to other repositories and which should be local branches
Merging of commits
The “git merge” command performs a merge operation. You can merge changes from one branch to the current active one using the following command.
# syntax: git merge <branch-name> # merges into your currently checked out branch git merge branchname
So, that’s it for now.Please note that i have tried to give just a brief introduction to git. It would help you for getting started with it.
Once you are done with it and are clear with the basics you can dive deeper into it and learn other commands from the resources available with you to get a good hand on it.
As git is one of the things that comes with practicing, so try to implement git with your projects which will provide you a better understanding and much clearer concepts.