msml 605 - lecture 8 version controlnayeem/courses/msml605/files/08_git.pdf · system...

54
MSML 605 - Lecture 8 Version Control

Upload: others

Post on 16-Oct-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

MSML 605 - Lecture 8

Version Control

Page 2: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Introduction

■ New changes keep coming up

■ We need to keep track of the changes

■ There is some mechanism to see what changed overtime and undo certain actions

Page 3: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

System Administration

■ New software is updated, may be server updates

■ If something breaks, without version control it would be a hassle.

■ With version control you could rollback to a stable version.

Page 4: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Keeping track

■ Making copies of the work

■ Something that was removed only to be added later

■ Keeping historical copies is elementary version control (primitive).

■ Who did what and why is lost.

Page 5: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Comparing files

■ Two copies of the same code from different times

■ Eyeball them?

■ diff command

■ meld, kdiff3, vimdiff

■ patch

Page 6: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Version Control

■ Keeps track of all the versions

■ Helps retrieve past versions and who changed the files and when

■ Files are organized in repositories

■ A repository can have thousands of contributors.

Page 7: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Git

■ Creator - Linus Torvalds

■ Open source - originally created to manage development of Linux kernel

■ Distributed architecture - copies on the local as well as the remote machines.

■ Git can be used as a server or a client or both on a machine.

Page 8: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Git■ Clients can communicate with the server over http, ssh or

git own protocol.

■ Multi-platform support

■ Public servers GitHub, gitlab, etc.

■ Official Git website, https://git-scm.com/(scm - source control management)

■ Other tools like Subversion (https://subversion.apache.org/), Mercurial (https://www.mercurial-scm.org/) etc.

Page 9: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Git - Installation

■ git —versionto check if it is already installed

■ conda install gitinstallation guide - (https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) download page - (https://git-scm.com/downloads)

■ Windows comes with the environment MinGW64 to operate like Linux

■ There are some IDE’s available

Page 10: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Git commands

■ To keep track of who made the changes git config —global user.email “email”git config —global user.name “name”

■ User name and email are global for all git repositories

■ Git init initializes a repository in the current directoryls -la ls -l

Page 11: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Git directory■ git clone creates a similar directory as git init

■ Area outside the Git directory is the working directory - current version of the project

■ Create a new file or copy an existing file.

■ Add it to the staging area. git add <filename>

Page 12: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Staging area and commit■ Git directory, working tree and staging area

■ git status

■ git commit

■ File Stages: modified, staged, committed

Page 13: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Tracking■ Check status

■ Modify a file that is under version control

■ Check status again

■ Add the file to the staging area

■ Commit the changes

■ Check status

Page 14: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Git workflow

■ git log

Page 15: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

No staging area

■ Any changes to tracked filesgit commit -a -m “message”

■ HEAD alias represent the currently checked out snapshot of the respository

■ HEAD can be a commit in a different branch of a project

Page 16: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Git log

■ git log -pp for patches

■ Uses a paging tool

■ git show

■ git log —stat

Page 17: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Git working area

■ git diffdifference before commit

■ git add -p

■ git diff —staged

Page 18: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Removing and renaming files

■ Git rm <filename>

■ Git commit -m “message”

■ Git mv <old file name> <new file name>

■ Git commit -m “message”

Page 19: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Ignore some files and remove changes

■ Add them to .gitignore fileecho .abc >.gitignoreexample: https://gist.github.com/octocat/9257657

■ git checkout <filename>to undo changes made that you don’t want to add to the staging area.

Page 20: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

remove changes in staged files

■ git restore —staged <filename>to undo changes made and added to the staging area.

■ git restore <filename>

Page 21: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Wrong commit message■ Create two files

■ Add one file

■ Commit one file with the message that two files were added

■ Then add second file

■ git commit —amend to fix the message Don’t use for public repositories

Page 22: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Wrong commit - undo last commit

■ git revert HEAD

■ git log -p -2

Page 23: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Wrong commit - by commit id

■ git log -1

■ git log

■ git show

■ git revert

Page 24: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Branches

■ git branch command to list, create , delete and manipulate branches

■ git branch

■ git branch new-devlopment

■ git checkout new-development

■ git checkout -b new-branch

Page 25: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Branches

■ git checkout -b new-branch

■ Create a new file (list_numbers.py) , add and commit it

■ git log -1

Page 26: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Branches

■ git status

■ git checkout master

■ git log -2

■ ls -l

Page 27: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Branches

■ git branch

■ git branch -d new-development

■ git branch -d new-branch

Page 28: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Merging

■ git branch

■ git merge new-branch

■ git merge uses fast forward and three-way merge

Page 29: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Merge Conflicts

■ git branch

■ Modify a file and commit to the master branch

■ Checkout a branch

Page 30: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Merge Conflicts

■ Modify the same file in the new branch and commit to it

■ Now Checkout the master branch

■ Now merge the branch

Page 31: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Merge Conflicts

■ Check the status. - git status

■ See the file for the conflicts

Page 32: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Merge Conflicts

■ Keep both changes and add the file

■ Check the status

■ git commit

Page 33: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Merge Conflicts

■ git commit

■ Add a comment like, Keeping changes from both branches

Page 34: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Merge Conflicts

■ Check the commit history

■ git merge —abort to reset to the previous commit to throw away the changes.

Page 35: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Remote repository

■ Github

■ Provides public and private repositories

■ You will need an account. It is free.

Page 36: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Remote repository

■ Create a repository - test

Page 37: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Remote repository

■ New repository

https://github.com/nayeemmz/test.git

Page 38: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Clone - Remote repository

■ Clone a repository

■ A directory is created locally

Page 39: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Update local repository

■ Go to local test repository and update README file

■ Commit the changes

■ Push the changes to the remote repository

Page 40: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Check the remote repository

■ remote repository

Page 41: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Avoid Authentication

■ Use ssh or

■ Credential helper -

■ Pull the remote repository

■ Pull the remote repository again

Page 42: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Remote repository

■ Use ssh or https

■ Check the remote url

■ Show origin on remote computer

Page 43: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Remote repository

■ Remote branches

■ Check status

Page 44: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Remote repository

■ Update an commit through Github

■ Check remote repository locally

Page 45: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Syncing

■ Sync remote and local repositories

Page 46: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Remote log

■ Check the log of the remote repository

Page 47: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Missing commit

■ Check status

■ Merge the changes

Page 48: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Check the log again

■ Check status

Page 49: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Conflicts

■ Update a file locally

■ Check the changes to accept

■ Commit

Page 50: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Conflicts

■ Update the file in the remote repository on Github

■ Push the change you made locally

Page 51: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Conflicts

■ Merge first

■ Check log

Page 52: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Conflicts

■ Check the commits on origin/master

Page 53: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Conflicts

■ Correct or merge the multiple changes in the file

■ Add the file

■ Commit the changes

■ Push the changes

Page 54: MSML 605 - Lecture 8 Version Controlnayeem/courses/MSML605/files/08_Git.pdf · System Administration New software is updated, may be server updates If something breaks, without version

Conflicts

■ Check the log to verify