19
The ECE297 Quick Start Guide Series Git “It is easy to shoot your foot off with git, but also easy to revert to a previous foot and merge it with your current leg.” — Jack William Bell “I name all my projects after myself. First Linux, now Git.” — Linus Torvalds 1 What is “Version Control”? Version control 1 describes a software tool that manages the source code of a project being developed by one or more programmers. The core objective is for it to be easy for multiple people to work on a project and not get in each others’ way. This is typically provided via a history of changes made to all files in a project and the ability to review and analyze this history. Real-world software projects have a lifetime of years or decades, and usually dozens of programmers working on the same project, which may grow to include millions of lines of code and thousands of files. It becomes effectively impossible to make changes to such large projects without some sort of automation – and so, version control. Even for your project in ECE297, you will soon find that using version control allows you to efficiently collaborate with your group members and effortlessly keep track of changes made to your code base. There might be the occasional complication, but don’t give up on it! The TAs during tutorials and though Piazza will be glad to help you understand and fix any problems. 2 Git, At a High Level All version control systems have a few basic actions: get the latest changes from your colleagues; make changes to your local copy of the project; package the changes for sharing; and, share your changes with your colleagues. Some version control systems combine some of these actions, but Git is very flexible, allowing you to do each of these actions separately.In Git terminology, the ‘get’ action is known as ing, the ‘make changes’ action is just editing local files, the ‘package’ action is known as ting, and the ‘share‘ action is known as ing. With four basic actions comes four basic ‘locations’ for your changes, and four commands to move changes from one place to another, visualized in Figure 1. Three of the four commands in the figure have been mentioned, save for . This command selects changes from your local files for to package. Also not mentioned so far is this directory – a hidden folder at the root of your local repository that stores information about it for Git to use. The remainder of this guide will walk you through actual typical usage of these commands – and more! – via completing some tasks of Milestone 0, then moving on to how to work with others. 1 Also known as revision control, or source control 1

ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

The ECE297 Quick Start Guide Series

Git

“It is easy to shoot your foot off with git, but also easy to revert to a previous foot and merge itwith your current leg.”

— Jack William Bell

“I name all my projects after myself. First Linux, now Git.”— Linus Torvalds

1 What is “Version Control”?

Version control 1 describes a software tool that manages the source code of a project being developedby one or more programmers. The core objective is for it to be easy for multiple people to workon a project and not get in each others’ way. This is typically provided via a history of changesmade to all files in a project and the ability to review and analyze this history. Real-world softwareprojects have a lifetime of years or decades, and usually dozens of programmers working on thesame project, which may grow to include millions of lines of code and thousands of files. It becomeseffectively impossible to make changes to such large projects without some sort of automation – andso, version control.

Even for your project in ECE297, you will soon find that using version control allows you toefficiently collaborate with your group members and effortlessly keep track of changes made to yourcode base. There might be the occasional complication, but don’t give up on it! The TAs duringtutorials and though Piazza will be glad to help you understand and fix any problems.

2 Git, At a High Level

All version control systems have a few basic actions: get the latest changes from your colleagues;make changes to your local copy of the project; package the changes for sharing; and, share yourchanges with your colleagues. Some version control systems combine some of these actions, butGit is very flexible, allowing you to do each of these actions separately.In Git terminology, the ‘get’action is known as pulling, the ‘make changes’ action is just editing local files, the ‘package’ actionis known as committing, and the ‘share‘ action is known as pushing.

With four basic actions comes four basic ‘locations’ for your changes, and four commands tomove changes from one place to another, visualized in Figure 1. Three of the four commands in thefigure have been mentioned, save for add. This command selects changes from your local files forcommit to package. Also not mentioned so far is this .git directory – a hidden folder at the root ofyour local repository that stores information about it for Git to use.

The remainder of this guide will walk you through actual typical usage of these commands –and more! – via completing some tasks of Milestone 0, then moving on to how to work with others.

1Also known as revision control, or source control

1

Page 2: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

ECE297 Communication and Design Winter 2019

WorkingDirectory Staging Area .git Directory

RemoteRepository

add commit push

pull

Figure 1: “stages” diagram, with commands that move changes between each stage

