What is Git?
Git is a distributed revision control system that allows a team of developers to work together, when all developers are using the same code, and it also helps the team of developers to cope with confusion that tends to happen when multiple developers are editing the same code.
At first, a new user starts to develop code; he first downloads files to the local directory and other developers have their own changes in their local directories. GitHub makes it easier for the developer to modify and commit the code online in a very organized way. Git stores everything as BLOB and every commit has its own SHA key. It also helps developers to fetch the branch as they needed in an easy way.
We can also get the other GitHub users’ code to our local repository, and work on them. So, using this tool doesn’t cause any data loss, because GitHub maintains a snapshot of every commit, and we have the ability to rollback any changes at any time
GitHub is used to host the code on web in a safe manner, and it is used by developers to fix the bug and develop new features in any platform. GitHub is a free or private code hosting system. Most of the open source developments are hosted on GitHub, and users can download the code from the web.
Each developer has their own branches for development and bug fixing. After fixing bugs or developing new features in projects, the branches are merged as a single branch and ready to be deployed to the production using that branch. It also helps developers patch their code in an organized way.
There are many version control systems currently used by the developers to organize their code in the web, like SVN, Git, Mercurial by IBM, CVS and Perforce.
Creating a Github.com account makes it easier to host the code Online. The core of the Git was originally written in C programming language, but Git has also been implemented in other languages like Java, Ruby and Python.
- Using the Git, we can to track the changes made by the team as well as new files added.
- Find out who made the changes in the team and when there were committed.
- Deploy changes to production, using GitHub by commands.
- Rollback the code, when needed.
Setting up Git in command line:
First install the Git, using apt-get install git-core on a Linux machine.
sudo port install git-core is used to install the git on a Mac machine.
Next, we need to configure your (user) name using the following command:
git config –global user.name “User name”
Also, use the following command to associate your email address, as it will help you to associate your commits to your GitHub account.
git config –global user.email “User Email”
Creating GitHub account, Organization and Online Repository:
The user needs to first create an account from here. After creating the GitHub account, the user needs to create an organization to maintain the project repositories. Then, the user needs to create a repository name (Gitsamplerepository). After creating the repository, the default branch becomes a Master, and the user needs to add files to it and commit the changes with a push operation. A single organization can have multiple repositories and a single repository can have multiple branches.
Creating a Local Repository:
After creating the online repository, we need to create local repository to clone our remote repository using git clone repositoryurl. Before doing that, we need to create a directory in the local machine using mkdir GitDemo , andthen cd GitDemo to change the directory to the GitDemo Directory.
Now, we need to initialize the repository to local using git init command; otherwise, we can’t use any git command.
A repository is simply a place where the history of your work is stored. It often lives in a .git subdirectory of your working copy – a copy of the most recent state of the files you’re working on.
Version Control System:
In a typical multi-developer environment here are how things will work:
- Every developer creates their own branch for fixing bugs and development.
- Every release will have an Integration branch also.
- Once the code is developed or bug is fixed, developer moves the changes to the integration branch and build will be made from the integration branch and application will be tested.
- Once the System is tested and verified, the changes in integration branch will be sent to the main branch.
- All the change in the main branch will be sent to the production.
Specific Git commands:
There are lot of git commands; however, the basic commands are mostly used by the GitHub users as below:
Initializes a new Git repository. Only after running this command, the folder becomes a Git repository or Git folder, and also the terminal accepts any Git commands.
This is used to set up the git account with local.
Git help command is used to understand git commands. Users can also use this command to understand a specific command, like git help merge.
This command is used to add the newly created or modified files to staged area. Staged area is the place where the files are grouped before they are committed.
This is the most important command in git and it commits the files to git repository. After the commit, the file is added to snapshot of repository.
The local changes will be moved to the remote branches using the git push command.
Git merge command merges two different branches into one branch. For example developer A changes his own commits with his branch and developer B changes his own commits with his branch. The merge command can merge both developer’s code to a single branch.
Git checkout command allows you to navigate between branches.
Git branch command is used to create a new remote branch.
Fetches the code from remote branches and merge them with local branches
Git stash is used to keep the changes made by developers in the branch aside for future use, allow to checkout another branch and do modifications on that branch, and checkout back to same branch.
Using GitHub in command line
Git with command line
To initialize Git repository, we need to use the command git init. It creates a .git directory and contains all necessary files in it to maintain the projects.
Next, type git status to see the current status of the project.
Now, you can developer code from your local machine and host the code in GitHub using the following commands:
For example, the repository name is MSTSolutions and the project name is MyProject. First, we need to create the MSTSolutions repository, create MyProject branch, and host the local code to that MyProject branch using git add command.
Now, clone an existing repository using the git clone repostoryurl (git clone firstname.lastname@example.org:mstsolutions/TestRepository.git)
git clone email@example.com:keybo/TestRepository.git directoryname
We also need to specify the target directory name to store the remote copy into our local directory using above command.
Add files to the stage using git add command. Multiple files names are separated by a space.
Git add command used to add the local files to staged area.
“Git add -A .” is used to add all files including deleted files to staged area. (Here, the dot indicates the current directory).
Git stash is used to keep the changes made by developers in the branch aside for future use, allow to checkout another branch, and do modifications on that branch and checkout back to same branch.
Developer commits changes to a branch and needs to know the code differences between the working directory and most recent commit. Here, we can use git diff HEAD.
Another great use of diff is used to compare the files that are staged. git diff –stage will show all differences in the stage like new change, modifications on the code, and everything related to the changes of on the stage.
To see the differences between two commits, use the following commands with its corresponding SHA code.
git diff –name-only SHA1,SHA2
If we don’t know SHA, we can use the following command to see the differences between two corresponding commits.
git diff –name-only HEAD~10 HEAD~5 which shows differences between 10th and 5th commit.
The place where the files are grouped together before the files are committed to Git. There are four steps in staging the files.
Files ready to be committed to the branch are called staged files.
Files in the local folder but not ready to be committed.
Files are new to the git and not tracked yet by the git.
Files are ready to be deleted from the Git.
We also use the wildcards symbol to add all files in git using the file extension like, git add “*.txt”.
After the files are added to the staged area, every time we need to commit the changes using
git commit -m “the message that you want add about the changes”.
We can also use the git log to view all commits that we have made to GitHub. Using this command, we can see all deleted and added files in chronological order. (git log –summary)
While we are working as team to develop or fix bugs in a project, we need to create a separate branch for every developer working on that project.
To create new branch, use git branch branchname.
To delete a branch, use git branch -D branchname.
If we want to clean up a particular directory, use the recursive command and specify the folder to delete: git rm -r foldertodelete.
After creating branches, we need to switch back to the newly created branch to modify the code using git checkout branchname.
git checkout sampleproject
Merging the Branches:
To merge a developer branch with a master branch, we need to first checkout master branch and use merge command.
git merge sampleproject
Fetch will get any changes from a remote branch, updating your repository data, but it will not change any of our local data.
git fetch – fetches all remote branches to local
Pull will perform a fetch and additionally merge the changes into the local branch.
git rebasing is a another technique for merging.
Clean Untracked directories from Git:
When we push the code from local to Git, there will be a lot of untracked files that can’t be pushed. So, we have to clean all the directories and files from the local branch and then push the code to remote branch by using this command git clean -d -fx.
GitHub is an effective online version control system that allows developers manage their code and versions in the most collaborative way.