Git
There are many version control tools.
1. Git
2. SVN
3. CVS
4. TFS
GIT : Git is a version control system or tool which is used to keep track of
versions of files and directories.
Difference between Git and SVN
Git - Git is a distributed version control system means whole respository will be
there in the local
workspace. If i want to go to the previous versions of code, i can go in the
local workspace itself.
Git has many features like rebase, fetch and stash etc.
SVN - Svn is a centralized version control system, we have only latest version of
code in the local workplace, if i want
to go the previous version of code, then need to checkout that previous
verion from central.
Svn does not have direct commands.
Git installation on linux, centos, Fedora, RHEL
sudo yum install -y update
sudo yum install -y git
Git installation on Ubuntu
sudo apt -get update
sudo apt -get install -y git
* Git commands
1. Setting up username and email globally for all users on specific machine. These
settings are important beacause
git uses them to associate your identity with your commits.
- git config --global user.name "<username>"
- git config --global user.email "<email id>"
2. To list all global settings
- git config --global --list
3. To list all settings (GLobal + repo specific)
- git config --list
4. Create gitrepo directory
- mkdir <reponame>
5. To initialize the repo
- cd repo/
- git init
6. ls -al -> To make sure .git folder is present
7. Create files
- vi <filename> - Add few lines and save it.
8. git status
- To check whether the files are in the local workspace or staging area or in git
repository.
- To make sure working directory is clean.
9. git add <filename> : To add files from workspace to staging area.
- To add multiple files at a time used -> git add .
10. git commit -m "message" - This will move or commit the files from staging area
to git repo.
11. git log - It shows the history of this repo.
12. git log <filename> - It shows the history of particular file.
13. git log -2 - It shows history of latest 2 commits of any file.
14. git log -2 <filename> - It shows the latest 2 commits of a particular file.
* There are 3 stages (git workflow)
1. workspace : It is a directory where we modify the project related files, files
present in workspace
are untracked. If we run git add command, it move files from
workspace to staging area.
2. Staging area/ Index area: Here we save the changes.
Once If i run "git commit" It will get committed to
git repo.
The committed id will get created here.
The files present here are called as indexed files.
3. Local repo/git repo : It is a location where version controlling happens & files
present in here are
called as committed files.
* Git Branch - Branch is for parallel development.
- If two people or two teams work on a same piece of code on two different branches
then later we can
integrate by merging.
1. git branch : It is used to list branches and check on which branch we are
working.
2. git branch <branch-name> : It is used to create new branch.
3. git checkout <branch-name> : It is used to switch to a different branch.
4. git checkout -b <branch-name> : It is used to create new branch and switch to
that branch.
5. git branch -d <branch-name> : It is used to delete the branch on local repo.
Note : Whenever a branch is created the entire commit history of the parent branch
will be copied to the
new branch.
Note : Irrespective of on which branch a file created or modified git only check
from which branch it is committed and
that file belongs to the committed branch only.
6. git log --oneline : To check the commit history in short form.
* Git tag : Tag is a name given to a set of versions of files and directories, It
indicates milestone of a project.
syn : git tag : To list all tags.
There are 2 types of tags
1. Lightweight tags - It is kind of bookmarks to a commit.
They are simply pointers to a specific commit.
It does not contain tagging messages, date, or
tagger info.
syn : git tag <tag-name>
for ex : git tag v1.0
2. Annotated tag : These are the full objects in git database.
It includes the tagger's name, email, date and a message.
syn : git tag -a <tag-name> -m "message"
* Explore Github
1. Create account
2. Add repository
3. create files inside repmote repo
4. Add content to the file
5. commit the changes
6. Clone the repository to the local repo
* git clone : Clone is used to bring remote repository to the local workspace for
the first time.
We need to clone it where git repo should not be there or else it
will show error.
syn : git clone <url>
* Git push : It is used to push the local repo changes to the remote repo.
syn : git push origin <branchname>
* Git pull : It is used to bring the changes from remote repo to the local repo and
merge it to the local repo automatically.
syn : git pull origin <branchname>
* git fetch : It is also used to bring the changes from remote repo and stores in a
separate branch where
you can review the changes and merge if required.
syn : git fetch origin <branchname>
Note : The branch that fetches the data is stored in FETCH_HEAD
* Merge and rebase
Merge : Merge is nothing but integrating two branches.
syn : git merge <branchname>
It will merge the specified branch to the existing branch.
- It is simple and easy to understand merge concepts.
- It will mere latest content of 2 branches.
- MAintains original content as source code.
Q: How to resolve merge conflicts ?
-- > Merge conflicts occurs if the same line of code is modified on the same file
on two different
branches and when we try to merge those 2 branches we will get merge
conflicts.
Q: How to resolve merge conflicts ?
--> If i don't know whose changes should i take in the merge, I contact developers
who modified file on
branch1 and branch2, then they will decide and give new changes, After that i
will commit that to a file and continue
with merge.
Rebase : It is nothing but a merge here one branch will get added to a tip of
another branch.
It will not continue with the maerge until and unless we resolve merge
conflicts.
syn : git rebase <branchname>
- It will add history also.
- unifies the line development by rewriting history from source branch so that they
appear as children.
- Simplifies your history.
* Git Revert and Reset
1. Git Revert - It is used to undo the committed changes, History will not be
removed, we can track the reverted
changes in git log.
syn : git revert HEAD
git revert <Commitid>
2. Git reset : It is also used to undo the committed changes but history will be
removed.
There are 3 types of reset
1. Soft reset - It will move the files from git repo to the staging area. It reset
to specified commit id and history will be removed.
syn : git reset --soft HEAD~1
2. Mixed reset - It will move the files from git repo to the workspace.
syn : git reset --mixed HEAD~1
3. Hard reset - It is used to reset the previous committed changes, there will be
no clue that you have
committed the changes. hard reset will remove files from git repo,
staging area and workspace.
syn : git reset --hard HEAD~1
Note : In interview if they ask diff b/w revert and reset the always give revert
and hard reset defination.
* Git stash : If i am working on one branch and if i get some coritical work which
needs to be fixed on the
other barnch, then i don't want to commit the changes in the other
branch then before i swith to the
other branch i need to stash on the current branch which stores the
local files on a temp location
(It will not store in workspace or staging area), once i am back to
the current branch after completing
the critical work, i can bring back the changes using git stash pop.
syn : git stash : It is used to stash the files.
git stash pop : To bring back the stashed files.
git stash delete <stashid> : To delete stash
git stash apply : To apply the changes in working directory as well as in
staging area.
Q1: How to delete all the stash list.
git stash list : To list all the stash
git stash drop : To delete/drop all stash list.
* git cherry-pick - It is used to merge specific commit to a branch.
syn : git cherry-pick <commitid>
Note : If i want to merge multiple commits
syn : git cherry-pick commitid1 commitid2 commitid3.....so on
* There are 2 types of repository
1. Bare repo
2. Non bare repo
1. Bare repo : It acts as a centralized repository here we can only push and pull
the changes to
the repository and we can't perform or do any git operations here.
syn : git init --bare
2. Non bare repo : It acts as a local repo here we can modify the files and push to
a central repo, we can run all the git
commands.
syn : git init
* Branch stratergy :
Developers creates feature branch for their purpose and merge it to the Dev branch.
DevOps team creates release branch and merge then to the Dev branch.
Developers can pull the changes from master branch but they cannot push the changes
to the master branch.
If there are bugs in the branch they will be fixed right there.
Branches can be created for many reasons, Here we create branches for releasing.
Once the develpoment branch
is created from the master, development will be going on the dev branch, once the
code is ready on the dev branch
for the first release, we create release1 branch and make 1st release. Whatever the
issues related to the first
release will be fixed on release1 branch, Parallelly, development will be going on
the dev branch for the 2nd release
once the code is ready for 2nd relase on the dev branch before we create release2
branch we merge release1 branch
to the dev branch and then we create release2 branch and make 2nd release from the
elease2 branch.
Here release2 branch will acts as maintainance branch and so on...