Git Commands
Git Commands
Version control software keeps track of every modification of the code in a special kind
of database.
Vcs system helps term of rollback to previous version in case of any issue with specific
version.
Centralized version control system :- CVCS uses a central server to store all files and enables
team collaboration.
CVCS works on a single repository to which users can directly access a control server.
Repository (server)
Working Copy
Workstation pc1
Crash of cvcs will result in losing the entire data of the project.
In distributed vcs every contributor has a local copy or "clone" of the main repository.
Users can change and commit local repo without any interference.
Users can update their local repo from the central server.
If the central server gets crashed at any point of time, the lost data can be easily
recovered from any one-off contributors’ local repositories.
Working dir
Repository
~/projects/my.project
./git
Text
V1 txt, v2 text
Stages of GIT:-
Working dir: - Area of live files also known as untracked area of GIT.
Staging Area: - Staging area is when git starts tracking and saving changes that occur in files.
Git Direcotry:- Also called "local repo" is yours .git repo its area where git save everything.
Remote repository is stored on a code hosting service like GitHub or on an internal server.
Branch In Git:- Branch in Git is a way to keep developing and coding a new feature or
modification to the software and still not effecting the main part of the project.
Git is open source and can be installed on all major os like Linux, mac, windows.
# sudo su
#apt-get update
To install git
To check version
#git --version
c://Git -version
#mkdir project
#cd project
#git init
GIT COMMIT:-
GIT STATUS: - it is a command which provides the details of current working details.
#git status
Note: - with the help of add. cmd we can move all working to staging area.
#git add .
#git diff
*** Git tracking & git Logs:-
First check the working dir on git with the help of cmd also we can create new project so
working to create new project.
#touch myfile
#ls
#git status
#git status
GIT LOGS: -
With the help of git log we can get to know about the commit details or commit file metadata.
#git log :- If we run this cmd then shows the logs of all commit details but in case we want to
check the particular single or author commit then run below cmd.
we can get the details of commit with the help email also.
Git hub is a website and cloud-based service that helps developers store and manage
their code as well as track and control changes to their code.
Owner
Repository Name
Raushan
Git Training
Description: -
Public
private
Add a README.file
add.gitignore
template gitbook
create.
Note:- if you want to sync with local git to remote git server firstly login into github ---goto the
code area form the will get clone option ,https, ssh, download zip other.
will use https copy that link after that come to the local server check the current path select the
path where you want to crrate a clone of remote server in your machine.
Basically git works an root folder come to the root folder and run this cmd.
#ls
#git status
We can create a file an local server that file we will upload to the remote server or central
server.
#git status
This cmd commit with changes & message (commit for track file)
#git push
Login into your github account -----settings---sshkey and GPG keys. (open this here will get
some option like your personal profile , nes ssh key,new gpg key with the help of ssh & gpg keys
for the setup.
Open ssh keys:- gener ssh key (click on it ) here will get some option like about ssh checking
for existing ssh keys generating a new ssh key and adding it to the ssh-agent adding a new.ssh
key to your github account.
1.Open gitbash
Copy the sshkey come back to github account ----setting--ssh and gpg keys---inside the ssh
keys click on new ssh key
sshkeyss/addnew
After that we need to test for that again we need to goto github ---setting---ssh and gpg
keys--ssh keys----generate sshkeys (click on that then you will get the option for testing ssh
connection -----click on testing your ssh connection).
#ssh -T [email protected]
Before running this cmd you should an the clone repo in your local machine.
After verification try to push or pull something from your local repo to remote server (github).
#git push
Branch In Git:-
#git branch
To create new branch:-
#git branch
#cat file.txt g1 23 e3
#git status
#ll
#git add .
#git status
#git push
First time we got error like (fatal:- the current branch develop has no upstream barnch to push
the current branch and set the remote as upstream use).
then we have to use below cmd:-
After run this cmd go to your github server referesh then you will get your develop branch
details like file or dir.
NOTE:- If you want to verify develop branch file or dir refelecting or not in master barnch.
#ll
After creation of develop branch if we create some file or dir in master branch it will not reflect
in develop branch.
#touch h1 b3 4
#git status
#git add .
#git push
After check into the develop branch file will not refelect.
Note:- if we are going to push the code by master branch then we dont need to used git push
origin (mastername).
All the branches are sync with master branch.
Note:- If you want to merge the code of two diffrent branchs like master or developer.
Go to your github account--verify fire where to where you want to merge the code.
Here we have two branch main or develop so we want to merge the code from develop to
main.
Review
If you want to work an merge file then we have to pull the latest code from develop branch.
branch develop:-
#Git init
# git status
#Git add .
#git status
# git log
Isolating features into diffrent branches is crucial practice for any serious developer.
At some point a pieace of code will reach a state where you will want to integrate it with
the rest of the project. this is where the git merge command come in.
Before merge :-
c0 c1 c2 c3 c4----hotfix
after merge:-
c0 c1 c2 c3 c4--- master
Practically we are going to do that:- go to your develop branch and do ls create fsome file or put
some data.
#ls
Note:- if we want to upload this code on github then we have to commit this code.
#git add .
#git status
#git commit
#git push
Get reset is a powerful command that is used to undo local changes to the state of a get
repo.
To reset the changes from both staging area and working dir at a time.
If we want to delete the add. file or current working dir file then we can use this cmd
The Net effect of the git revert command is similar to reset, buts its approach is
different.
Revert adds a new commit at the end of the chain to "cancel" changes.
REVERT or RESET?
If User have already pushed commit to the remote repo , a revert is a mice way to
cancel out changes.
Git workflow works well for picking up additional commits at the end of a branch, but it
can be challenging if a set of commits is no longer seen in the chain when someone
resets the branch pointer back.
It commits in local then reset is good, it commits is pushed then revert is good option.
Practical: -
Git reset:-
to check log:-
touch file1
vi file1
i am raushan.
:wq
git status
git add .
again edit file1 and enter some more data along with previous data.
vi file1
i am emplyee of estuate,
:wq
git add
after commit we fell ilke second commit is not correct in that way we will use RESET for
previous code line or commit.
If you want to reset your second commit then use this cmd firstly find the commit id with the
help of commit.
To soft cmd it will delete from the commit history log only if you want to delete from dir as well
as then we have to used hard cmd like
vi file1
:wq
git push
vi file1
:wq
git push
If you want to delete the commit of "commit revert" from remote server use below cmd
To this cmd we can delete the latest commit to the remote server.
git push
If we run this cmd they will create another commit with previous commit in that commit they
will delete the content as well as.
git add .
git commit
Git Conflict:-
When same file having different content in different branches if you do merge, conflict occurs
resolve conflict then add and commit.
Suppose you are implementing a new feature for your product, your code is in progress and
suddenly a customer escalation comes because of this, you have to keep aside your new feature
work few hours.
you cannot commit your partial code and also cannot throw away your changes so you need
some temporary storage, when you can store your partial charges and later an commit it.
To stash an item:-
git stash
git status
we can get the details of track or untracked file with the help of below cmd:-
git stash -a
If you want to continue your work o pending or stash file used another method or cmd also.
To check list:-
To pop will retrieve the file or delete from the stash area but apply cmd retrieve the data file noe delete
from the stash.