3 To Finish Milestone 0

! Before proceeding, be sure to have finished the Milestone 0 handout up to whereyou are instructed to read this guide.

3.1 Setting Up Git

Now that you have a basic understanding of some of Git’s concepts, lets move on to actually usingthe commands. All Git commands are of the form git sub-command --command-options They also allaccept a --help option (exit by pressing q). Finally, running git --help offers a quick listing of theavailable commands, only some of which will be covered here.

Before we get started, lets set your name and email properly. Run the following commands, withthe place-holders replaced with your info.

1 > git config --global core.editor nano # set the editor to a simple one (optional)2 > git config --global user.name "Your Name"3 > git config --global user.email "[email protected]"

3.2 What Changes Have I made?

Change directory to your Milestone 0 code,2 and run git status

1 > cd ~/ece297/work/milestone02 > git status3 On branch master4 Changes not staged for commit:5 (use "git add <file>..." to update what will be committed)6 (use "git checkout -- <file>..." to discard changes in working directory)7

8 modified: main/src/main.cpp9

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

This status tells you several pieces of information, and is very useful to get a quick glimpse ofthe current state of your local repository. It is always safe to run, and as you can see from the

2Git can be used from any directory in your local repository, but the commands in this guide assume you are here

Page 2 of 19

Page 3: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

ECE297 Communication and Design Winter 2019

output, offers suggestions for how to accomplish common tasks. If you are doing something and notsure what to do next, try running git status

Let’s look at the output. First, it tells you that you are on a ‘branch’ 3 called ‘master’. Branchescan be thought of as a name for a sequence of commits, and git allows for many of these, butcovering them is beyond the scope of this guide. Next, it gives you a summary of the changes thathave been made to files in this repository. Specifically, these are the differences between the WorkingDirectory and the Staging area. Finally, it would tell you what you have added to the Staging Area,but at this time, there is nothing.

Lets say you want to see exactly what changes you made. Run the command1 > git diff

You should now see a line-by-line detailed list of changes to the working directory. Press q to exit,or h for help.

3.3 Making A Commit & Sharing It With Your Colleagues

You may have noticed the suggested commands printed by git status - let’s try one of them now.Add the changes you made to main.c to the Staging Area by running

1 > git add main/src/main.cpp

Now run git status again. It should now report that your main.c is to-be-committed. Thismeans that the changes to it have been moved to the Staging Area. Note that a file can havechanges in the Staging Area and not, in which case it will show up in both lists. Only the changesin the Staging Area will be committed. The more official name for this Staging Area is the “Index”.We’ll use this term from now on.

Try running git diff again. What do you see? Recall that by default it shows the differencebetween the Working Directory and the Index. Now that we’re almost ready to make a commit,it’s a good idea to know what exactly is going to go into the commit. This can be done by addingan option to the diff command: take a good look at the changes you have added to the Index, byrunning

1 > git diff --cached

We’re almost ready to make a commit, but let’s talk about commit messages for a moment. Agood commit message has a brief and meaningful first line (think of it as an email subject4) Ideally,you should commit often enough so that it is easy to come up with a terse subject. If you’d like tocomplement your subject with a body, see Section 8.1.

Now, lets make the commit.1 > git commit -m "Modified main.cpp by computing a new center of mass with2 a new polynomial."

Since you’ve made a commit, let’s share it with your colleagues.5 This is also necessary forsubmitting, and to keep things simple, always push right after committing.

3yes, of a tree data structure, though the branches are allowed to ‘merge’ back together4Git was originally developed for use with the Linux kernel’s source code, and that community generally sends

commits around by email, so the commit message is intended to literally be an email subject and body.5Imaginary ones, for this milestone

Page 3 of 19

Page 4: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

ECE297 Communication and Design Winter 2019

1 > git push

If that command succeeded, then you have published the commit you just made to the RemoteRepository.

Up to here is sufficient to complete the Milestone 0 work, but is insufficient forworking with others for the rest of the semester, hasn’t mentioned any time-savingtricks, and hasn’t provided the answers to all the questions a TA might ask you.

4 Essential Extras

4.1 Viewing Old Commits

