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

Similar documents
Version Control with GIT

Computer Science Design I Version Control with Git

git commit --amend git rebase <base> git reflog git checkout -b Create and check out a new branch named <branch>. Drop the -b

Git for Version Control

GUIDE TO MAKE A REAL CONTRIBUTION TO AN OPEN SOURCE PROJECT 1. 1

Submitting your Work using GIT

Version Control. Software Carpentry Github s Hello World Git For Ages 4 And Up You need source code control now

Version control with Git.

Git. Presenter: Haotao (Eric) Lai Contact:

2 Initialize a git repository on your machine, add a README file, commit and push

Git. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

John DeDourek Professor Emeritus Faculty of Computer Science University of New Brunswick GIT

GETTING STARTED WITH. Michael Lessard Senior Solutions Architect June 2017

Git. Charles J. Geyer School of Statistics University of Minnesota. Stat 8054 Lecture Notes

Chapter 5. Version Control: Git

Index. Alias syntax, 31 Author and commit attributes, 334

Effective Software Development and Version Control

Windows. Everywhere else

Version Control System - Git. zswu

GIT VERSION CONTROL TUTORIAL. William Wu 2014 October 7

1. Git. Robert Snapp

GIT Princípy tvorby softvéru, FMFI UK Jana Kostičová,

Topics covered. Introduction to Git Git workflows Git key concepts Hands on session Branching models. Git 2

Git & Github Fundamental by Rajesh Kumar.

Working with GIT. Florido Paganelli Lund University MNXB Florido Paganelli MNXB Working with git 1/47

CSE 391 Lecture 9. Version control with Git

How to version control like a pro: a roadmap to your reproducible & collaborative research

1/20/13 Git tutorial. Git tutorial. Mike Nolta. file:///users/nolta/github/reveal.js/git.html?print-paper#/ 1/31

GIT. A free and open source distributed version control system. User Guide. January, Department of Computer Science and Engineering

Distributed Version Control Git

Lab Exercise Git: A distributed version control system

Tutorial: Getting Started with Git. Introduction to version control Benefits of using Git Basic commands Workflow

EECS 470 Lab 4. Version Control System. Friday, 31 st January, 2014

Git. Ľubomír Prda. IT4Innovations.

Software Development I

Software Revision Control for MASS. Git Installation / Configuration / Use

CS 520: VCS and Git. Intermediate Topics Ben Kushigian

Outline. Version Control System (VCS) basics Git terminology & concepts Basic Git commands Branches in Git Git over the network (time permitting)

Introduction to distributed version control with git

Git Tutorial. André Sailer. ILD Technical Meeting April 24, 2017 CERN-EP-LCD. ILD Technical Meeting, Apr 24, 2017 A. Sailer: Git Tutorial 1/36

A Brief Introduction to Git. Sylverie Herbert (based on slides by Hautahi Kingi)

Working in Teams CS 520 Theory and Practice of Software Engineering Fall 2018

Source Code Management wih git

git Version: 2.0b Merge combines trees, and checks out the result Pull does a fetch, then a merge If you only can remember one command:

Git Introduction CS 400. February 11, 2018

LAB MANUAL GIT BY PRATIK JAIN. To start with git bash, we need to authenticate ourselves so that for every commit, git can use this information.

Git tutorial. Katie Osterried C2SM. October 22, 2015

Version control. what is version control? setting up Git simple command-line usage Git basics

A Brief Git Primer for CS 350

What is Git? What is Git? Version Control. Barry Grant

GIT CHEAT SHEET. CONFIG --global => write to ~/.gitconfig, otherwise to.git/config. git config [--global] -l git config [--global] -e

A BASIC UNDERSTANDING OF VERSION CONTROL

From Commits to Collaboration: A Git Tutorial for Social Scientists

Tutorial 2 GitHub Tutorial

Revision Control and GIT

git the SCM system Jan-Simon Möller training.linuxfoundation.org

Git: Distributed Version Control

Git Tutorial. Version: 0.2. Anders Nilsson April 1, 2014

Eugene, Niko, Matt, and Oliver


Distributed Version Control (with Git)

Algorithm Engineering

GIT. CS 490MT/5555, Spring 2017, Yongjie Zheng

Git version control with Eclipse (EGit) Tutorial

Advanced Operating Systems Control Versioning with GIT. Giuseppe Massari

Git, the magical version control

Assumptions. GIT Commands. OS Commands

213/513/613 Linux/Git Bootcamp. Cyrus, Eugene, Minji, Niko

Software Project (Lecture 4): Git & Github

Hg Tutorial. For : COP Object oriented Programming (Using C++) Biswas Parajuli

Version Control: Gitting Started

Git: Distributed Version Control

FEEG Applied Programming 3 - Version Control and Git II

