Git And GitHub - Cornell University

2y ago
27 Views
3 Downloads
800.31 KB
40 Pages
Last View : 1m ago
Last Download : 1m ago
Upload by : Adalynn Cowell
Transcription

Git and GitHubCS 4411Spring 2020

If that doesn’t fix it, git.txt contains the phone number of a friend of mine who understands git. Just wait through a few minutesof “It’s really pretty simple, just think of branches as.” and eventually you’ll learn the commands that will fix everything.

Outline for Today Git overview Git vs. GitHub Basic Git commandsConflicts and mergesBranchesRecovering from errors

Git ModelRemote repoHeadACommitsBCCommit s/mt.cRepo contentsOriginLocal repoHeadABCMakefilesrc ---apps ---earth ---grass ---process.c ---process.h ---lib“Working tree”

Making a CommitRemote repoHeadABCDpushOriginLocal repoHeadABCDCommit efile.apps

Git with GitHubGitHub repo: etremel/egosGitHub repo: jsmith/egosHeadHeadForkABCDOriginABCEOriginLocal repoLocal repoHeadHeadABCDABCE

Outline Git overview Git vs. GitHub Basic Git commandsConflicts and mergesBranchesRecovering from errors

Getting Started with Clone git clone: Create a new local repository by copying a remote repo git clone t64-egos.gitProtocolServerPath to repository on server Result: New folder named “ejt64-egos” in current directory,containing new git repo Contents identical to repo on server Origin set to 64-egos.git

HTTPS or SSH? git clone t64-egos.git Easy to start, no setup required Must enter username and password every time you pull or push git clone egos.git Once set up, no username or password required Need to create an SSH key on yourcomputer and add it to your CornellGitHub account

Add and Commit1.2.3.4.Make changes to filesChoose some changed files that you’re ready to “publish”git add the changed filesgit commit and write a message /egos /egos /egos /egos vimvimgitgitsrc/apps/mt.csrc/grass/process.cadd src/apps/mt.ccommitCommit E:/src/apps/mt.c

Pull and Push At first, a commit is only on your local repo git push copies commits to the “origin” remote repo git pull downloads commits from origin and applies themOrigin repoABHeadCDPartner’s repoLocal repoHeadABCDpushpullHeadABCD

Understanding Git Status /egos git statusOn branch masterYour branch is up to date with 'origin/master'Current branchWhether you haveunpushed commitsChanges to be committed:(use "git reset HEAD file " to unstage)modified:src/grass/process.cChanges you haveadded with git addChanges not staged for commit:(use "git add file " to update what will be committed)modified:src/grass/disksvr.cUntracked files:(use "git add file " to include in what will be committed)src/apps/myprogram.cgit knows a file haschanged, but youhaven’t added it yetNew files you have notyet added in any commit

Git Status After a Commit /egos git statusOn branch masterYour branch is 1 commit behind 'origin/master'You made a commit,but haven’t pushedChanges not staged for commit:(use "git add file " to update what will be committed)modified:src/grass/process.cAfter your last commit,you continued editingthis fileUntracked files:(use "git add file " to include in what will be committed)src/apps/tagssrc/grass/tagsThese files still haven’tbeen added to any commit

Ignoring Files You’ll Never Add Some files you never wantto commit: ctags files,compiled output, LaTeX auxfiles Git will keep bothering youabout them in git status Add a file named.gitignore to the rootof your repo, and then addit to a commit /egos cat .gitignore# ctags filestags# LaTeX junk*.aux*.log*.bbl# The debug log directorylogs/# Object files in the build dirbuild/**/*.o /egos nges/gitignore

