Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated:...

29
Git Guide Meher Krishna Patel Created on : Octorber, 2017 Last updated : May, 2020 More documents are freely available at PythonDSP

Transcript of Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated:...

Page 1: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

Meher Krishna PatelCreated on : Octorber, 2017

Last updated : May, 2020

More documents are freely available at PythonDSP

Page 2: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Table of contents

Table of contents i

1 Commands Summary 1

2 Git Local repository 32.1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.2 Git Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.3 Create project directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.4 Add files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.5 .gitignore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.6 Untracked, Staged and Modified file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

2.6.1 Untracked file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.6.2 Staged file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.6.3 Commit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.6.4 Modified files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

2.7 Git log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.8 Git diff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.9 Git reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2.9.1 Reset stage file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.9.2 Reset to previous commit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2.10 Git checkout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.11 Git rm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.12 Git branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

2.12.1 Create and delete git branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.12.2 Switch between branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.12.3 Git merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122.12.4 Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132.12.5 Removing conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132.12.6 Create branches through older commits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

2.13 Git tag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152.13.1 Create tag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152.13.2 List existing tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152.13.3 Delete tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

2.14 Git GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

3 Git remote repository 163.1 Creating repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.2 Add Repository to local git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.3 Push changes on repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.4 Clone repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173.5 Pull changes from repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

3.5.1 git remote add . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173.5.2 git pull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

i

Page 3: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

3.5.3 git fetch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

4 Working in a team 194.1 Pull the master branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194.2 Create branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204.3 Add reviewer and merge the changes to master . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214.4 Pull the master branch again . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

Page 4: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Chapter 1

Commands Summary

Following is the list of commands which are used in this tutorial.

