0% found this document useful (0 votes)
15 views

GIT Notes

git

Uploaded by

kartheekdevops95
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

GIT Notes

git

Uploaded by

kartheekdevops95
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

git open source tool

can be installed on any OS, same commands will work on every OS

Install git

connect edureka lab:

git is already installed

new Ec2 machine AWS

command is:

$ yum install git -y

$ apt-get install git -y

ON lab:

click on terminal

execute command:

$ sudo su -

$ git --version

Scenario 1: Create a local repo

$ mkdir myproject
$ cd myproject
$ touch index1.html index2.html
$ git init
created a local repo

Scenario 2: Git configuration to add username and email details

$ git config --global user.name sonal0409

$ git config --global user.email [email protected]

$ git config --list

$ cat ~/.gitconfig

Scenario 3: Add files to local repo

$ git add index1.html

$ git commit -m "add file"

to check files in local repo

$ git ls-files
Scenario 4: git log

$ git log
$ git log --oneline

$ git show <commitid>

$ git show 8020b67

**********
Scenario 5: Modify an exisitng file, which is already in Local repo

add the modification to LR

1 method:

$ git add index1.html


$ git commit -m "modification"

add the modification to LR

2nd method:(using -a option: add all the changes & commit it)

$ git commit -a -m "modification index2.html"

************

Scenario 6: to check difference between 2 version of a file

$ git diff filename

$ git diff commit1 commit2

********************************************************
Scenario6: Deletion of file

- Deletion of file from WD & local repo

# git rm file1
# git status
# git commit -m "deletion of file1"
# ls
# git ls-files

- Deletion of file local repo only

# git rm --cached file2


# git commit -m "deteled file from local"
# git status // file will be untracked
# ls
# git ls-files

# add the untracked file to local repo


***********************************************************

Scenario7: Ignoring Files

# vim log
# git status // file untracked
# vim .gitignore // add name of file "log" , which wil be noe ignored
# git status // log will be ignored and .gitignore untracked
# git add .gitignore
# git commit -m "added ignore file"
# git status // working tress has to be clean

************************************************************

Scenario 8: Reverting the changes:

# vim file3 // create a new files


# git add file3
# git commit -m "added file3"

//Delete the file

# git rm file3
# git commit -m "deleted file3"
# ls
# git ls-files

# git log --oneline // last commit id is of delete action, copy it


// lets revert the commit

# git revert commit id

// file back to WD and local


# ls
# git ls-files

***************************************************************
Scenario 9: Resetting the commits OR Removing Certain changes

# git log --oneline

# git reset --hard commitid // permanently removes, changes cannot be reverted


back.

*******************************************************************

Branching

# git status ==> shows master branch


# git branch b1 // copies all files form master
# git status
# git checkout b1
# git status
// On branch b1

# git ls-files // all files same as master on b1


# git log --oneline // all commit same as master

Create a file on branch b1


# vim file4
# git add file4
# git commit -m "added file4"
# git ls-files
# git log --oneline // 1 new commit which is not on master right now

Merging a file from branch b1 to master

# git checkout master


# git ls-files
# git merge b1 master // merge from source b1 to destination master
# git ls-files
# git log --oneline

Resolve Conflicts on Merge

// create a file on Master

# git branch // should be on Master branch

# git login // new file , add text


# git add login
# git commit -m "login file"

# git checkout b1

# git login // new file , add text ==> filename same but text is different
# git add login
# git commit -m "login file"

Merge b1 to master

# git checkout master

# git merge b1 master

// Conflict error message


// manually edit login file and add changes wherever required.
# vim login // save it
# git add login
# git commit -m "conflict resolve"

File on b1 can also be merged if required

# git checkout b1
# git merge master b1
# git chckout master

********************************************************
STASHING of files on GIT

# git status ==> * indicates we are on master branch


# vim login.txt ==> inster new code for signout button ==>esc ==> :wq!

New feature development activity on branch 1

# git status ==> chnages that are untracked and we don’t want to commit them right
now.

So stash them on temporary space:

# git stash ==> all the untracked changes will be moved to temperory space

Where is this temporary space?

# git stash list ==> will give list of chnages stashed

# git show stashnumber ==> give stash number

Now developer can work on new feature by switching to branch b1

# git checkout b1
# vim logout.txt
==> add code for logout ==>esc ==> :wq!
#git add logout.txt
# git commit -m “logoutcode”