Run this command to see the id (long hex numbers) and message of the commit you just created,as well as the same information for all previous commits (press q to exit).

1 > git log2 commit af4297fe811a1c2ae67f796d6caefb8f474ca4353 Author: My Name <[email protected]>4 Date: Fri Nov 23 16:14:43 2018 -05005

6 also print the first moment of x+2y^2+3z^3 over [0,5)x[0,5)x[0,5)7

8 As requested by the handout. Passes tests for correct output now.9 Created a new Polynomial, called compute_center_of_mass on it,

10 and printed using same settings as first 1st moment.11

12 commit d6bac6e26bae3bac4eaa04e8fabd128107fe389e13 Author: My Name <[email protected]>14 Date: Fri Nov 23 15:49:13 2018 -050015

16 Initial Milestone 0 commit by /cad2/ece297s/.public/bin/ece297init for namemy1

To view a file as it was committed at a particular commit, use show (press q to exit):1 git show d6bac6:./path/to/file.c # use the ID you are interested in. The first 4-8 chars is enough.

To see the difference between the current commit and another, use diff again.1 git diff d6bac6 # use the ID you are interested in. The first 4-8 chars is enough.

4.2 Checkout

Consider a situation where you want to see how the state of the code was at a certain commit (aftercommitting and pushing new changes). This can be achieved by

1 > git checkout <commit_ID> # Replace <commit_ID> by the desired ID from git log

You can also do this for only certain files or directories by:1 > git checkout <commit_ID> path/to/file/or/dir # Replace <commit_ID> by the desired ID from git log

Page 4 of 19

Page 5: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

ECE297 Communication and Design Winter 2019

While you can commit new changes from this state, we would recommend not to do so as thiswould create what we call branches, which are outside the scope of this course and may lead tomessy behaviors, especially when working with others.

To go back to the latest version, type1 > git checkout master

4.3 Reverting committed files

In case you want to cancel certain commits you made where you discovered that the new changeshas bugs for example, you can use the following command:

1 > git revert <commit_ID>

The revert command creates a new commit that inverts the changes in the commit specified by<commit_ID>. From this state, you can continue working on your code as if <commit_ID> didn’thappen.

You can also specify files and/or directories to be the only ones to revert by adding <path/to/file/or/dir>to the above command.

4.4 Reverting uncommitted changes

To permanently obliterate all uncommitted changes to a file, or all files in a directory (recursively),run:

1 > git reset path/to/file/or/dir # just in case you added it2 > git checkout path/to/file/or/dir

5 Working With Others

5.1 Getting Your Colleagues’ Changes

Consider the situation where your colleague has been working at the same time as you, has madeand pushed a commit, and you would like to update your working directory with their changes. Toaccomplish that, and update your local repository and working directory, you can run the following

1 > git status # do I have any uncommitted changes?2 > git commit # commit if you have any3 > git pull -r

This performs a special type of merge called a “rebase”, which makes sure that your project historyis very simple. Explaining exactly what it means (or how Git works in general) is beyond the scopeof this guide.

5.2 Conflicts After Pulling

Consider another situation where your colleague happens to make commit around the same timeas you, but pushed it before you did.What should happen when you try to push? Well, the push

Page 5 of 19

Page 6: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

ECE297 Communication and Design Winter 2019

command will fail!6 You can use the same sequence of commands as above (Section 5.1) to resolvethis situation. However, the pull -r may fail when the commit you made conflicts with theirs. Whenthis happens, git brings you into a special mode that allows you to fix your commit, like in thefollowing session.

1 > git pull -r2 First, rewinding head to replay your work on top of it...3 Applying: I, your colleague, changed file.c. This is the commit message4 Using index info to reconstruct a base tree...5 M path/to/file.c6 Falling back to patching base and 3-way merge...7 Auto-merging path/to/file.c8 CONFLICT (content): Merge conflict in path/to/file.c9 error: Failed to merge in the changes.

10 # oh, no. There’s a conflict between our changes in file.c11 > cat path/to/file.c # let’s take a look at that file12 more code13 more code14 <<<<<<< HEAD15 code added by you16 =======17 code added by colleague18 >>>>>>> I, your colleague, changed file.c. This is the commit message19 more code20 more code21 > nano path/to/file.c # resolve all the merge conflicts by editing the file to look like you want

