You are on page 1of 51

Git Introduction

29 August 2020 12:42

Git-GitHub-Consolidated Page 1
Git-GitHub-Consolidated Page 2
Git-GitHub-Consolidated Page 3
Git-GitHub-Consolidated Page 4
Install Git
29 August 2020 12:53

Git-GitHub-Consolidated Page 5
Initialize Git folder
29 August 2020 13:13

//after executing the command

Git-GitHub-Consolidated Page 6
Commits are snapshots of a point in time or points of interest
along the timeline of a project's history.

//after executing git commit -m "message"

Git-GitHub-Consolidated Page 7
//Keep the commit atomic & give appropriate message
//should be single set of change-could be one or more files

Git-GitHub-Consolidated Page 8
//Every commit new snapshot is made

Git-GitHub-Consolidated Page 9
//If they are not related ,it is not good to commit together
//stage it and commit separately

Git-GitHub-Consolidated Page 10
//modifying a file

//again modifying without committing


//accidently overwriting

//story1.txt in staging area is cached

Git-GitHub-Consolidated Page 11
//got overwritten by accident

//restoring

//I want to commit story2.txt before story1.txt


//we can use restore

//git rm
//to remove file(s) from staged area
//be careful with arguments

Git-GitHub-Consolidated Page 12
//you added both by mistake

//to remove notes.txt from staging area

//this file will not be tracked

Git-GitHub-Consolidated Page 13
Git Log
29 August 2020 16:28

• Commit id /Commit Hash


• Author Name
• Date & Time Committed
• Commit Message

Git-GitHub-Consolidated Page 14
Git-GitHub-Consolidated Page 15
Git Branch
29 August 2020 23:24

Default branch is the master branch

Git-GitHub-Consolidated Page 16
Head is where you are currently in Git
Repository

Head points to the last commit in the


branch you are currently on

When you switch branches head moves with


you

Note : Any changes in a branch is reflected in other branch also when it added but commit takes a
separate snapshot

Git-GitHub-Consolidated Page 17
Git Merge //set vim as core editor in ubuntu
21 August 2020 14:55 //useful if you set to something else and is not working

1.changes in the master and committed will not reflect in


the branch
2. Note: already existing branch
3. We have to checkout to that branch and merge from
source
4.Left side is the example

Git-GitHub-Consolidated Page 18
Fastforward merge-when branch (non master) is
committed more than once and master is not changed.
Simple merge will work from master
No Fastforward merge- when master has 1 or more
commits and the non master branch does not know it
has its new commit . We have to use
--no-ff option

Git-GitHub-Consolidated Page 19
Rebasing
31 August 2020 01:03

//one liner command

Git-GitHub-Consolidated Page 20
//new merge commit happens

Git-GitHub-Consolidated Page 21
Git-GitHub-Consolidated Page 22
//we are basically putting one branch on top of other one

//new commit id is created when we merge

//when we rebase the commit id/hash is updated

Git-GitHub-Consolidated Page 23
//interactive rebase
//remember to squash the commits

Git-GitHub-Consolidated Page 24
Remote Repository
30 August 2020 00:36

//to list all the remote repositories


//we can push the data and fetch from the hosted repository

//current branch we are on

//Before Push, our local branch(master) has 3 more commits


//remote repository has update till Added second story
//We need to push the latest commit for remote repository to be in sync with local repo

//after push

Git-GitHub-Consolidated Page 25
//below is the case where lot of commits have happened but not set to remote
//repository & remote repository files and local master does not match
//so if we try to push ,it will not work
//

//if not remote is set then we use add

//to remove the existing remote git


//note:we have to use add again if we have to set

//to update the existing remote git

//if we want to push to remote first set the upstream and branch name

Git-GitHub-Consolidated Page 26
//if we want to push to remote first set the upstream and branch name

//sometimes it may not work, if there content in the local repo & remote repo does not match
//then we have to pull from remote & then push
//this can happen if remote master is merged from another branch in remote and we
//don’t have the changes

//suppose if the commits does not match because of different repositories we have to use
//--allow-unrelated-repositories
//git hub does not allow to raise pull request from remote if the branch histories are different
//solution is pull from remote branch ,merge here and update the master directly

Or

Git-GitHub-Consolidated Page 27
Pull Request
30 August 2020 01:14

Steps to create a pull request


1.push your branch to branch in your name(not master)
//below example content moves from local branch rakesh to remote branch rakesh

2. Rakesh has to create login to his github account & create a pull request
3.

4.

5. Click Compare & pull request

Git-GitHub-Consolidated Page 28
6.

7. The owner of the repo will check ,compare & merge

8.

9.

10.

Git-GitHub-Consolidated Page 29
Fetching & Pulling
30 August 2020 02:17

//any update happening to origin/master will ahead in commit in the remote


//it will not be automatically updated in the local
//In order to sync with the origin/master we have to (fetch & merge ) / pull

Or

Git-GitHub-Consolidated Page 30
Git-GitHub-Consolidated Page 31
Cloning & Forking
30 August 2020 22:16

Cloning Remote Repository

//when you clone, Repository Name RakeshVijay will become folder and the repo
//contents will be inside the RakeshVijay folder
//we have to shift to the RakeshVijay folder in order to access and Work on it

//we can also achieve the same with downloading the zip and unzipping in the location we wish to work

Forking