Now switch back to master brach

# git checkout master

GET changes back from stash ===> UNSTASHING

# git stash pop stash@{0} ==> will revert back all untracked file chnages
After unstashing, the files will be dropped from temperory shelves.

OR
# git stash apply statsh@{0} ==> will revert back chnages from temperory shelves
to working directory
// and files still remains on temperory shelves if needed latter in other branches.

# git statsh list ==> nothing will be there on executing stash pop.

PARTIAL STASH

# git stash -p ==> give y for which ever file we want to stash

==> give N if we don’t want to stash

# git stash drop stash@{0}

# git stash list

# git stash clear ==> no stash will be there


***********************************************************************************
*
REBASE:

# git log --oneline ==> see how many commits are there and reset commits to only
last 2 commits

# git reset --hard commitid ( give last second commit id)


# git log --oneline ==> 2 comits will be there

# git branch b2 master ==> create new branch b2 from master


# git log --oneline ==> 2 commits will be there

# git checkout master


# vim login.txt ==> instert text ==>esc ==> :wq!
# git add login.txt
# git commit -m “file4 added to master”

# git log --oneline ==> three commit id will be there --copy it on notepad

# git checkout b2
# vim logout.txt ==> instert code for logout ==> esc ==> :wq!
# git add logout.txt
# git commit -m “logout on b2”
# git log --online ==> 3 commits will be there on branch b2 ==> copy it on notepad

Now if we use merge command, we will have commits and code as file1.txt, file2.txt,
logout and login.txt(from master) . which is called as parallel merge

But if we want to merge linear on branch then we will do rebase.


Like this : file1.txt, file2.txt, login.txt(frommmaster) , logout.txt

# git rebase master

# git log --oneline ==> we will have commits arranges in liner fashion. ==> copy on
notepad and show.

***********************************************************************************
*
REMOTE REPOSITORY:

Create login and password of GITHUB

Create a new repository on the git hub

Copy the URL of the repository to add local repo to external repo:

# git remote add origin URL of externalrepo

# git remote -v // will show that origin is mapped to external repo url

PUSH files of master into external repo


# git branch ==> checkout on master branch
# git push origin master ==> pushes master files to origin(external repo)
Username:
Passowrd:
//Now go to external repo and see all the files of master will be available

PUSH branch b1 onto external repo

# git push origin b1 ==> pushes files of b1 branch onto external repo
It will ask for username and password ..give follwoing details:
Username:
Passowrd:
Now go to external repo and see all the files of b1 will be available.

************************************************************************
DELETE Branches in external REPOSITORY
- Delete branch on local repo:

# git branch -D b1 ===> Branch get deleted on local

# git branch

Branch will still be on external repo

To remove branch form external repo


**********************************************
# git push origin --delete b1

It will ask for username and password ..give follwoing details:


Username:
Passowrd:

PULL files from external repo to local repo


***************************************************
Create files from github and pull the file into local repo

# git pull origin


# ls ==> new file will be there

Fetch files from external repo to local repo


******************************************************
Create a new file on external repo

# git fetch origin

# git pull origin ===> pull= fetch +merge

# git pull remotebranch localbranchname


# ls //All files will be there in local as same as remote

Fetch only specific file from remote Master branch


**********************************************

# git fetch --all

# git checkout origin/<branch_name> -- fileName

# git checkout origin/master -- fileDave

Conflicts in external and local repo.


*****************************************
Create a file in remote repo file5.txt ==> add text
Now create a file in local repo ==> same name file5.txt ==> add text
# git add file5.txt
# git commit -m “local file5”

Now without pulling from remote just push your changes

# git push origin master

It will give conflict as there are changes in remote repo file

So we have to first pull chnages from external repo , add them , commit them and
then push our changes
# git pull origin

Agin conflict will be there

Resolve conflict again

# vim file5.txt ==> add the content required


# git commit -a -m “new chnages”

# git pull origin

# git push origin master ==> give credentials

All changes will be there

Cloning a repository
*************************************************
Go to github--> create a new repository
Use this repo:
https://github.com/Sonal0409/7AMGITDEMO

On host machine:
Create an emplty directory
# cd ..
# pwd

# mkdir project2
# cd project2
# git clone URL of new external repository

==> it will copy all the files from external to your local repository

# ls
# cd directoryname/
# ls ==> files will be shown
# ll -al ==> all files and .git file will also be there

You might also like