it to22 > git add path/to/file.c # mark it as resolved23 > git status # are all the conflicting files marked as resolved?24 > git diff --cached # make sure everything looks good. Make sure to check if the code builds and

works.25 > git rebase --continue # tell git that you are done

If you’re not sure what to do, run git status for hints. If you are really confused, and justwant to start over, run git rebase --abort and start again from the top, at git pull -r This processactually works with multiple non-pushed commits, but for simplicity, try to avoid that situationby always pushing after committing. If that is the case though, then you may have to resolvemultiple conflicts and run git rebase --continue multiple times. You can also use the Netbeansconflict resolution tool, though you will have to invoke it yourself. (Section 9.7)

6 Summary, Tips & Tricks

If you are interested is using Netbeans’ Git integration, see Section 9

.6What are you trying to do? overwrite your colleague’s work? :)

Page 6 of 19

Page 7: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

ECE297 Communication and Design Winter 2019

6.1 Command Summary

git command Summaryadd file Add changes from a given file to the Indexreset file Remove uncommitted changes to a given file from the Indexreset Remove all uncommitted changes from the Indexcommit Package the changes in the Index together, with a messagerevert c0mm1t Revert changes of a commit from current versionpush Share your commits to the Remote Repositorypull -r Get commits that your colleagues have sharedrebase --continue Tell git that I’m done fixing this commitlog Look at the commit historyshow c0mm1t View a commitcheckout c0mm1t Change to the given commit idcheckout master Go back to the most recent version you have in the Local Repository

6.2 Tips

git command Explanationgit add . add accepts directories, so this is a quick way to add all files in a the

current directory, and all subdirectories, recursively.git add -u This adds all files that have modified status. Does not add new files –

you still have to add them file-by-file, or use git add .

git add -up Interactively adds all modified files to the Index. The -p option alsoworks for several command like stash, reset and checkout.

git commit -a Just like running git add -u then git commit, but in one command.Note, there’s no opportunity to run git diff --cached

git blame file See who changed what lines of a file, when, and what commitgit remote -v list information about the remote repositorygit clone url create a new working copy of the repository at the given url or path.

Note: the remote repository for this new working copy is the reposi-tory at the url given, so use the url/path from git remote -v in therepository created in your home directory by ece297init

git init create a new, empty, git repository in the current directorygit config --global

alias.st "status"

Now, you can type git st instead of git status (one line)

git config --global

alias.dc "diff --cached"

Now, you can type git dc instead of git diff --cached (one line)

git rm file Deletes the file and adds the "removal" to the Index. (It’s difficult torefer to deleted files)

git mv src dest Renames the src file to dest, and adds this to the Indexgitk --all Visualizes your local git repository

Page 7 of 19

Page 8: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

ECE297 Communication and Design Winter 2019

git citool A graphical tool for making commits. Makes it easy to only commitparts of a file.

6.3 Flows Described In This Guide

The flows described in this document are good practices for using git with a single branch. Use thissection as a quick reference for them. They will work well enough for your small ECE297 team andproject, but is likely not exactly what will be used on a project with more people, say, at a companythat uses Git. Git is a powerful and flexible tool, of which we have only touched the surface.

6.3.1 Making A Commit

(Section 3.3, plus a tip from Section 6.2)1 > git add -u # add all modified files2 > git add path/to/file.c # add any new files3 > git diff --cached # look at the changes4 > git commit # package with a message5 > git push # share it with your colleagues

6.3.2 Getting Your Colleagues’ Changes

(Section 5)1 > git status # do I have any uncommitted changes?2 > git commit -m "work in progress" # commit any changes if you have some3 > git pull -r

If the git pull -r failed, fix stuff (Section 5.2), then run1 > git rebase --continue

7 See Also

Pro Git (book) If you want to know more details. Especially sections 2, 3, 5 & 8.A glossary of terms Also available via the command man gitglossaryGit+Netbeans docs Official guide for the Git support in Netbeans. A supplement to Section 9.

