Version control and it's history
- version control is system that record changes to a file or set of files over time
- history goes back to 1972 where SCCS(Source Code Control System) was develop in Bell labs
- in 1982 RCS (Revision control system) was developed which was the cross platform
- both work or single user only
- in 1986 CVS (Concurrent Version System) was developed with central repository
- Slowly the concept of DVCS(Distributed Version Control system) arise and eventually in 2005 git was created by Linux kernel group.
Need for version Control
- To keep track of the changes to file in a project
- to keep the backup of the project
- for example - if we need to change a file in the project then first we may keep the backup of file line file_v1.txt
- Again we may need to change the file and this time the backup may be file_v2.txt
- so, it seems tedious right? Every time we need to keep the backup before any changes
- so, the version control system comes to rescue.
Git
- Git is version control system
- Used for tracking changes in the source code during program execution
- It helps the programmers/ developer to coordinate among themselves during software development
Installing Git
- In windows:
- https://git-scm.com/downloads
- In Linus:
- sudo apt-get install git
- In MacOs:
- First install homebrew (refer to above link)
- brew install git
git init ===> start
git add ===> Add files to staging area
git add file name
git add . ===> if we want to add all file
git status ===> shows current status
git commit -m "message" ===>
if doing for first time we should config
git config --global user.email "youremail"
git config --global user.name "username"
it will commit now
then we can copy
git remote add origin URL(from new repo)
git push origin master ==> to add remote origin
git log ===> shows history
git log --oneline
git checkout master ===> to goto main branch
git diff ===>to see the changes
Let's Use it
- First create your local repository
- start a terminal and go to your project folder
- enter the command 'git init'
- this will initialize your project as a local repository
- Setup your git configuration:
- git config --global user.name '<your-username>'
- git config --global user.email '<your-email>'
- In case you want to setup different username and email for different project you can commit '--global' flag.
- Now in this local repository you can use all the git commands
Creating SSH-Key (Secure Shell)
- open your terminal and enter
- ssh-keygen -t rsa -b 4096 -C 'your email@ex.com'
- Set the folder to save you ssh-key (default would be better)
- The default location of ssh-key is /home/user/.ssh/ where there would be two files id_rsa and id_rsa.pub
Here are some basic git commands
- git init => initializes a new local repository
- git clone <project-url> => makes a clone of the entire project
- git status => checks the status of files you've changed in your working directory
- git add . => adding the changes allows the git to keep track of the files
- git commit -m '<commit-message>' => commits your changes and sets it to new commit object for your remote
- git push origin <branch-name> => push your changes to the remote
- git pull => pull the changes from the remote
- git branch => list out all the branches
- git merge <branch-name> =>merge the '<branch-name>' branch with the current working branch
- git stash => saves the changes that you don't want to commit immediately
- git stash apply => brings your saved changes back
- git checkout <branch-name> => jumps to the specified branch leaving the current branch
- git checkout -b <branch-name> == >create the new branch and jumps to it leaving the current branch
- git reset <mode> <commit> => set you index to the latest commit that you want to work on with
- git diff <branch-name> => shows the changes between the current branch and the <branch-name> branch
Introduction to Github
- Github is a git repository hosting service
- Git is a command line tool, but github is the place to host the local repository
- Unlike git , it provides the web-based graphical interface for source code management
- Also provides other features like wikis and the task management tools
There are other web-based git technologies like github. Here are listed few:
- GitLab
- Bitbucket
- SourceForge
- BeanStalk etc
Adding you SSH Key
- login you github account
- after ssh-key is created open your id_rasa.pub in any text editor (you can 'cat id_rsa.pub' in the linux terminal) copy that key
- go to the setting of your github account and there you can click the 'SSH and GPG keys' options
- Now paste your copies key in the key box and 'Add ssh Key'
- Remember that the key must be the public(id_rsa.pub)
To create repo
- git init in the project folder
- git remote add origin 'ssh-link-of-remote-repo'
- git add .
- git commit -m 'first commit'
- git push -u origin master
In case you want to clone other git repos
- We can simple enter the command in the terminal
- git clone <ssh-link-of-remote-repo>
- Remember we can only clone the public project or the ones where we have been granted access
Git Branches
- while working in team we work on different branches
- creating a new branch
- git branch <branch-name>
- git checkout <branch-name>
- Note single line command for above two commands
- git checkout -b <branch-name>
- Checking out the new branch will copy you project from the current branch to the new branch
- Now we can separately work in our new branch
- the changes in one branch will not affect the source code of another branch
- so after adding the features(code) in the new branch and performing proper test we can merge it to the stable branch
Merging the branches
- The proper way to merge the branches:
- git checkout <stable-branch-name>
- git pull (it will bring all the changes from the remote to the local branch)
- git merge <newly-added-branch>
- While merging two branches may arise the 'Merge-conflict'
- so lets discuss on merge conflict
Merge Conflicts
- While merging two branches, if the same line of code was previously committed by one user and now while merging, the line has been changed by another user then the case of merge conflicts arises
- It is the case where the git want the developers to be sure which line of code is need to be kept and which to be removed
- consulting with the team to add the suitable code and avoiding the other code would fix the merge conflicts
- The good practice would be to create a 'Pull Request' rather than directly merging the branches.
Pull Request
- It is the process of observing the changes in the two branches before merging
- the change from a branch is pushed to the remote and before merging it to the stable branch we create pull request(merge request)
- click on 'Pull requests' option in the repo and 'New pull request'
- Select your 'base branch' and the 'compare branch'
- if there are changes to be merged with the base branch then you will get the option 'create pull request'
- Now you can compare the changes in the two branches. If there is any 'merge conflicts' then you will be notified prior to the merging. If everything is fine, then now we can merge our pull request.
- Remember the pull request merge the code in the remote git. To reflect the changes to the local git you need to 'git pull'
Git and Github workflow summary
- Set up your local repo using 'git init'.
- Create a new repo in the github and add its remote ssh link in the local repo using 'git remote add
- Add, commit and push your local repo. (git add ., git commit —m commit-message>', git push origin master)
- While working in team, make one of your branches a stable branch (master).
- Checkout other branches from the master and start adding new features in the different branches.
- After completion, test the features in the new branches and merge the master branch to this branch using command 'git merge master'
- Push the changes to the remote branch using 'git push origin <branch-name>'
- Create a pull request with base branch 'master' and compare branch
- Merge the pull request.
- To reflect the changes in the local repo, checkout to the stable branch (master) and enter command 'git pull'.
Comments
Post a Comment