Git AN INTRODUCTION. Introduction to Git as a version control system: concepts, main features and practical aspects.

Git AN INTRODUCTION. Introduction to Git as a version control system: concepts, main features and practical aspects.

6 Git & Modularization

Agenda. - Final Project Info. - All things Git. - Make sure to come to lab for Python next week

CS 261 Recitation 1 Compiling C on UNIX

G E T T I N G S TA R T E D W I T H G I T

! #Running this command from the top directory of your project will add all your changes."! $ git add."

Revision control. INF5750/ Lecture 2 (Part I)

Introduction to Git. Database Systems DataLab, CS, NTHU Spring, 2018

Common Git Commands. Git Crash Course. Teon Banek April 7, Teon Banek (TakeLab) Common Git Commands TakeLab 1 / 18

Introduction to Version Control with Git and GitHub

Introduction to Git and Github

DEAD-SIMPLE VERSION CONTROL FOR YOUR TEAM GIT WITH MATTHEW REIDSMA GRAND VALLEY STATE UNIVERSITY

Version Control with Git

Crash course in version control (focusing mainly on git)

GIT FOR SYSTEM ADMINS JUSTIN ELLIOTT PENN STATE UNIVERSITY

Introduction to Version Control

Introduction to Version Control using Git

Reproducibility with git and rmarkdown

Linux and Git Boot Camp

Barry Grant

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Gerrit

CSCE UVM Hands-on Session-1 Pre-Work

Lab Objective. Lab Assignment. Downloads and Installation

Versioning with git. Moritz August Git/Bash/Python-Course for MPE. Moritz August Versioning with Git

Version Control Systems (VCS)

VCS VERSION CONTROL SYSTEMS

Transcription:

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

Table of contents Table of contents i 1 Commands Summary 1 2 Git Local repository 3 2.1 Installation................................................. 3 2.2 Git Configuration.............................................. 3 2.3 Create project directory.......................................... 4 2.4 Add files................................................... 4 2.5.gitignore.................................................. 5 2.6 Untracked, Staged and Modified file................................... 5 2.6.1 Untracked file............................................ 5 2.6.2 Staged file.............................................. 5 2.6.3 Commit............................................... 6 2.6.4 Modified files............................................ 6 2.7 Git log.................................................... 7 2.8 Git diff................................................... 7 2.9 Git reset................................................... 8 2.9.1 Reset stage file........................................... 8 2.9.2 Reset to previous commit..................................... 9 2.10 Git checkout................................................ 10 2.11 Git rm.................................................... 10 2.12 Git branch................................................. 10 2.12.1 Create and delete git branch................................... 11 2.12.2 Switch between branches..................................... 11 2.12.3 Git merge.............................................. 12 2.12.4 Conflicts............................................... 13 2.12.5 Removing conflicts......................................... 13 2.12.6 Create branches through older commits............................. 14 2.13 Git GUI................................................... 15 3 Git remote repository 16 3.1 Creating repository............................................. 16 3.2 Add Repository to local git........................................ 16 3.3 Push changes on repository........................................ 16 3.4 Clone repository.............................................. 17 3.5 Pull changes from repository....................................... 17 3.5.1 git remote add........................................... 17 3.5.2 git pull............................................... 17 3.5.3 git fetch............................................... 18 i

Chapter 1 Commands Summary Following is the list of commands which are used in this tutorial. Commands Local Repository sudo apt-get install git git config global user.name meher git config global user.email abc@gmail.com git config global core.editor vim git config global credential.helper cache git init git status git add. git add file1 file2 git rm cached file1 git commit -m commit message git log git log oneline git log graph git log oneline graph git diff git diff cached git diff stat git reset git reset file1 git reset hard 13802e3 git reset HEAD hard git checkout file1 git rm file1 git branch git branch branch1 git branch -d branch1 git checkout branch1 git checkout master git merge branch1 git checkout 13802e3 git checkout -b branch1 Remote repository git remote add reponame https://url_of_repo Table 1.1: Commands summary Descriptions Install git in Linux Ubuntu Set username as meher Set email as abc@gmail.com Set vim as default text-editor Cache username and password Initialize git repository File status i.e. modified and untracked etc. Add all untracked files Add (stage) file1 and file2 Remove the staged file file1 Commit stage file with commit message Show detail list of commits Show hash and commit name only Show commits in the form of graph Show online-commit in the form of graph Differences between unstaged files and previous commit Differences between staged files and Show only changed filenames (not the details) Remove all files from stage list (i.e. back to modified) Remove file1 from stage list (i.e. back to modified) Reset to previous commit with hash 13802e3 remove all changes after last commit Remove changes from non-staged file1 to previous commit Delete file1 from git (but available in previous commit Show all the branches Create branch1 Delete branch1 Go to branch1 Go to master branch Merge the brach1 to current branch e.g. master Create new branch from previous commit 13802e3 First checkout and then create branch Add remote repo with name reponame Continued on next page 1