Page 8 of 19

Page 9: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

ECE297 Communication and Design Winter 2019

8 A Few Extra Things

Not completely necessary, but might be useful; This section contains some things you might wantto do, but are not in the minimal necessary set of tasks for using Git. The TAs shouldn’t ask aboutanything in here.

8.1 Longer Commit Messages

If you run git commit (ie. no -m) then a command-line editor will be brought up. You should nowtype your message, then press control-o (and then the enter key) to save, and control-x to exit.Earlier we mentioned that commit message is sort-of like an email. Just writing a commit subject isfine most of the time, but sometimes you will want to add more explanation, like the body of anemail. To do this, add an empty line, and then type the body. For example:

1 Modified main.cpp by computing a new center of mass with2 a new polynomial.3

4 Added a new polynomial g(x, y, z) to main.cpp, computed center of mass5 of a cube with (x, y, z)=(5, 5, 5) and mass-density distribution g(x, y,6 z), and then printed the result.

8.2 Looking at Old Commits

1 > git log # find the commit id of the "Initial Milestone 0 commit". Copy it with ctrl-shift-c.2 > git commit -m "work in progress" # make a pristine local repository3 > git checkout c0mm1t_1D # paste (ctrl-shift-v) the copied commit it in place of c0mm1t_1D4 # Take a look around, and when you are done, run the next command5 > git checkout master # remember this name from before? this brings you back to where you were

It’s always a good idea to have a pristine repository before jumping around to different commits –otherwise, things might get very confusing. Or, git might not even allow you to change, because ifyou did, you would loose your uncommitted work!

8.3 Removing Changes From the Index

If you’ve added something to the Index that you don’t want to commit, there a command for that.1 > git reset path/to/file.c

Or, run it with no file argument (just git reset) to clear the Index completely.

8.4 The Stash

A common situation is that you have made some changes, and you think you’ve introduced a bug,but you’re not sure what the old behavior was. Or, your colleague says that they have found a bug,and you want to quickly investigate – if only you could stash your changes away somewhere. Otherpeople have thought this was useful, and Git has a mechanism for this: the stash.

Page 9 of 19

Page 10: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

ECE297 Communication and Design Winter 2019

1 > git status # assume you have some uncommitted changes2 > git stash push3 > git status # will show no changes!4 > git stash pop5 > git status # your changes will be back

8.5 Fixing and “Undoing” Commits

Only do these things to commits you haven’t pushed!If you forgot to add something, but don’t want to make another commit, or you just made a

typo in the message, run this:1 > git commit --amend

If you didn’t mean to make a commit at all, run this:1 > git reset HEAD^

Page 10 of 19

Page 11: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

ECE297 Communication and Design Winter 2019

9 Netbeans Integration for Git

Some may be more comfortable using a graph-ical interface, or maybe can’t be bothered tomemorize so many funny-named commands.Forthose people, there is a reprise: Netbeans hassome graphical tools for using Git. Don’t dismissthe command-line entirely though, it is the onlything that will be always be there for you to use,no matter your working environment. Finally,note that switching between using the Netbeansinterface, and the command-line will work seam-lessly.

The most reliable way to use this integra-tion is by right clicking on a file or folder in theProjects panel that you want to do some oper-ation on, and choosing an operation from theGit sub-menu. Often you want to commit allchanged files, or add all file changes, or do some-thing else to the entire codebase, so usually youshould specifically right-click on the project node(the one with a gear-in-box icon). The operationsare also available from the Team menu, but youmust select the file/folder/project in the Projectspanel before doing anything.

Armed with the prior sections’ information,many of the various sections and options shouldmake some sense, but let’s go through how to sosome specific things. In general, doing an opera-tion with a given name will have the same effectwhether in Netbeans or the command-line, butsometimes the defaults are different. Also, makesure to avidly read every checkbox and field Net-

beans presents you, because checking or not check-ing something may drastically change what youare doing. One last thing is that Netbeans some-times says something has been ‘Added’, whichyou might think means it has been added to theIndex, but instead this means that it is a newfile.

More information beyond this guide can befound at the Netbeans documentation for its Gitsupport.

