Getting to the core, Git is a free and open source Version control system. It helps to handle small to large projects with speed and efficiency.It has many features like cloning,local branching, merging several contributions, giving pull requests etc. It is really a space to store content. Apart from the remote repository one has in git, it also provides a local repository in one’s computer on which a person can work on. It is generally a distributed VCS as the changes made in the code or project occurs in computers all the developers working on it.
SETTING UP GIT
So let’s get started with installation of Git.It can be easily installed usig the following lines of command.
- For Ubuntu (debian based OS), use:
$ sudo apt install git-all
- For Windows:Check this site: https://git-for-windows.github.io/.
- For macOS:
Checkout this site: http://git-scm.com/download/mac.
Now you can check the version of git through your terminal, using the command:
Now its all set to make through to the Git world.
CREATING A REPOSITORY
After getting Git installed the main task is to create a repository and get initialized to it. Git repository is a virtual storage space which you can access at any later time.You can give a description for the repo and also the programming language for the projects and codes that would be pushed into thst repo. Or you can clone an existing repository from elsewhere.
For creating a new repository, follow the steps below:
- Either create a new folder of choose any of the local directories in your computer.
- Get into that directory using:
- Initialiseinto that directory using:
For cloning into any existing repository, follow the steps below:
- Fork the repository that you wanted to clone.(This creates a copy of the repository that you wanted to work on).
- Clone the repo using the the url given using the command:
git clone <repo url>
- Get into the repository that was cloned using the command:
Next, you can work in the cloned or created repository and make changes or new projects in it.
PUSHING A FILE INTO GIT
After making required changes in the local repository, these changes has to be updated into the remote repository, which can be done by the process of pushing the code into the repository.This includes several steps like:
- Staging- Adding the committed changes and to keep track of all changes.
git add example.txt
For adding mutiple files do the following:
git add file1 file2 file3
- Commiting- Commit is a change done to a local repository. It should be given a commit message signifying the commit done.
git commit -m "Commit Message"
- Pushing- This pushes all the changes made in the local repository into the remote repository.
It is done as follows:
git push origin master
BRANCHES AND REMOTES
A branch serve as an independent abstraction for git operations like edit/commit/stage etc.The git branch command is used to create, rename, or delete branches.The main branch for a repository is mostly master branch or development branch. We can create several branches from this branches and work on it.Some commands related to branches are:
git branch\\lists all branches in the repository.
git checkout -b <branchname>\\Checkouts the current branch and switches to the new branch.
git branch -D <branch>\\deletes the branch
git remote -v\\lists all the remote present
They are the features that provide a user-friendly interface for viewing proposed changes before integrating them into the main project.When you work on a different repository in Git and make certain changes in it, you have to let the collaborator know about the changes so as to let them merge your changes into the original repository. When the pull requests are open we can compare the changes between your branch and the remote branch and if there are no conflicts, you can send the PR. This is helpful well many developers are working on a single project parallely . While creating a PR, you can create a summary of the proposed changes.Once the pull request is created, the committed file undergoes certain checks which sees for anything which harms the already existing codes.
Once you push a code into your repository, go to the repository page and and click on ‘Create new pull request’. Pick up the branch to which you have to push the request and the branch from which you want to push. Add a title and a description for the PR. Then click on Create pull request which makes the process done.
Rebasing is the process of combining a sequence of commits to a new commit. This is used mainly so as to make a branch even with the master branch of the original repository. This can be explained taking a situation where the master branch had progressed certain commits forward compared to the feature branch by the time you are working on it. This makes the feature branch to be certain commits behinf the master branch. Git rebase can be used so as to seem that you have been working from the branch that have been even with the master branch. Hence there are 2 ways of merging, either by direct merging which would lead to conflicts or by rebasing and then merging which would make the branch look clean. Hence rebasing helps to integrate the upstream changes with the local repository.
The steps of rebasing are as follows:
- Checkout the feature branch:
$ git checkout <branch>
- Rebase the feature branch to the mater branch
git rebase master
It is the method of consolidating multiple commits made by a branch into a single commit before pushing into git.It is a manner of picking several commits and squashing them into one.The steps of squashing are as follows:
- Rebase the feature branch as this shows all the commits done in that branch.
git rebase -i master
- This shows something like:
pick fb554f5 This is commit 1 pick 2bd1903 This is commit 2 pick d987ebf This is commit 3
- Edit all the “pick” tags except the first one to “squash”.
- This looks something like:
pick fb554f5 This is commit 1 squash 2bd1903 This is commit 2 squash d987ebf This is commit 3
- Save the editor give a commit message and close it.
- Force push this into the feature branch.
git push -f origin <featureBranch>
This would squash all the previous multiple commits into one.
It is a complex command of undoing certain local changes like undoing addition of some files etc.
$ git add abc.java //will add a file named abc
$ git reset abc.java //will remove the added filed named abc.
There are 4 ways of using reset command. They are:
- –hard: It rsests the whole commit. By using this all the local changes gets removed.Hence be very careful using this as it would probably clear all the local changes made so far.
git reset --hard
- –mixed: This is the default one. It rests the staged files,but the working tree is not affected. Any difference between original commit and reset ones will be shown as untracked files.
git reset --mixed
- –soft: It does not affect the added files or the working tree. It is almost similar with –medium except that the changes show as the changes to be committed.
git reset --soft
- –merge: It is the recently added one. It helps to abort a failed merge.It resets the added files and the files affected by the merge.
git reset --merge
This is a command where your uncommitted changes are stored in some other space and can be reverted back to the branch. In this all changes whether staged or unstaged will be saved, unless it is committed.This helps in freely making other changes and commits and apply the stashed changes when required.This is also useful when you have to apply same changes for different branches.
The steps of stashing are as follows:
- Check status to confirm that the changes are not committed.
$ git status
- Stash the changes.
$ git stash
- Pop the stash to reapply the changes to the current branch.
$ git stash pop
Inorder to give a proper idea of the stash we can make it descriptive by using the following command:
git stash save "description"
These steps would help to stash the changes and work with ease.
The above are the important terms that one may use in Git. All these would help to collaborate projects integrating the contributions from around the world.