GIT Tutorials
GIT Tutorials
Git References
In order to remember the git commit id’s we can use these references
Instead of commit id’s we can use these names directly
Commit id’s are very larger we can’t remember them so we use references
References are pointers to commit id’s
Most recent commit id is head or master
.git/refs directory -- > In these all the references will be stored
What is master
Master is nothing but the name of the branch
It is the reference or the pointer to the last commit
Wherever we require last is required we can easily use master
Master is a file and it contains commit id
git show -- > Following command gives complete information about this commit \
git show first 7 characters of the commit id
git show master is also same as above command as we are searching with commit id previously and
here, we are using master which is reference to commit id
master ~1 -- > Commit before recent commit -- > It means commit before head
Detached Head -- > It means head wont point to latest commit called master and head may point to
another commit, it is called as Detached head
V14
Usage 1:
Git Reset command -- > To remove the changes from the staging area
For example, I have a file a.txt -- > I have added and didn’t commit the changes to staging
Now, I want to discard the changes from stating area
Then we need to go for git reset a.txt
V15
Usage 2:
If we need to discard the commits from local repository then we need to go for git reset
Git reset <mode> <commit id>
Here whatever commit id we specify that will become the head and above all commits will be
deleted
Note: <mode> -- > It decides when the commit is deleted the related files present in staging and
working directory to be deleted or not
The allowed modes are
--mixed
It is the default mode. To discard changes from local repo and staging we need to use –mixed mode
It wont touch working directory.
By mistake if we have reset the values , we can easily revert as the files are present in working
directory
--soft
--hard
Soft reset
It will remove files only from local repo, but not from staging and working directory
Hard reset
It will remove changes from all the places (wd , stag , local repo)
We can’t even revert back the changes as the changes are unavailable
V16
GIT Aliasing
We need to know whether the alias name is available or not
Type the alias name type git1 , if git says no such command then we can consider so that the alias
name is free
By using got config command we can define aliases
V 17
.gitignore -- > If we want to ignore set of files we need to create file with .gitignore and we need to
specify the file names which should not be tracked
V 18
Git doesn’t consider directories. It only considers files.
Whenever we are adding files in an directory , implicitly directories will also be added
V 19 -- > Branching
git checkout -b branch_name -- > It creates new branch with the specified branch name and switches
to the new branch
V 21
V 23
Merging of Branch -- > Whatever changes are present in child branch needs to be merges with main
branch that is called as merging
For example we have master and child branch , we need to merge child branch changes to master so
we need to go to master branch and then merge the changes
git merge child_branch
After creating a new branch from master branch and if we don’t do any changes in the master
branch then git performs fast forward merge
In fast forward merge master will move to last commit
Three way merge -- > For example if we perform changes in master branch and try to merge created
new child branch with master branch then we will come across Three way merge , conflict issue will
come in to picture)
In 3 way merge a new commit will be created and HEAD pints to new commit
Before merge operation total commits are 5
After merge new commit id will be generated
Differences b/w fast forward and 3 way merge
V24
After creating the branch if updation happens in both the branches 3 way merge happens
We are aware of that in V23, now lets see the conflicts
If same file modified by both master and child branches then if we try to perform merge operations
then we come across a scenario called as conflict
We need to use merge tool here
Merge tools setups are in V 10
While we are working on some work , we got some urgent work then we need to save the un
committed changes from working directory and staging area changes to some location. We can do
that using stash
Git stash is applicable only for tracked files
To perform git stash at least one commit should be there
git stash
After un stash all the untracked changes will come to working directory
V 29
Partial stash -- > I want only few files to be stashed but not all files then I will go for partial stashing
git stash -p -- > This command will ask for confirmation whether to stash or not for each and every
file
V 30
How to Delete the stashes -- > git stash clear (Deletes all the available stashes)
How to delete particular stash -- > git stash drop stash_id
V 31
Remote Repository
When we want to share our code with other developers then we need to go for remote repo
Direct communication problems within peers
On every peer system git server is required else the developer cant share code with other developer
If dev A wants to communicate with dev B then dev A should be aware of host name or the ip
address of dev B , dev A should be aware of the dev B port number, on which port number git is
running on dev B system
Tomorrow Dev B configurations like host name , port num and other things may change that is the
reason why we are going for common repository
V 32
git remote -- > Gives us all the list of remote URL’s alias names
git remote -v -- > Gives the URL information as well
git push -- > I want to send the changes from local to remote repo we use git push command
git push <remote repo name> <branch name>
git push origin master
Using settings option, we can rename or perform required changes to remote repository
git clone -- > we can clone complete remote repo to local repo
git clone remote repo url
We can get clone url from here as well
git clone remote repo url my_project -- > Using this we can create our own project name
We don’t need to use gitinit command before closing because git clone command itself is
responsible to create local repo
V 35
Git tagging -- > git tag -- > To define static references we need to go for git tagging.
It is a label or mark to a specific commit in our repository. Generally, we use tags concept for release
purpose
There are 2 types of tags 1) Lite weight tag 2) Annotated tags (tags with information)
Lite weight tag - -> git tag <tag name> - > Label acts as a tag for latest commit as we haven’t
specified the commit id
git tag –list -- > Following command gives us the information about list of tags
Where tags will be stored
.git/refs/tags - -> All our lite weight tags information will be stored here
Is it possible to delete a tag? git tag -d <tag name>
git tag –delete <tag name>
With in the repository tag names should be unique
V 36
Annotated tag -- > We are going with this as we require more information about the tag created
Annotated tag internally implemented as object
git tag -a <tag name>
We need to create annotated tag with message as follows
git tag -a <tag name> -m ‘message needs to be typed here’
git tag -v <tag name> - -> Provides information about annotated tag
In the first command we got error because we are trying to get information about lite weight tag
-v option will work only for annotated tag
By using -f option or –force option we can replace or update the existing tag without deletion
git tag -a <tag name> -f <correct commit id> -m ‘commit message’
V 38
V 39
git reset command is destructive command and not recommended to use in public repos
git revert -- > Alternative to git reset
because git revert won’t delete commit history
git revert c3 -- > It will create new commit with previous commit c2 and commit ids of c3 and c4
remain and c2 will become latest commit
V 40