git and github for collaborative code development …...$ git config --global...

23
Git and GitHub for Collaborative Code Development and Distribution https://git-scm.com/ http://xkcd.com/1597/

Upload: others

Post on 07-Jul-2020

31 views

Category:

Documents


0 download

TRANSCRIPT

Git and GitHub for CollaborativeCode Development and Distribution

https://git-scm.com/

http://xkcd.com/1597/

vers1_2016_2.c,vers1_2016_1a.c

Git: A Version Control System (VCS) that lets you:• Track changes in software projects• Give creators the power to view previous

versions of files and directories• Develop speculative features without disrupting

the main development (branches)• Securely back up the project and its history• Collaborate easily and conveniently with others• Deploy applications more easily

https://www.learnenough.com/git-tutorial

1) Start by Cloning an ExistingRemote Repo

● Clone a project from a remote repository into a new folder in thecurrent directory

$ git clone [email protected]:/home/pi/git/repositories/AutoMouseWeight

● ALL the revisions from the remote repository (not just the mostrecent revision) are now in the current directory

● Git remote commands (like clone) work over ssh so you need anaccount on remote host with permission to access repo

● If using SSH, the remote host does not even need to run git.– Though it makes it easier to, e.g., set permissions for new repos

– No such thing as a “git Server” in same sense as SVN server vs client

2a) Start by Setting Up a New LocalGit Repo ...

● Assuming you have an existing folder full of code:

– $ cd myLocalFolder

– $ git init

$ Initialized empty Git repository in/Users/jamie/Documents/WaveMetrics/MurphyLabGit/testrepo/.git/

– $ git add -A

– $ git commit -m “this is a git commit message”

● You can use git for tracking changes and making brancheswithout a remote repository

● Even share code if your computer allows remote login over ssh

● “Everything Is Local”

2b) and Linking it to a New EmptyRemote Repo

● Remote login to repository

– $ sh [email protected]

● (optional) Make new user for git

– $ sudo adduser git

– $ sudo passwd git

● Make directory for repositories

– $ mkdir repos

● Start an empty repository called testrepo

– $ git init –bare testrepo

● Logout of remote server

– $ logout

● Set remote as master origin for push

$ git remote add origin [email protected]:/home/pi/git/repositories/testrepo

$ git push origin master

Initialize a Local Repository

$ mkdir testrepo

$ cd testrepo

$ git init

$ Initialized empty Git repository in/Users/jamie/Documents/WaveMetrics/MurphyLabGit/testrepo/.git/

● You can use git for tracking changes and makingbranches without a remote repository

● Even share code if your computer allows remote loginover ssh

● “Everything Is Local”

Clone or Init Result

544B 6 Oct 19:00 .git

416B 3 Aug 15:21 .gitignore

4.8K 21 Jul 16:55 HX711.py

1.7K 21 Jul 16:55 Scale.py

3.3K 18 Jul 19:56 compileNotes.txt

612B 26 Sep 18:07 cppExt

• Sequential revisions stored as differences from previousfiles, so repository size is kept small

.git folder contents

7B 26 Sep 16:22 COMMIT_EDITMSG

0B 6 Oct 16:34 FETCH_HEAD

23B 19 Jul 11:47 HEAD

41B 26 Sep 18:07 ORIG_HEAD

68B 19 Jul 11:47 branches

330B 19 Jul 11:47 config

73B 19 Jul 11:47 description

374B 19 Jul 11:47 hooks

2.5K 6 Oct 16:34 index

102B 19 Jul 11:47 info

136B 19 Jul 11:50 logs

5.1K 26 Sep 18:07 objects

170B 19 Jul 11:51 refs

156B 6 Oct 16:34 sourcetreeconfig

Git ignore#Compiled Code #Packages #OS generated

*.com *.7z .DS_Store

*.class *.dmg ._*

*.dll *.gz .Spotlight-V100

*.exe *.iso .trashes

*.o *.jar ehthumbs.db

*.so *.tar #Logs and Database

*.pyc *.zip *.log

*.pyo # config files *.sql