Forking is done to make a separate copy of the repository which we would not likely to merge it back
//eg some publicly available projects ex linux
//we can also contribute to the open source project, make changes in your copy & push it to your branch and generate pull request. If they agree
//with your code ,it will be merged

Git-GitHub-Consolidated Page 32
Git-GitHub-Consolidated Page 33
Merge Conflict
30 August 2020 22:56

1. When a merge conflict occurs, open the file remove what is not required & keep what is required
2. Add the file git add filename(s)
3. git commit
4. Git merge

5.

6.

7.

Git-GitHub-Consolidated Page 34
Cherry Picking
31 August 2020 11:41

Git-GitHub-Consolidated Page 35
Git-GitHub-Consolidated Page 36
Undoing
30 August 2020 02:55

// to move around in the previous commits


//to work normally again checkout branch you wish to work with

Git-GitHub-Consolidated Page 37
//unstaging

//to change the commit message

Git clean -n //dry run


Git clean -f //force clean

//git clean deletes the untracked files present in the working directory

//Note: Revert will delete the changes (any file added in the commit will be deleted)
//it will not be available in the staging as it is already committed
//it will keep the changes in git history

Git-GitHub-Consolidated Page 38
//hard reset , you will loose the contents committed if reset

//soft reset, the contents of the commit will be in the staging area

Undoing hard reset to HEAD


$ git reset --hard HEAD@{1}

$ git reset --soft HEAD@{1}

Git-GitHub-Consolidated Page 39
Git CheatSheet1
14 August 2020 14:30

Git-GitHub-Consolidated Page 40
Git-GitHub-Consolidated Page 41
Git Cheat Sheet2
31 August 2020 02:40

Git-GitHub-Consolidated Page 42
Password Solution
17 August 2020 19:12

Git-GitHub-Consolidated Page 43
Git Stash
01 September 2020 01:38

Git-GitHub-Consolidated Page 44
//to see the content of the stash

//to pop a specific stash

Git-GitHub-Consolidated Page 45
Reflog
01 September 2020 01:46

//this command shows us all the actions taken on this repo


//merges, reverts, resets, simple commit

//if we made a mistake, we can undo this by resetting HEAD based on the information
//from the reflog

Difference b/w git log & git reflog


Git log shows only information about the commits
Git reflog shows the information along with the state of the repository

Git-GitHub-Consolidated Page 46
GitEclipse
24 May 2020 00:29

Git Eclipse Usage

1 Setting Up Eclipse a)Latest Eclipse comes with Git Built In


for Git b)Earlier versions did not come with Built In support

To check- if Git is present

1)In Eclipse IDE - Windows->Preferences


Day 2)Type filter text git
You should see

If Git is not present


1 .Click on Help-> Install New Software
2. Click Add ,Enter
Name: Egit
Locations: https://download.eclipse.org/egit/updates

3.Select All , follow the process(steps to complete)

4.Go to Windows->Preferences type git

You can type your email and name(Very Important


Note: You can also open .gitconfig file

2 Setting the 1.Select Window->Perspective->Customize Perspective


2.
perspective

Git-GitHub-Consolidated Page 47
3.Click Apply & Close

3. Open Git 1.Select Window->Show View->Other


2.
Repository &
Staging View

3.Click Open (Use Ctl Key to select both)


4.You should see at bottom left corner

5.You should see this in the menu bar.


(Note: It will be inactive if no repository is selected)

4.Creating a git Open the Project(java/maven)-> Right click on the Project ->Team-> Share Project

local repository for


a project
(Similar to git init)

Select Use or create repository in parent folder of project


Select project
Click Create Repository
Click Finish

5.Clone a project 1.Click on file->Import-

from git hub using


eclipse

2.Click Next

Git-GitHub-Consolidated Page 48
3.From GitHub repository copy the .git url

Click Next & Finish

6. While working to 1.Make sure Git Repositories view is open


Note: If you are not seeing this view, Window->Show View->Other-> Git-> Select Git Repositories, also Git Staging
this.

Click on Branches to check what branches are avaiblable in local repositories

To create a new branch

Select master as source and type new branch name


And click Finish

6.Pushing changes Note: Don’t push directly to master branch unless you are sure
1.Before pushing your code pull from remote master to local master
to Git Hub 2.

7.Pull from GitHub 1.Switch to branch(local master)


2.Fetch from

Git-GitHub-Consolidated Page 49
This will reflect in Git Repositories(shows the change in the remote master)

3.click on Merge

4.Select master from Remote Repository to merge with local master

5.Click Merge (Local Master will get updated from Remote Master)

8.Working with 1.Switch to local branch you want to work with (typically you will work with the branch)
2.
branches

3.click Check Out


4.Select Merge, Select Local ,master and click Merges

5.

Make sure that the Id is Same


6.Also Open History Tab

Git-GitHub-Consolidated Page 50
7.Work with the branch ,add files,
1.When you create a new file or modify an existing file, after saving it will come to unstaged/tracked
2.Click on add file/files to index which will become Staged

3.Type the commit msg & click on commit

7.Once the work over /milestone reached ,right click on the branch
8.

9.Click Preview

9.Click Push (The code gets pushed to remote branch


10.Code Moderator/Maintainer will merge all the request /resolve and merge to master branch(remote)

Git-GitHub-Consolidated Page 51

You might also like