Table 1.1: Commands summaryCommands DescriptionsLocal Repositorysudo apt-get install git Install git in Linux Ubuntugit config –global user.name meher Set username as ‘meher’git config –global user.email [email protected] Set email as ‘[email protected] config –global core.editor “vim” Set ‘vim’ as default text-editorgit config –global credential.helper cache Cache username and passwordgit init Initialize git repositorygit status File status i.e. modified and untracked etc.git add . Add all untracked filesgit add file1 file2 Add (stage) file1 and file2git rm –cached file1 Remove the staged file file1git commit -m “commit message” Commit stage file with ‘commit message’git log Show detail list of commitsgit log –oneline Show hash and commit name onlygit log –graph Show commits in the form of graphgit log –oneline –graph Show online-commit in the form of graphgit diff Differences between unstaged files and previous commitgit diff –cached Differences between staged files andgit diff –stat Show only changed filenames (not the details)git reset Remove all files from stage list (i.e. back to modified)git reset file1 Remove file1 from stage list (i.e. back to modified)git reset –hard 13802e3 Reset to previous commit with hash 13802e3git reset HEAD –hard remove all changes after last commitgit checkout file1 Remove changes from non-staged file1 to previous commitgit rm file1 Delete file1 from git (but available in previous commitgit branch Show all the branchesgit branch branch1 Create branch1git branch -d branch1 Delete branch1git checkout branch1 Go to branch1git checkout master Go to master branchgit merge branch1 Merge the branch1 to current branch e.g. mastergit checkout 13802e3 Create new branch from previous commit 13802e3git checkout -b branch1 First checkout and then create branchRemote repositorygit remote add repoName https://url_of_repo Add remote repo with name ‘repoName’

Continued on next page

1

Page 5: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

Table 1.1 – continued from previous pageCommands Descriptionsgit remote -v Show list of added repoNamesgit remote remove repoName Remove repoName from listgit push repoName branch1 Push ‘branch1’ to ‘repoName’git push repoName –all Push all branches to repoNamegit clone https://nameOfRemoteRepository Clone or download remote repositorygit clone –depth 1 https://nameOfRemoteRepository Clone only last branchgit pull repoName branchName Download and merge ‘branchName’ of repoNamegit fetch repoName branchName Download, but not merge repoName

2

Page 6: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Chapter 2

Git Local repository

Git is the version control system which is used for tracking the changes in the codes. Also, if we made some wrongchanges in the code or we are not happy with current changes, then the previous versions of codes can be restoredusing git. Lastly, version control systems allows to work in a group, so that different people can solve differentproblems and finally all the solution can be merge together. In this way, projects are more manageable and canbe completed efficiently. Further, git repository can be stored on the local machine as well remotely on the web.In this chapter, git local repository is discussed, whereas git remote repository described in next chapter. Finally,last chapter shows the list of command which we learn in this tutorial.

2.1 Installation

We need to install git to use this tutorial. Git can be install on Ubuntu using following command.

sudo apt-get install git

For more information or to install Git on other linux systems, window or macs, please visit the git websitehttp://git-scm.com/

2.2 Git Configuration

First we need to configure the git setting so that git can identify the person who is making changes in the codes.For this use following commands,

First two commands set the username and email id, whereas third command (optional) sets the default text-editorfor git. For example, vim is set as default editor here, therefore vim will be open for the commands which requiressome inputs from users.

$ git config --global user.name meher$ git config --global user.email [email protected]$ git config --global core.editor "vim"

Git stores these information in .gitconfig file, which is stored in the home directory. In Ubuntu, content of this filecan be seen using following commands,

$ cd$ vim .gitconfig[user]

email = [email protected] = meher

[core]editor = vi

3

Page 7: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

Use following command to cache the username and password, so that we need not to give credential values, i.e.username and password, for each git connection to remote repository,

$ git config --global credential.helper cache

2.3 Create project directory

Lets create a project directory at any desired location and initialize the git repository in that directory as follows,

$ mkdir gitProject$ cd gitProject$ git init

Last command will create a git repository, which is store in .git folder. Use following commands to view the contentof this folder,

$ ls -A.git

$ cd .git && ls -Abranches config description HEAD hooks info objects refs

$ cd ..

Now, any changes in the folder ‘gitProject’ will be tracked by the git. To see the change status in the folder, use‘status’ command as follows,

$ git statusInitial commitnothing to commit

Since, no changes are made in the directory, therefore status commands shows that there is ‘nothing to commit’.

2.4 Add files

Next, add one file e.g. ‘hello.py’ in the folder, with following contents,

# hello.py

print("hello 1")

Now, run the status command again and the changes in the directory will be displayed as below,

$ git statusOn branch master

Initial commit

Untracked files:(use "git add <file>..." to include in what will be committed)

.hello.py.swphello.py

nothing added to commit but untracked files present (use "git add" to track)

2.3. Create project directory 4

Page 8: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

Above message shows that there are two untracked files in the project directory i.e. hello.py and .hello.py.swp.Second file i.e. .swp is the file generated by vim editor. Since, we do not want to keep track of files which aregenerated by editors or software e.g. .pyc (python) or .so(C/C++), therefore these files should be removed fromtracking list; which can be done by using .gitignore file as shown next.

2.5 .gitignore

Create a .gitingore file in project directory; add file extensions and folder names, to avoid tracking of certain filese.g. automatically generated files i.e. .pyc and .swp or folders i.e. build etc. These files can be added to .gitignorefile as shown below,

.gitignore*.swp*.pyc

# sphinx-build foldersbuild:generated

# Byte-compiled / optimized / DLL files__pycache__/*.py[cod]*$py.class

# C extensions*.so

After adding .gitingore file, run status command again as below; we can see that .swp file is not displayed now, asit is removed from the tracking list.

$ git status[...]Untracked files:

(use "git add <file>..." to include in what will be committed)

hello.py[...]

2.6 Untracked, Staged and Modified file

2.6.1 Untracked file

The newly added files in the project directory are considered as untracked files. Since hello.py is created curretlyand it is new to git therefore it is shown as ‘Untracked’ file by status command. Status will remain the same untilthis file is added to git as shown next.

2.6.2 Staged file

When any file (untracked or modified) is added to git, then it is called staged file. ‘git add’ command’ is used toadd file to git as shown in below commands,

$ git add hello.py$ git status[...]Changes to be committed:

(continues on next page)

2.5. .gitignore 5

Page 9: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

(continued from previous page)

(use "git rm --cached <file>..." to unstage)

new file: hello.py[...]

Results of above status commands shows two things.

• First, use “git rm –cached <file>. . . ” to unstage, which means that files is staged and we can use ‘git rm’command to unstage it.

• Further, stage files are the files which are ready for the backup (but not backed up yet), hence git displaysthe message that ‘changes to be committed, new file: hello.py’. Commit is used to backup the staged file asshown next.

2.6.3 Commit

‘git commit’ command is used to store the changes in the git so that these can be recovered later. Each commitrequires a name so that we can identify the changes made during those commits. Commit operation can beperformed as below,

$ git commit -m "commit1 hello.py is added"[master (root-commit) 5e1b96b] commit1 hello.py is added1 file changed, 5 insertions(+)create mode 100644 hello.py

$ git statusOn branch masternothing to commit, working directory clean

Since, all the changes are added to git using commit, therefore status command displays that there is nothing tocommit now.

Note that, if ‘git commit’ is used in place of ‘git commit -m “commit1 hello.py is added” ’, then a vim editor willpop-up (as vim is set as default editor) and we need to enter the commit message on the top of that file i.e.“commit1 hello.py is added”.

2.6.4 Modified files

If we change the ‘hello.py’ file again, then git considered as modified file (instead of new file). To understand this,let add one more line at the end of hell.py as below,

# hello.py

print("hello 1")print("hello 2")

Now, use git status command and it will show that the hello.py file is modified file. Also, we can add and committhe modified file (not new file) using one line command i.e. git -am as shown below,

$ git status[...]Changes not staged for commit:[...]

modified: hello.py

$ git commit -am "Commit2 hello.py modified"[master d524017] Commit2 hello.py modified1 file changed, 1 insertion(+)

2.6. Untracked, Staged and Modified file 6

Page 10: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

2.7 Git log

Git log command is used to see the list of commits. Following git log commands are quite useful,

$ git logcommit d52401733f7cd237cd837cd362bf3e0c546aef47Author: meher <[email protected]>Date: Mon Jan 30 22:13:08 2017 +0000

Commit2 hello.py modified

commit 5e1b96bd7e89c94ccb1b6b85704ed23958cdff59Author: meher <[email protected]>Date: Mon Jan 30 22:00:42 2017 +0000

commit1 hello.py is added

$ git log --onelined524017 Commit2 hello.py modified5e1b96b commit1 hello.py is added

‘git log’ displays the details of commits whereas ‘git log –oneline’ give the hashes of the commits (i.e. numbers atthe beginning) and commit name only.

To see list of commits in the form of graph, use following command (red lines will be shown on the side of thecommits, which are quite useful, when branching is used in later part of the tutorial).

$ git log --graph[...]

$ git log --oneline --graph[...]

2.8 Git diff

‘git diff’ is used to see the difference between last commit and current files. Let add one more line at the end ofhello.py file. Then run the ‘git diff’ command to see the changes in the file as below.

# hello.py

print("hello 1")print("hello 2")print("hello 3")

• git diff : shows the differences between unstaged files and previous commit• git diff - -cached shows the differences between staged files and previous commit.• - -stage: it is used to avoid details of the differences.

$ git diffdiff --git a/hello.py b/hello.pyindex 78860b8..181ed03 100644--- a/hello.py+++ b/hello.py@@ -3,4 +3,5 @@

print("hello 1")print("hello 2")

+print("hello 3")(continues on next page)

2.7. Git log 7

Page 11: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

(continued from previous page)

$ git diff --stathello.py | 1 +1 file changed, 1 insertion(+)

$ git diff --cached

Note that - -cached command does not show any difference as there is no difference between stage file and previouscommit. Once hello.py is staged, then - -cached command will show the differences as displayed below,

$ git add hello.py$ git diff

$ git diff --cacheddiff --git a/hello.py b/hello.pyindex 78860b8..181ed03 100644--- a/hello.py+++ b/hello.py@@ -3,4 +3,5 @@

print("hello 1")print("hello 2")

+print("hello 3")

$ git diff --cached --stathello.py | 1 +1 file changed, 1 insertion(+)

Finally commit these changes as follows,

$ git commit -m "commit3 hello 3 is added"

2.9 Git reset

‘git reset’ is opposite of ‘git add’.. Reset command can be used in two ways, i.e. to reset the staged files in currentworking directory or reset the directory to previous commits, as discussed in this section.

2.9.1 Reset stage file

Reset can be used when we want to unstage some file which is added accidentally. Let add one moreline in the end of hello.py as below,

# hello.py

print("hello 1")print("hello 2")print("hello 3")print("hello 4 Password : 1234")

Next, stage this file using ‘git add’ and then unstage it using ‘git reset’ command as shown below,

$ git status[...]Changes not staged for commit:[...]

modified: hello.py(continues on next page)

2.9. Git reset 8

Page 12: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

(continued from previous page)

[...]

$ git add hello.py$ git status[...]Changes to be committed:[...]

modified: hello.py

$ git reset hello.pyUnstaged changes after reset:M hello.py

$ git status[...]Changes not staged for commit:[...]

modified: hello.py[...]

2.9.2 Reset to previous commit

In previous section, reset command is used to unstage the file which is not committed. If file is committed, thenwe need to reset the header i.e. go to previous commits to remove the file from the commit.

This can be very useful, when we committed something wrong (e.g. saved the password in some file) and want toremove those changes. In such cases, we need to reset the header, i.e. we need to go back to previous commit, asdiscussed next.

First stage and commit the changes made in the previous section, as shown below,

$ git commit -am "commit4 password added wrongly"

Now, use ‘revert –hard’ command to remove the changes i.e. the last line which contains the password. First seethe list of commits using ‘git log’ command,

$ git log --onelineb78daef commit4 password added wrongly13802e3 commit3 hello 3 is addedd524017 Commit2 hello.py modified5e1b96b commit1 hello.py is added

Header ‘b78daef’ is the commit in which password is stored, and we want to go to previous commit i.e. header13802e3. For this use reset command as shown below,

$ git reset --hard 13802e3HEAD is now at 13802e3 commit3 hello 3 is added

$ git log --oneline13802e3 commit3 hello 3 is addedd524017 Commit2 hello.py modified5e1b96b commit1 hello.py is added

Note that, the header ‘b78daef’ is removed now. If we look at hello.py again, then we will see that the last line,i.e. ‘ print(“hello 4 Password : 1234”)’ is removed from the file.

2.9. Git reset 9

Page 13: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

2.10 Git checkout

Suppose we add one more line at the end of hello.py, which is not staged till now. Then, ‘git checkout’ commandcan be used to remove the changes. To understand this, add one line to hello.py as below,

# hello.py

print("hello 1")print("hello 2")print("hello 3")print("hello 4 checkout example")

Now, run the checkout command as below. Since, hello.py file is modified, therefore ‘M hello.py’ is shown by statuscommand. Next, checkout command is used for hello.py which revert the changes in hello.py to previous commit.When status command is again run, it does not display anything as everything is same as previous commit. Also,if we look hello.py, then we find that the last line i.e. ‘print(“hello 4 checkout example”)’ is removed from the file.

$ git status -sM hello.py

$ git checkout hello.py$ git status -s

2.11 Git rm

‘git rm’ is used to delete the file and commit it as shown below,

$ git rm hello.pyrm 'hello.py'

$ git status[...]

deleted: hello.py

$ git commit -m "hello.py delete"

$ git log --oneline8c168c6 hello.py delete13802e3 commit3 hello 3 is addedd524017 Commit2 hello.py modified5e1b96b commit1 hello.py is added

Use reset command to restore the file as shown below,

$ git reset --hard 13802e3HEAD is now at 13802e3 commit3 hello 3 is added

$ git log --oneline13802e3 commit3 hello 3 is addedd524017 Commit2 hello.py modified5e1b96b commit1 hello.py is added

2.12 Git branch

Branches are the useful concept in git. Currently, we have only one branch i.e. master, which can be seen usinggit branch command,

2.10. Git checkout 10

Page 14: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

$ git branch* master

With the help of branches, we can experiment with the codes, without touching the code in master branch. Later,after completing the the experiments, we can add useful changes to master branch as shown below,

2.12.1 Create and delete git branch

Let create one branch with name ‘add2Num’,

$ git branch add2Num$ git branch

add2Num* master

‘git branch’ shows two branches i.e. add2Num and master. The * sign shows that currently we are in masterbranch. ‘git branch -d’ is used to delete a branch, e.g.

$ git branch diff2Num$ git branch

add2Numdiff2Num

* master

$ git branch -d diff2Num$ git branch

add2Num* master

2.12.2 Switch between branches

Checkout command is used to switch between branches,

$ git checkout add2NumSwitched to branch 'add2Num'

$ git branch* add2Num

master

Now, * is on add2Num branch. Next make some changes in the hello.py again. Add following lines at the endof the code, not on the top. If we add code at the top, git will generate ‘conflict’, which we will discuss later.

# hello.py

print("hello 1")print("hello 2")print("hello 3")

x = 2y = 3print (x+y)

Next, commit these changes as below,

git status -sM hello.py

(continues on next page)

2.12. Git branch 11

Page 15: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

(continued from previous page)

$ git commit -am "commit from add2Num"

Now, switch back to master branch and see the code. We can see that, when we switch back to master,the last 3 lines are removed.

$ git checkout master

Next, switch to add2Num branch again as below, and last three line will appear again. In this way, we canexperiment with the codes without affecting the main branch,

$ git checkout add2Num

Also, note the differences between ‘git log’ for both the branches. Currently, ‘master’ branch contains only 3headers, whereas add2Num branch contains 4 header as below,

$ git checkout masterSwitched to branch 'master'

$ git log --oneline13802e3 commit3 hello 3 is addedd524017 Commit2 hello.py modified5e1b96b commit1 hello.py is added

$ git checkout add2NumSwitched to branch 'add2Num'

$ git log --oneline223e688 commit from add2Num13802e3 commit3 hello 3 is addedd524017 Commit2 hello.py modified5e1b96b commit1 hello.py is added

2.12.3 Git merge

In previous section, we create a new branch and modified the code. Now, we are done with the change and wantto include those changes in the master branch. Merge command is used for this purpose.

First go to master branch, and then merge the add2Num branch as shown below,

$ git checkout masterSwitched to branch 'master'

$ git merge add2NumUpdating 13802e3..223e688[...]

$ git log --oneline223e688 commit from add2Num13802e3 commit3 hello 3 is addedd524017 Commit2 hello.py modified5e1b96b commit1 hello.py is added

Now we can see that the master branch has have 4 commits i.e. commits from add2Num branch is added to master.

2.12. Git branch 12

Page 16: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

2.12.4 Conflicts

If two branches contains different code at same lines of same file, then conflict will be generated by git; as git isunable to understand the correct version of the code. Lets, see it with example.

First checkout to add2Num branch and then modify the ‘print(“Hello 1”)’ line to ‘print(“Hello 1 from add2Num”)’as below,

# hello.py

print("hello 1 from add2Num")print("hello 2")print("hello 3")

x = 2y = 3print (x+y)

Next, commit the changes as below,

$ git checkout add2NumSwitched to branch 'add2Num'

$ git commit -am "Hello 1 from add2Num"

Now, go to master branch and modify the hello.py again with message “print(hello 1 from master)” as shown below,

# hello.py

print("hello 1 from master")print("hello 2")print("hello 3")

x = 2y = 3print (x+y)

Commit the changes and try to merge the branches. We can see that, git generates a conflict as shown below,because both branches are changing the same file with different content as same line,

$ git commit -am "Hello 1 from master"

$ git merge add2NumAuto-merging hello.pyCONFLICT (content): Merge conflict in hello.pyAutomatic merge failed; fix conflicts and then commit the result.

2.12.5 Removing conflicts

Conflicts need to be removed manually. If we open the hello.py file, then it will look as below,

# hello.py

<<<<<<< HEADprint("hello 1 from master")=======print("hello 1 from add2Num")

(continues on next page)

2.12. Git branch 13

Page 17: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

(continued from previous page)

>>>>>>> add2Numprint("hello 2")print("hello 3")

x = 2y = 3print (x+y)

The lines between arrows, are the line which are in conflict. Also, = sign separates the line which are generatingconflicts. Now, we need to modify the code manually, which can be done as below,

# hello.py

print("hello 1 from master and add2Num")

print("hello 2")print("hello 3")

x = 2y = 3print (x+y)

Finally, commit the changes and see the ‘git log’ as below.

$ git commit -am "Hello 1 accepted from both"

$ git log --oneline54133a4 Hello 1 accepted from both6f0d127 Hello 1 from master5336b3c Hello 1 from add2Num223e688 commit from add2Num13802e3 commit3 hello 3 is addedd524017 Commit2 hello.py modified5e1b96b commit1 hello.py is added

2.12.6 Create branches through older commits

Suppose we want to create a new branch ‘diff2Num’ from the commit3 i.e. through the header ‘13802e3’. For this,first we need to checkout the header and then create a branch there using ‘git branch -b’ command as below,

$ git checkout 13802e3Note: checking out '13802e3'.[...]

$ git checkout -b diff2Num

$ git branchadd2Num

* diff2Nummaster

In above listing, * shows that currently git is in diff2Num branch. Next, check the ‘git log’ command to confirmthat the new branch starts from third commit as below.

$ git log –oneline 13802e3 commit3 hello 3 is added d524017 Commit2 hello.py modified 5e1b96bcommit1 hello.py is added

Now, we can modify the code from third commit and merge those changes to master or work separately on thisbranch.

2.12. Git branch 14

Page 18: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

2.13 Git tag

Tags can be used to mark release points (v1.0 and v2.0 etc.).

2.13.1 Create tag

• Create tag v1.3 with message “it’s version 1.3”

$ git tag -a v1.3 -m "it's version 1.3"

2.13.2 List existing tags

• List the tags as below. Currently we have only tag,

$ git tagv1.3

• Show the tag details,

$ git show v1.3tag v1.3Tagger: Your Name <[email protected]>Date: Thu May 14 22:45:30 2020 +0530

it's version 1.3

commit e6372b406d0dafefca0980525ee8acaecf16962dAuthor: mpat260 <[email protected]>Date: Sat May 19 12:20:06 2018 +1200

updated in all tutorials

diff --git a/doc/source/_static/copybutton.js b/doc/source/_static/copybutton.jsold mode 100644new mode 100755

[...]

2.13.3 Delete tags

• Delete the tag with -d option,

$ git tag -d v1.3Deleted tag 'v1.3' (was 506739d)

$ git tag<nothing to display>

2.14 Git GUI

There are various graphical user interfaces are available e.g. git-cola, git-ext and giggle etc. Once, basic conceptsare understood, then we can use those interfaces to perform various operations using mouse clicks.

2.13. Git tag 15

Page 19: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Chapter 3

Git remote repository

In previous chapter, we make a local repository which stores all the changes on our local machine. To store therepository online, i.e. remote repository, we need to create an account on git-hosting-sites e.g. “BitBucket (whichprovides free private repository)” or “GitHub” etc.

3.1 Creating repository

First login to hosting site and create a repository there. Further, set it as public or private repository.

3.2 Add Repository to local git

Use following command to remote repository location to the git,

$ git remote add repoName https://url_of_repository

In above command, replace following according to your project,

• https://url_of_repository : replace this with actual web address of the repository.• repoName : give it a suitable name, which can be used as short-name of above url. In the other words, we

can use url and repoName interchangeably. Further, origin is the preferred name for this purpose.

Use following, command to see the detail of added repository

$ git remote -vrepoName https://url_of_repository (fetch)repoName https://url_of_repository (push)

In this way, we can add other contributers repository as well, which are working on the same project, which isdiscuss later part of the tutorial. Further, use following command to remove certain repoName,

$ git remote remove repoName

3.3 Push changes on repository

‘git push’ command is used to push the local changes to remote repository as below,

$ git push repoName branchName$ git push repoName --all

In above command, replace following according to your project,

16

Page 20: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

• reponame : replace it with the name of remote repository or with complete url address of the repository.• branchName: replace it with correct branch name e.g. master and add2Num here. To push all the branches

use - -all keyword.

3.4 Clone repository

Once repository is pushed on the remote, then it can be cloned (downloaded) on other computers by using ‘gitclone’ command as below,

git clone https://bitbucket.org/Userid/nameOfRemoteRepository

Replace url ‘https://bitbucket.org/Userid/nameOfRemoteRepository’ with your project’s url.

Also, we can use ‘–depth 1’ option, which will clone the latest branch only. This can reduce the download time,

git clone --depth 1 https://bitbucket.org/Userid/nameOfRemoteRepository

3.5 Pull changes from repository

If the repository is maintained by more than one user, then we can get the updates of others to local repositoryusing ‘fetch’ and ‘pull’ commands as shown below,

3.5.1 git remote add

First, we need to add other users repository to our repository. Use ‘git add remote’ command to add the otherusers repository, who is contributing the same repository, as follows,

$ git remote add userName https://bitbucket.org/otherUserid/nameOfRemoteRepository

In above command, replace following according to your project,

• ‘userName’ : replace it with desired name, which will be used as short name of the url• otherUserid : replace otherUserid with other user’s BitBucket or Git username• nameOfRemoteRepository : replace it with the remote repository name.

Next, use the ‘git remote -v’ command to see the list of contributor, which are added to our local repository,

$ git remote -vorigin https://BitBucket.com/myId/nameOfRemoteRepository (fetch)origin https://BitBucket.com/myId/nameOfRemoteRepository (push)userName https://BitBucket.com/otherUserid/nameOfRemoteRepository (fetch)userName https://BitBucket.com/otherUserid/nameOfRemoteRepository (push)

Here, origin is the name of out repository, where as userName is the name of repository of other contributor. Wecan add more contributor using ‘git remote add’ command.

3.5.2 git pull

This command will download the remote copy of current local branch and merge immediately to local branch.

$ git pull userName

Note that, userName is the name given for the url of remote repository in ‘git remote add’ section. Further, pullcommand will download the userName’s repo and merge it immediately with your repository if there is not conflict.In case of conflict, we need to manually remove the conflicts.

3.4. Clone repository 17

Page 21: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

3.5.3 git fetch

If we do not want merge the repository immediately, then Fetch command should be used to get the remotecopy of specific branch or all branches.

$ git fetch nameOfRemoteRepository$ git fetch nameOfRemoteRepository branchName

Here, first command fetch all the branches, whereas second command will fetch the specific branch. To mergethese changes, first go to the master branch and then run merge commands.

$ git checkout master$ git merge userName/master

Above commands will merge the userName’s master branch to our master branch.

3.5. Pull changes from repository 18

Page 22: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Chapter 4

Working in a team

In the previous section, we have seen various git-commands. In this section, we will use these commands toeffectively work in a team. We are assuming that, we already have a local copy of existing remote repository e.g.here the name of repository is ‘try_to_edit’

4.1 Pull the master branch

• First, step to work on git is to pull the master repository from the online repository e.g. github or bitbucketetc. This will ensure that our master branch is up-to-date.

• Also, before pulling the master branch, checkout the master branch in the local repository so that pull willmerge to master branch.

• In the below, we have checkout the master and then pull the master branch. Here, we can see the outputof pull command made the necessary changes in the readme.md file (i.e. local copy of readme.md file isupdated to remote repository)

$ git checkout masterAlready on 'master'Your branch is up-to-date with 'origin/master'.

$ git pull https://github.com/testgmj/try_to_edit.gitremote: Enumerating objects: 5, done.remote: Counting objects: 100% (5/5), done.remote: Compressing objects: 100% (2/2), done.remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0Unpacking objects: 100% (3/3), done.From https://github.com/testgmj/try_to_edit* branch HEAD -> FETCH_HEAD

Updating 1856873..224fc81Fast-forwardreadme.md | 2 +-1 file changed, 1 insertion(+), 1 deletion(-)

• Run the pull command again, and we can see that the master branch is up-to-date

$ git pull https://github.com/testgmj/try_to_edit.gitFrom https://github.com/testgmj/try_to_edit* branch HEAD -> FETCH_HEAD

Already up-to-date.

19

Page 23: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

4.2 Create branch

• If we are trying to add the new feature, then it’s better to create a branch for it. In this way, we can preservethe master branch.

• And we can push this branch to git repository. After a review by the team, we can merge it into the masterbranch. Later, we can delete the branch, if required.

• It’s always good to keep the master branch nice and tidy.

(checkout a new branch)$ git checkout -b add_num

Switched to a new branch 'add_num'

(see the current branch)$ git branch

* add_nummaster

• Now we will modify the location repository.• Create a new file with name add_num.py

# add_num.py

x = 2y = 3print("c = ", x+y)

• Next modify the readme.md file,

############## Meher #################Test 1 by MeherAdded add_num.py

######################################test 2 by Mekrip

useful command to remember

git clone https://github.com/testgmj/try_to_edit.git

git statusgit commit -agit push

• Check the status of the git repository,

$ git statusOn branch add_numChanges not staged for commit:

(use "git add <file>..." to update what will be committed)(use "git checkout -- <file>..." to discard changes in working directory)

modified: readme.md

Untracked files:(use "git add <file>..." to include in what will be committed)

add_num.py

no changes added to commit (use "git add" and/or "git commit -a")

• Add and commit the changes,

4.2. Create branch 20

Page 24: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

$ git add -A$ git commit -m "add_num.py added"

[add_num 8bf12e0] add_num.py added2 files changed, 7 insertions(+), 4 deletions(-)create mode 100644 add_num.py

• Now push the branch to online repository,

$ git push origin add_numUsername for 'https://github.com': xxxxPassword for 'https://[email protected]':Counting objects: 4, done.Delta compression using up to 8 threads.Compressing objects: 100% (3/3), done.Writing objects: 100% (4/4), 460 bytes | 0 bytes/s, done.Total 4 (delta 0), reused 0 (delta 0)remote:remote: Create a pull request for 'add_num' on GitHub by visiting:remote: https://github.com/testgmj/try_to_edit/pull/new/add_numremote:To https://github.com/testgmj/try_to_edit.git* [new branch] add_num -> add_num

4.3 Add reviewer and merge the changes to master

• After pushing the branch, we can see the changes ‘compare and pull request’ option on github as shown inFig. 4.1,

Fig. 4.1: Compare and pull request

• Also, we can see it in ‘pull request tab’ as shown in Fig. 4.2,

• We can add summary in the Github so that other can understand the changes well as shown in Fig. 4.3.Also, assign the reviewer for the changes. Finally, send a pull request.

• Summary of the action can be seen on the pull request page as shown in Fig. 4.4,

• Comments can be put as ‘general comments (see Fig. 4.5) or specific to certain lines (see Fig. 4.6)

• Once all the suggestions are fixed, then send a merge-pull request as shown in Fig. 4.7,

• Confirm merge

• After successful merge, we will have a option to delete the branch as shown in Fig. 4.9,

• Also, we can see the list of open and close pull request as shown in Fig. 4.10,

4.3. Add reviewer and merge the changes to master 21

Page 25: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

Fig. 4.2: Pull request tab

Fig. 4.3: Assign reviewer, add summary and pull request

4.3. Add reviewer and merge the changes to master 22

Page 26: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

Fig. 4.4: See summary

4.3. Add reviewer and merge the changes to master 23

Page 27: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

Fig. 4.5: Reviewer can provide comments to update

Fig. 4.6: Comment on individual lines

Fig. 4.7: Merge request

4.3. Add reviewer and merge the changes to master 24

Page 28: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

Fig. 4.8: Confirm merge

Fig. 4.9: Delete branch (optional)

Fig. 4.10: List of open and close pull request

4.3. Add reviewer and merge the changes to master 25

Page 29: Git Guide - Read the Docs...Git Guide MeherKrishnaPatel Createdon: Octorber,2017 Lastupdated: October,2018 More documents are freely available at PythonDSP Table of contents Tableofcontents

Git Guide

4.4 Pull the master branch again

• Now we can pull the master branch again to update the local repository.• Also, we can optionally delete the branch which is already merged to master branch.

(checkout master )$ git checkout master

Switched to branch 'master'Your branch is ahead of 'origin/master' by 1 commit.

(use "git push" to publish your local commits)

(pull master from remote repository)$ git pull origin master

remote: Enumerating objects: 1, done.remote: Counting objects: 100% (1/1), done.remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0Unpacking objects: 100% (1/1), done.From https://github.com/testgmj/try_to_edit* branch master -> FETCH_HEAD

1856873..6c8bbac master -> origin/masterUpdating 224fc81..6c8bbacFast-forwardadd_num.py | 3 +++readme.md | 8 ++++----2 files changed, 7 insertions(+), 4 deletions(-)create mode 100644 add_num.py

(delete the branch)$ git branch -d add_num

Deleted branch add_num (was 8bf12e0).

4.4. Pull the master branch again 26