Diff: What Am I Committing? /egos git diffFirst file withdiff --git a/src/lib/queue.c b/src/lib/queue.cchangesindex c638853.19106c3 100644Line number--- a/src/lib/queue.cskipped to b/src/lib/queue.c@@ -19,7 19,7 @@ struct element {void queue init(struct queue *q){Deleted from originalq- first 0;(last commit)q- last &q- first;q- nelts 0;Added in current q- num elements 0;state of file}/* Put it on the wrong side of the queue. I.e., make it the next@@ -34,7 34,7 @@ void queue insert(struct queue *q, void *item){}e- next q- first;Skip ahead againq- first e;to line 34

Diff Details /egos git diff src/grass/process.c Shows differences only for that file /egos git diff /egos Why does it give no results? I know I made changes! Answer: you have already git added your changes /egos git diff --stageddiff --git a/src/lib/queue.c b/src/lib/queue.cindex c638853.19106c3 100644--- a/src/lib/queue.c b/src/lib/queue.c@@ -19,7 19,7 @@ struct element {

Un-Adding and Deleting Oops, I didn’t mean to add that file! /egos git add src/lib/queue.c /egos git reset HEAD src/lib/queue.c Telling git you want to delete myprogram.c: /egos git rm src/apps/myprogram.c /egos git statusOn branch masterYour branch is up to date with 'origin/master'Changes to be committed:(use "git reset HEAD file " to unstage)deleted:src/apps/myprogram.c

Renaming Telling git you want to rename myprogram.c: /egos git mv src/apps/myprogram.c src/apps/newname.c /egos git statusOn branch masterYour branch is up to date with 'origin/master'Changes to be committed:(use "git reset HEAD file " to unstage)renamed:src/apps/myprogram.c - src/apps/newname.c Otherwise, git will think you deleted myprogram.c, and bothmyprogram.c and newname.c will end up in the repo

Outline Git overview Git vs. GitHub Basic Git commandsConflicts and mergesBranchesRecovering from errors

Concurrent ChangesGitHub repoHeadABCDpushMy local repoPartner’s local repoHeadHeadABCDABCE

Possible Outcomes No conflicts, just merge /egos git pull# Editor pops upMerge made by thesrc/lib/queue.c1 file changed,'recursive' strategy 14 branch ------Merge'master' of 7 insertions ( ), 7 deletions (-)# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commitPartner’s local repoABCDHeadEM

Possible Outcomes Conflicting changes to the same file(s) /egos git pullAuto-merging src/lib/queue.cCONFLICT (content): Merge conflict in src/lib/queue.cAutomatic merge failed; fix conflicts and then commit the result /egos git statusOn branch masterYour branch and 'origin/master' have diverged,and have 1 and 1 different commits each, respectively. Unmerged paths:(use "git add file ." to mark resolution)both modified:src/lib/queue.c

Conflict File SyntaxInside queue.c:void queue add(struct queue *queue, void *item){struct element *e calloc(1, sizeof(*e));e- item item;e- next 0; HEAD*queue- last e;queue- last &e- next;queue- nelts ; *q- last e;q- last &e- next;q- num elements ; 354a72479204de581ffa83551843b92e585506b8}Merged lineswith no conflictsHEAD means thesechanges are in yourlocal HEAD commitYour local version ofconflicting linesOrigin repo’s versionof conflicting linesHash of commit fromorigin that contains theseconflicting changes

Finishing the Merge Edit the file to choose a single version of the conflicting lines Make sure to delete the and lines! When you have resolved the conflict: /egos /egos # Write /egos git add src/lib/queue.cgit commita message for the merge commitgit push

Outline Git overview Git vs. GitHub Basic Git commandsConflicts and mergesBranchesRecovering from errors

Git Branches Track differentsequences of commitsdiverging from commonstarting point Make explicit whathappened already whenyou & your partner madeconflicting commits Let you choose when tomergenewfeatureABCDEJKmaster

Branch Basicsthreaddevelop /egos git checkout -b thread-developSwitched to a new branch 'thread-develop' Creates a new branch, pointing to samecommit as masterACBmaster /egos git add src/apps/mt.c /egos git committhreaddevelop New commit goes on thread-develop,master still points to last commitDABCmaster

Branch Basics /egos git checkout masterSwitched to branch 'master'Your branch is up to date with 'origin/master' Changes your repo’s active branch tomaster – you’ll notice the changes youmade to mt.c are gone /egos git add src/grass/process.c /egos git commit This commit goes on master, and theHEAD pointer for master moves upthreaddevelopDABCmasterJ

Pushing and Pulling You want to push the commit on your new branch to GitHub: /egos git checkout thread-developSwitched to branch 'thread-develop' /egos git pushfatal: The current branch thread-develop has no upstream branch.To push the current branch and set the remote as upstream, usegit push --set-upstream origin thread-develop OK, do what it says After this point, git push will just work

Why Was That Necessary? checkout -b only creates a branch onyour local repo GitHub repo won’t add a commit to aPushbranch that doesn’t existthreaddevelopLocal repo“add this commit tothread-develop”GitHub repo?ABCmasterDOriginABCJmaster

Pushing and Pulling You want to switch to a new branch your partner created /egos git checkout thread-developerror: pathspec 'thread-develop' did not match any file(s) known to git Why didn’t that work? /egos git pullNow the local repoRemote: Enumerating objectsknows about the branch From github.coecis.cornell.edu:etremel/egos* [new branch]thread-develop - origin/thread-developAlready up to date. /egos git checkout thread-developBranch 'thread-develop' set up to track remote branch 'thread-develop' from 'origin'Switched to a new branch 'thread-develop'

Merging Branches Eventually, you’ll want tomerge your branch backinto master First, switch your currentbranch to masternewfeatureABCDEJKmaster /egos git pull /egos git checkout master Then, merge the featurebranch into master /egos git merge new-feature /egos git pushMResolve any merge conflicts, sameas when you pull and see conflictsPublish the merge commit, soeveryone else can see the merge

Repository Design with Branches In many software teams, master is the “stable” branchEach new feature or bug fix is developed on its own branchWhen tested and safe, branch is merged into masterWhile developing on a branch, merge from master into yourbranch to get updates and bugfixesABDEFCJKLM

Outline Git overview Git vs. GitHub Basic Git commandsConflicts and mergesBranchesRecovering from errors

Some Useful Incantations Oops! I made a commit but then made one small change# Do this BEFORE you push git add changed-file.c git commit --amend --no-edit Oops! I want to edit the message on the commit I just made git commit --amend# Edit the message in your editor Oops! I deleted a file and didn’t mean to git checkout path/to/file.c

Branch-Related Problems I just made a commit to master, but I should have put it on a newbranch instead# # # Create a new branch with the same state as mastergit branch new-branch-nameRemove the latest commit from mastergit reset HEAD --hardSwitch to the new branch, which still has the commitgit checkout new-branch-name

Branch-Related Problems I just committed to the wrong branch – I thought I was on thequeue-develop branch but I’m on the experiments branch# # # Undo the last commit, but leave files changedgit reset HEAD --softStash the changed files, then move to the right branchgit stashgit checkout queue-developgit stash popCommit the changes on the correct branchgit add queue.c queue.hgit commit

The Unnecessary Merge Make changes, commit, push then realize your repo is stale git push! [rejected]master - master (fetch first)error: failed to push some refs to 'https://github.com/etremel/egos.git'hint: Updates were rejected because the remote contains work that you dohint: not have locally. This is usually caused by another repository pushinghint: to the same ref. You may want to first integrate the remote changeshint: (e.g., 'git pull .') before pushing again. Upon pull, you’re prompted to create a merge commit git pullMerge branch 'master' of https://github.com/etremel/egos.git# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit

The Unnecessary Merge If I had known to pull first, I wouldn’t have to merge! First, ensure the merge aborts without making a merge commit Either due to genuine conflict, or by deleting the merge commitmessage and saving (empty commit message aborts the commit) Then: gitgitgitgitgitgitgitreset --hardreset HEAD stashpull # no merge this timestash popadd foo.c bar.c # etccommitDiscard changes made by mergeUndo your last commit, leaving files changedStash your changes, then pull againRe-do your commit on the new head

Further Reading Atlassian Git Tutorials:https://www.atlassian.com/git/tutorials Detailed documentation on every command:https://git-scm.com/docs Happy Git with R’s “Useful Git tro.html Oh Shit, Git!?! (source of my error-recovery examples):https://ohshitgit.com/

Telling git you want to delete myprogram.c: /egos git add src/lib/queue.c /egos git reset HEAD src/lib/queue.c /egos git rm src/apps/myprogram.c /egos git status On branch master Your branch is up to date with 'origin/master' Changes to be committed: (use "git reset H A

Related Documents:

13 Features in EGit 0.8 Supported Partially supported Not yet supported * planned for 0.9 in September ’10 git init / git clone git add git status git commit git di! git fetch git log git merge* git rebase git remote git pull git push git stash* git branch git tag git checkout git config* git format

Pro Git professional version control Home Book Blog About Support Us This is an in-progress translation. To help translate the book, please fork the book at GitHub and push your contributions. Git Git Git Git Git git init.git Git .git git add Git git add *.c git add README .

Using Git Getting a Git repository git init Create an empty Git repository in the current directory. By default it will have one branch named master. git clone url Clone the Git repository from url. This may be over HTTP, SSH, or the Git protocol, or it may be a path to another local repository. Both of these operations will create a working copy.File Size: 1MB

Using Git Getting a Git repository git init Create an empty Git repository in the current directory. By default it will have one branch named master. git clone url Clone the Git repository from url. This may be over HTTP, SSH, or the Git protocol, or it may be a path to another local repository

Introduction Basic Git Branching in Git GitHub Hands-on practice Git: General concepts (II/II) I clone: Clone remote repository (and its full history) to your computer I stage: Place a le in the staging area I commit: Place a le in the git directory (repository) I push: Update remote repository using local repository I pull: Update local repository using remote repository

Make a local copy of the server repository. git clone remote Url 3. Local changes Git add Add a file to staging (Index) area git add Filename Add all files of a repo to staging (Index) area git add* Git commit Record or snapshots the file permanently in the version history with a m

1 Pro Git Scott Chacon Základy práce se systémem Git / Větve v systému Git / Git na serveru / Distribuovaný charakter systému Git / Nástroje systému Git / Individuální

Grade 2 ELA Curricular Frameworks with ELL Scaffolds . Grade 2 Unit 2 Reading Literature and Reading Informational Unit 2: RL.2.1, RI.2.1, and WIDA Standards . Reading Literature and WIDA Standards Reading Informational Text and WIDA Standards Critical Knowledge and Skills WIDA Criterion RL.2.1. Ask and answer such questions as who, what, where, when, why, and how to demonstrate .