Table 1.1 continued from previous page Commands Descriptions git remote -v Show list of added reponames git remote remove reponame Remove reponame from list git push reponame branch1 Push branch1 to reponame git push reponame all Push all branches to reponame git clone https://nameofremoterepository Clone or download remote repository git clone depth 1 https://nameofremoterepository Clone only last branch git pull reponame branchname Download and merge branchname of reponame git fetch reponame branchname Download, but not merge reponame 2 PythonDSP

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 wrong changes in the code or we are not happy with current changes, then the previous versions of codes can be restored using git. Lastly, version control systems allows to work in a group, so that different people can solve different problems and finally all the solution can be merge together. In this way, projects are more manageable and can be 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 website http://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-editor for git. For example, vim is set as default editor here, therefore vim will be open for the commands which requires some inputs from users. $ git config --global user.name meher $ git config --global user.email mkpatel@gmail.com $ 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 file can be seen using following commands, $ cd $ vim.gitconfig [user] email = mkpatel@gmail.com name = meher [core] editor = vi 3

2.3. Create project directory 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 content of this folder, $ ls -A.git $ cd.git && ls -A branches 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 status Initial commit nothing 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 status On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed).hello.py.swp hello.py nothing added to commit but untracked files present (use "git add" to track) 4 PythonDSP

Chapter 2. Git Local repository 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 are generated by editors or software e.g..pyc (python) or.so(c/c++), therefore these files should be removed from tracking 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 files e.g. automatically generated files i.e..pyc and.swp or folders i.e. build etc. These files can be added to.gitignore file as shown below,.gitignore *.swp *.pyc # sphinx-build folders build :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, as it 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 curretly and it is new to git therefore it is shown as Untracked file by status command. Status will remain the same until this 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 to add file to git as shown in below commands, $ git add hello.py $ git status Changes to be committed: (continues on next page) 5 Meher Krishna Patel

2.6. Untracked, Staged and Modified file (use "git rm --cached <file>..." to unstage) (continued from previous page) 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 displays the message that changes to be committed, new file: hello.py. Commit is used to backup the staged file as shown 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 commit requires a name so that we can identify the changes made during those commits. Commit operation can be performed as below, $ git commit -m "commit1 hello.py is added" [master (root-commit) 5e1b96b] commit1 hello.py is added 1 file changed, 5 insertions(+) create mode 100644 hello.py $ git status On branch master nothing to commit, working directory clean Since, all the changes are added to git using commit, therefore status command displays that there is nothing to commit now. Note that, if git commit is used in place of git commit -m commit1 hello.py is added, then a vim editor will pop-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") Now, use git status command and it will show that the hello.py file is modified file. Also, we can add and commit the 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 modified 1 file changed, 1 insertion(+) 6 PythonDSP

Chapter 2. Git Local repository 2.7 Git log Git log command is used to see the list of commits. Following git log commands are quite useful, $ git log commit d52401733f7cd237cd837cd362bf3e0c546aef47 Author: meher <mkpatel@gmail.com> Date: Mon Jan 30 22:13:08 2017 +0000 Commit2 hello.py modified commit 5e1b96bd7e89c94ccb1b6b85704ed23958cdff59 Author: meher <mkpatel@gmail.com> Date: Mon Jan 30 22:00:42 2017 +0000 commit1 hello.py is added $ git log --oneline d524017 Commit2 hello.py modified 5e1b96b 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 at the 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 the commits, 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 of hello.py file. Then run the git diff command to see the changes in the file as below. # hello.py print("hello 1") 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 diff diff --git a/hello.py b/hello.py index 78860b8..181ed03 100644 --- a/hello.py +++ b/hello.py @@ -3,4 +3,5 @@ print("hello 1") +print("hello 3") (continues on next page) 7 Meher Krishna Patel

2.9. Git reset (continued from previous page) $ git diff --stat hello.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 previous commit. Once hello.py is staged, then - -cached command will show the differences as displayed below, $ git add hello.py $ git diff $ git diff --cached diff --git a/hello.py b/hello.py index 78860b8..181ed03 100644 --- a/hello.py +++ b/hello.py @@ -3,4 +3,5 @@ print("hello 1") +print("hello 3") $ git diff --cached --stat hello.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 current working 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 more line in the end of hello.py as below, # hello.py print("hello 1") 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) 8 PythonDSP