Page 11 of 19

Page 12: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

ECE297 Communication and Design Winter 2019

9.1 Viewing Changes Made

There is no direct equivalent to git status

with all the hints and extra information, but anview of your changes can be seen by right-clickingon a folder (or your project to see all), and select-ing Git → Show Changes (Figure 2). This willopen a list of the files that have been changed ina panel at the bottom of the Netbeans window.The listing of files changed is, by default, thechanges between the Index and Working Direc-tory, ie. things that have not been added. Toselect a different kind of diff, select one of thethree buttons highlighted in Figure 3. The mid-dle one is equivalent to git diff --cached To viewthe diff for one file, double-click on it’s entry.

To view the commit history for a project/file/-folder, right-click on it, and select Git → Show

History (Figure 4, then press Search.

Figure 2: Location of Show Changes item.

Figure 3: Location of different types of diffs, withrefresh, view diff, revert and commit buttons tothe left

Figure 4: Location of Show Changes item.

Page 12 of 19

Page 13: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

ECE297 Communication and Design Winter 2019

9.2 Manipulating The Index

9.2.1 Adding

Right click on the file/folder/project and selectGit → Add. (Figure 5) The file should now showup when using Netbeans to Show Changes in theIndex, and in git status

9.2.2 Resetting

Right click on the file/folder/project and selectGit → Revert Modifications... (Figure 6). Youwill be presented with a window with 3 options.The bottom one is what we want – simply re-move changes from the Index – an equivalent togit reset

Note that the top option is selected by default,but this one is like doing a combination reset pluscheckout that obliterates all your changes to thefile/folder/project – be careful with this one! Thecentre one makes it so that the file/folder/pro-ject in your working directory matches the filesthat have been added to the index – this is anuncommon choice.

Figure 5: Adding a changed file to the Index

Figure 6: Location of the Revert Modifications...

item

Figure 7: Selecting the type of revert. The equiv-alent to git reset is selected, and the mouse isover the ‘obliterate’ option.

Page 13 of 19

Page 14: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

ECE297 Communication and Design Winter 2019

9.3 Committing Changes

To make a commit from Netbeans, right-click on your project and select the item Git → Commit...

This will open a window like the one below (Figure 8). Note that pressing Commit right now willcommit will commit all files changed, not just ones added to the Index. This is sometimes what youwant, but is prone to committing too many things. The behavior can be changed by pressing theleftmost button that is adjacently above the file list, or by carefully un-checking and checking files.Before you commit, it is a good idea to look at the changes you will be committing, which can bedone by double-clicking on a file name in the commit window.

This window can also accessed by pressing the commit button in the ‘Show Changes’ panel(Figure 3).

Figure 8: A default commit window, with the cursor over the button to change to committing theIndex’s contents only

Page 14 of 19

Page 15: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

ECE297 Communication and Design Winter 2019

9.4 Stashing

To store your changes somewhere in orderto safely pull or look at another commit, selectyour project, open the Team menu, select ShelveChanges → Stash Changes (Figure 10), andpress the Stash Changes button.7 You shouldsee all the changes you made disappear from theeditor.

When you want to get those changes back,select your project, open the Team menu, go intothe Shelve Changes → Unstash Changes sub-menu, and select the 0 entry – this was the laststash you made (Figure 10). If there is a con-flict, then Netbeans will tell you that the stashcommand failed. You must invoke the conflictresolution tool, or fix the conflicts manually – seeSection 9.7.1.

Netbeans differs from git defaults here slightly.Git automatically forgets (drops) stashes thatmerge without problems, but Netbeans keepsthem around either way. If there are too manystashes, then you can then open the repositorybrowser (Team → Repository → Repository

Browser, Figure 11) and drop any stashes youdon’t need (right-click, select Drop Stash... (Fig-ure 9) and click Yes.

Figure 9: Dropping a stash

Figure 10: Location of the Git Stash sub-menu

Figure 11: Location of the Repository Browser

item7If you use the right-click menu, it always affects the whole repository

Page 15 of 19

Page 16: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

ECE297 Communication and Design Winter 2019

9.5 Checking-out Files and Commits

9.5.1 Checking-out a File

Right click on the file/folder you would like toremove changes from, select Git → Checkout

→ Checkout Files... (Figure 12), and press theCheckout button.

9.5.2 Checking-out a Specific Commit

Remember to commit first! (Section 9.3) Rightclick anywhere in the project (any file/folder isfine) and select Git → Checkout → Checkout

Revision... Or, select your project, open theTeam menu and select Checkout → Checkout

Revision... Either will open a window like Fig-ure 13. Press Select and select the commit youwould like to view from the list on the right (Fig-ure 14).

Figure 12: Location of the Checkout sub-menu

Figure 13: The Checkout Selected Revision win-dow

Figure 14: Choosing a commit

Page 16 of 19

Page 17: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

ECE297 Communication and Design Winter 2019

9.6 Pulling

Be sure to commit first (Section 9.3). If youdon’t there will be an error window saying thatyou have local modifications that will conflict.Click Cancel if this happens.

To pull, select your project, open the Teammenu, select Remote → Pull From Upstream

(Figure 15), and click Rebase (Figure 16).If there is a problem, a window will pop up

telling you there were unresolved conflicts (Fig-ure 17). If you would like to fix the conflictsnow, press Resolve and see Section 9.7 for howto continue in that case. To resolve conflictsmanually, press Review instead. Press Abort tocancel the operation and go back to the statebefore you pulled. If you did have to fix conflicts,don’t forget to continue the rebase when you’redone fixing conflicts – see Section 9.7.2. Also,Section 9.7.1 describes how to manually invokethe conflict resolution tool.

Figure 15: Location of the Pull From Upstream

item

Figure 16: Window for choosing pull type

Figure 17: Window that shows up when there isa conflict

Page 17 of 19

Page 18: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

ECE297 Communication and Design Winter 2019

9.7 Resolving Conflicts

9.7.1 The Merge Conflicts Resolver

When pulling-in your colleagues’ work, some-times you will have modified the same parts ofthe code. Git will not automatically merge thesecases, and the require intervention. To help withthis, Netbeans provides a graphical conflict reso-lution tool. It can be opened by right-clicking afile/folder/project, and selecting Git → Resolve

Conflicts (Figure 18), or by selecting Resolve

after a pull with conflicts (Figure 17).In Figure 19 there is an example of what you

might see after trying to resolve conflicts froma failed pull. Notice the number of unresolvedconflicts, and the tabs for each file with conflicts.

For each file tab, navigate between conflictsusing the pair buttons at the top left, and choosewhether you would like to keep (“accept”) the leftcode or the right code. If you need to manuallyfix the code, leave the change unresolved, or selectaccept both. When you are done with the tool,press OK, and fix anything that needed manual

fixing. Files with unresolved conflicts will havetheir name in red, and will say Both Modified inthe Show Changes panel. When you’re done, itis a good idea to test that the code compiles, andthe tests pass as expected.

Figure 18: Location of the Resolve Conflicts item

Figure 19: Resolving a conflict from a failed pull -r. Location of the unresolved conflicts count indashed red, the next and previous difference button in dashed green, and the files with conflictstabs in dashed orange

Page 18 of 19

Page 19: ECE297 Quick Start Guide Git - University of TorontoThe ECE297 Quick Start Guide Series Git “Itiseasytoshootyourfootoffwithgit,butalsoeasytoreverttoapreviousfootandmergeit withyourcurrentleg.”

ECE297 Communication and Design Winter 2019

9.7.2 Continuing or Aborting a Rebaseor Merge

Remember how we had to issue git rebase --

continue after fixing conflicts from a pull -r?(Section 5) Well, you have to do something equiv-alent after a pull conflict in Netbeans too. To getNetbeans to finish the rebase, you must open theTeam menu and select Branch/Tag → Rebase...

(Figure 21). It will present you with the windowin Figure 20, where you should press ContinueYou can press Abort to go back to the state beforeyou pulled.

Note that trying to pull again before eitheraborting or continuing will not work, givinga cryptic error about not having an upstreambranch.

Figure 20: An Unfinished Rebase window

Figure 21: Location of the Rebase... item

Page 19 of 19