__pycache__ *.jsn *.sqlite

#build folder

build/

Identify Yourself

You will be asked to do this before first commit

$ git config --global user.name "Your Name"

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

• This info will be seen by other developers, will identify yourchanges (commits)

• Need to do this before you can commit changes

$ git config --global –lis

• Shows configuration, including user and email

$ git config --global color.ui auto

$ git config --global core.editor "nano"

Add new files to local repository

$ git add .

$ git add -A

● Either one tells git to track all files in thedirectory

$ git add testfile adds a single file to be tracked

● A text file named .gitignore tells git to NOT addcertain types of files when adding all files

Commit Changes to Local Repo

$ git commit -m "fixed daylight savings time"

● Need to specify a commit message. This will besaved as part of the log

● Can specify a single file, or folder

$ git commit <myfilename> -m “just this file”

$ git commit . -m “just this folder”

● Remember, commits only affect the localrepository

Push and Pull to/from remote repo

$ git push

● Synchs remote repo to latest commit in local repo

● Now others can pull your changes from remote repo

$ git pull

● updates your local repository with files from centralrepository that have been changed

● Pull before you push

Lifecycle of a file

LifeCycle of a Project

● Revisions – HEAD● Every commit is a revision● Most recent commit is HEAD

● Branches – master● Master is tree’s “trunk”● Branches come off the trunk

● Remotes/origin/master● pushes and pulls are directed to this location

Track Revisions

● Each commit saves a new revision

– Identified by a long hash

– Most recent revision is referred to as HEAD

– Git log shows previous commits

● git log -10 --pretty=oneline

commit8f4e4936077c21a8cd0f7322268aa55ef0a9dd5b wrotehello world.c

• Use git tag to give a revision a nicer name to refer by

$ git tag 1.0 master

Revisit Previous Revision

● Checkout makes working directory match a commit – without altering HEADrevision

$ git checkout 1.0

You are in 'detached HEAD' state. You can look around, make experimental

changes and commit them, and you can discard any commits you make in this

state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may

do so (now or later) by using -b with the checkout command again. Example:

git checkout -b <new-branch-name>

HEAD is now at f3b913c... Added code for DLS

• Go back to master, and nothing is lost (Perhaps making a new branch to savechanges)

$ git checkout master

Revert to Previous Revision

$ git revert 9bb658b● undoes the changes introduced by the commit and appends a new

commit with the result

$ git reset --hard

● Resets working directory to previous commit, undoing any changes

$ git reset –hard <commit>

● Obliterates everything since the named commit

$ git fetch --all

$ git reset --hard origin/master

● When you have conflicts with remote, this deletes all local changes

Branches● Always at least one branch called master

– It is the trunk of the tree others branch off of

● When should you branch

– use a branch to save a copy of your code that’sready to release before starting V. 2.0

– Use a branch to add a new feature

● Make a release branch from master:

$ git branch release1 master

$ git checkout release1

Branch and Merge

When should you branch

– use a branch to save a copy of your code

– Use a branch to add a new feature whiledevelopment continues on trunk

$ git branch funkyexp

$ git checkout funkyexp

$ git checkout master

$ git merge funkyexp

Git Resources

● https://git-scm.com/book/en/v2– The git book

● https://www.learnenough.com/git-tutorial

– One of a series of good tutorials

● https://www.linux.com/learn/how-run-your-own-git-server– Step by step instructions to set up a git server

– Explains a bit about using RSA-key authorization

● https://apps.ubuntu.com/cat/applications/gitg/

● Graphical git repository viewer– Visual diff and merge tool to compare different versions

Other Version Control Systems

Subversion

– Centralized server model, only working copy code is local

– Widely used

– Well supported on Linux, Mac, AND Windows● Good Quality GUI tools● Support in your IDE (Xcode, Visual Studio, Eclipse)l

● Mercurial

● Bazaar

GitHub

● A web-based front end for git

● A place to host a remote repository– Free account for open source projects (your code

available to everyone)

– Paid model for private repositories

● https://guides.github.com/activities/hello-world/– Tutorial to make a repository hosted on GitHub