Chapter 2. Git Local repository (continued from previous page) $ git add hello.py $ git status Changes to be committed: modified: hello.py $ git reset hello.py Unstaged 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, then we 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 to remove those changes. In such cases, we need to reset the header, i.e. we need to go back to previous commit, as discussed 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 see the list of commits using git log command, $ git log --oneline b78daef commit4 password added wrongly 13802e3 commit3 hello 3 is added d524017 Commit2 hello.py modified 5e1b96b 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. header 13802e3. For this use reset command as shown below, $ git reset --hard 13802e3 HEAD is now at 13802e3 commit3 hello 3 is added $ git log --oneline 13802e3 commit3 hello 3 is added d524017 Commit2 hello.py modified 5e1b96b 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. 9 Meher Krishna Patel

2.10. Git checkout 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 command can be used to remove the changes. To understand this, add one line to hello.py as below, # hello.py print("hello 1") 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 status command. 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 -s M 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.py rm 'hello.py' $ git status deleted: hello.py $ git commit -m "hello.py delete" $ git log --oneline 8c168c6 hello.py delete 13802e3 commit3 hello 3 is added d524017 Commit2 hello.py modified 5e1b96b commit1 hello.py is added Use reset command to restore the file as shown below, $ git reset --hard 13802e3 HEAD is now at 13802e3 commit3 hello 3 is added $ git log --oneline 13802e3 commit3 hello 3 is added d524017 Commit2 hello.py modified 5e1b96b 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 using git branch command, 10 PythonDSP

Chapter 2. Git Local repository $ 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 master branch. git branch -d is used to delete a branch, e.g. $ git branch diff2num $ git branch add2num diff2num * 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 add2num Switched 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 end of 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 3") x = 2 y = 3 print (x+y) Next, commit these changes as below, git status -s M hello.py (continues on next page) 11 Meher Krishna Patel

2.12. Git branch (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 can experiment 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 3 headers, whereas add2num branch contains 4 header as below, $ git checkout master Switched to branch 'master' $ git log --oneline 13802e3 commit3 hello 3 is added d524017 Commit2 hello.py modified 5e1b96b commit1 hello.py is added $ git checkout add2num Switched to branch 'add2num' $ git log --oneline 223e688 commit from add2num 13802e3 commit3 hello 3 is added d524017 Commit2 hello.py modified 5e1b96b 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 want to 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 master Switched to branch 'master' $ git merge add2num Updating 13802e3..223e688 $ git log --oneline 223e688 commit from add2num 13802e3 commit3 hello 3 is added d524017 Commit2 hello.py modified 5e1b96b 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. 12 PythonDSP

Chapter 2. Git Local repository 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 is unable 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 3") x = 2 y = 3 print (x+y) Next, commit the changes as below, $ git checkout add2num Switched 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 3") x = 2 y = 3 print (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 add2num Auto-merging hello.py CONFLICT (content): Merge conflict in hello.py Automatic 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 <<<<<<< HEAD print("hello 1 from master") ======= print("hello 1 from add2num") (continues on next page) 13 Meher Krishna Patel

2.12. Git branch >>>>>>> add2num print("hello 3") (continued from previous page) x = 2 y = 3 print (x+y) The lines between arrows, are the line which are in conflict. Also, = sign separates the line which are generating conflicts. 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 3") x = 2 y = 3 print (x+y) Finally, commit the changes and see the git log as below. $ git commit -am "Hello 1 accepted from both" $ git log --oneline 54133a4 Hello 1 accepted from both 6f0d127 Hello 1 from master 5336b3c Hello 1 from add2num 223e688 commit from add2num 13802e3 commit3 hello 3 is added d524017 Commit2 hello.py modified 5e1b96b 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 13802e3 Note: checking out '13802e3'. $ git checkout -b diff2num $ git branch add2num * diff2num master In above listing, * shows that currently git is in diff2num branch. Next, check the git log command to confirm that the new branch starts from third commit as below. $ git log oneline 13802e3 commit3 hello 3 is added d524017 Commit2 hello.py modified 5e1b96b commit1 hello.py is added Now, we can modify the code from third commit and merge those changes to master or work separately on this branch. 14 PythonDSP

Chapter 2. Git Local repository 2.13 Git GUI There are various graphical user interfaces are available e.g. git-cola, git-ext and giggle etc. Once, basic concepts are understood, then we can use those interfaces to perform various operations using mouse clicks. 15 Meher Krishna Patel

Chapter 3 Git remote repository In previous chapter, we make a local repository which stores all the changes on our local machine. To store the repository online, i.e. remote repository, we need to create an account on git-hosting-sites e.g. BitBucket (which provides 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 -v reponame 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 is discuss 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

Chapter 3. Git remote repository 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 git clone 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 repository using 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 other users 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 -v origin 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. We can 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, pull command 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. 17 Meher Krishna Patel

3.5. Pull changes from repository 3.5.3 git fetch If we do not want merge the repository immediately, then Fetch command should be used to get the remote copy 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 merge these 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. 18 PythonDSP