Welcome!
In this guide, we will be teaching you how to use git
, a sharing tool for code. git
is used throughout software engineering and mechatronics fields because of its ability to store, share and merge parallel versions of functional code. This makes it very useful, so let's get it right!
If you want to deep dive into what git is, checkout the more-about-git
branch at the end of this tutorial ;)
git
is a tool for sharing collections of files. github
is a centralised space to store collections of files, accessible using git
. If you like metaphors, github
is to git
as outlook
is to email
.
As a roboticist, not all computers have screens - such as robots you might find in the brain of a self-driving car, self-sailing boat, self-walking dog or self-chilling smart fridge. git
especially was designed in an era when screens with GUIs were harder to come by - so in this tutorial we'll give you a crash course on terminal commands too.
- Open a terminal window.
- Windows: Type in 'cmd' into your start, and click
command prompt
. If all goes well, a black cmd window should appear.- Don't use
git bash
when it appears on your start menu. That will bring up a linux style terminal.
- Don't use
- OSX: Type in 'terminal' into your launcher. If all goes well, a white terminal window should appear.
- Linux: Type in 'terminal' into your start. If all goes well, a black terminal window should appear.
- Windows: Type in 'cmd' into your start, and click
- Locate yourself.
- Windows: Each line of your terminal should show you where you are. E.g. Your terminal should start with
C:\Users\<your-username>
. - OSX: Your terminal should show you the folder you are in next to your username. To figure out the entire path (aka absolute path) to where you are, type
pwd
and hit enter. - Linux: Depending on which distribution you're using, you may have your full path or just the folder. Typing
pwd
will work as well.
- Windows: Each line of your terminal should show you where you are. E.g. Your terminal should start with
- Make a folder (aka directory).
- All platforms: Type in
mkdir USRC
and hit enter.mkdir
stands for "MaKe DIRectory".
- All platforms: Type in
- Enter the directory you just made.
- All platforms: Type in
cd USRC
and hit enter.cd
stands for "Change Directory".
- All platforms: Type in
- Leave the directory you just made.
- All platforms: Type in
cd ..
...
is a special sequence that tellscd
that you want to go out of the folder.
- All platforms: Type in
- Go into the directory you just made again. (Review step 4).
- Make a file in this directory.
- Windows: Type in
type nul > newfile.txt
in your terminal and hit enter. (Ask stackoverflow if you want to know what this means - its a long answer). - OSX: Type in
touch newfile.txt
in your terminal and hit enter. - Linux: Type in
touch newfile.txt
in your terminal and hit enter.
- Windows: Type in
- Figure out what is in this directory.
- Windows: Type in
dir
and hit enter. Read the output. - OSX: Type in
ls -a
and hit enter. Read the output. - Linux: Type in
ls -a
and hit enter. Read the output.
- Windows: Type in
- Open a normal file explorer, and check everything matches up.
- Windows: Type
explorer .
, then confirm using the file explorer that newfile.txt exists. - OSX: Type
open .
, then confirm using finder that newfile.txt exists. - Linux: Type
nautilus .
, then confirm using the file explorer that newfile.txt exists.
- Windows: Type
- [TEST YOUR KNOWLEDGE]: List all the files in your
Desktop
folder.
A quick summary of the commands we learnt:
mkdir <name>: Make a directory called <name>.
cd <name>: Open the directory called <name>.
cd ..: Go up one directory level.
ls -a OR dir: List the files and directories in this folder.
touch <name> OR type nul> <name>: Create an empty file in this folder.
For more terminal fun, checkout the fun-with-terminal
branch at the end of this tutorial ;)
Your git
journey will be different depending on what platform you're using. Select from the below:
- If you're on windows, you'll need to start by downloading git. Go here: https://git-scm.com/downloads
- Once you get to the screen where there are a whole bunch of checkboxes, leave it at the default configuration.
- When you get to the screen where it says 'Use VIM as the editor by default, change it to Nano. Unless you're a masochist.
- Choose 'Use windows' default console window' when you get to it.
- Leave the rest of the config as is.
- Once you're done, follow the COMMON steps below.
- If you're on MacOSX, you'll need to start by installing Homebrew, which then can install git, so copy this command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Open up Terminal, which can be found through Spotlight, paste the link and then press enter. (You might need to enter your computer password)
- Next press enter when prompted, and let the terminal install Homebrew. You'll know this is over when you stop getting prompts.
- Type
brew install git
into terminal and press enter, this is installing Git. Just let the computer do its thing and you're done. - Once you're done, follow the COMMON steps below.
- Follow the instructions here: https://git-scm.com/downloads
- Once you're done, follow the COMMON steps below.
- Close the terminal that you opened in the previous part, and open a brand new terminal. (You need to do this because an old terminal won't know what
git
is if git is installed while the terminal is alive) - Create a git username and email address. (You only need to do this once per computer, ever.)
- Type in
git config --global user.name "YOUR_NAME"
and hit enter. If you want to change your name or you blindly copied and pasted, you can run this command again anytime. - Type in
git config --global user.email "MY_NAME@example.com"
and hit enter. [//]: # (git doesn't actually verify your email address for the above command and github doesn't check your commits, so you could theoretically commit as a person you arent...)
- Type in
The simplest use for git
is copying helping contribute to others's code. This is known as clone
ing. Let's copy all the files in this here intro
repository.
repository: A collection of files. Local repositories are ones on your computer. Remote repositories are ones on the web (github), or other peoples' computers.
cd
so that you are inside theUSRC
directory. You may already be in theUSRC
directory - can you remember how you would find this out?- Scroll up to the top of this page and press the BIG GREEN "Clone or Download" BUTTON under the header. You will see Clone / HTTPS / a link. Copy that link.
- Type into your command window:
git clone https://github.com/usydroboticsclub/intro.git
. (Yep, that's the link you just copied. You canctrl-v
it in on windows, orctrl-shift-v
on MacOS and Linux. - Press Enter. You should see something like this:
Cloning into 'intro'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 675 bytes | 51.00 KiB/s, done.
- Go into the directory you just created using git clone by typing
cd intro
. - List all the files in this directory. (Do you remember how?) Check that these are the same files that are in this repository, if you scroll to the top.
- Open your file explorer (Do you remember how?) Click on
Completed.md
to open it with your text editor. (If you don't have vscode or atom or notepad++ on your computer, you may have to "open with" notepad or some other text editor.) - Check out all the cool people who have done this tutorial already!
And that's it! If you want to practice this, then why not try cloning some other stuff on github, like this one.
You can use git with 2 computers directly, but chances are that you don't have two computers on hand; so instead we'll have to use github as a third party. Let's get you signed up to github.
- Go here: https://github.com/
- Sign up, in the top right corner.
- That's it. Github is nice and easy.
The polite way of making changes to a repository on github is to copy it to your own github repository. This is called forking, and happens ALL on github.
- Scroll up to the top of this webpage, and find the
Fork
button in the top right corner. Press that button.
Fork: To create a copy of someone else's repository in your personal repository.
- Once the repository is forked, continue the tutorial on your forked version of this repository. (Yes, that means close this window and skedaddle over. We'll come back here the long way around.)
cd
back to theUSRC
directory, and make a directory calledmy_fork
. (Do you remember how to make a directory?)- Clone your fork of the repository. (Do you remember how?)
- Do not reclone the usydroboticsclub version! If you accidentally do that, then use your file explorer to delete the
intro
folder.
- Do not reclone the usydroboticsclub version! If you accidentally do that, then use your file explorer to delete the
- Since this is your copy, you are the owner, so you can change it willy nilly. Let's go do that now.
Now, we'll make some changes to this very repository, and tell Git that we've done so git will save it for us.
- In your version of the repository, open the
Completed.md
file using a text editor. - Type your name on the end of
Completed.md
with your favourite text editor. I recommend getting VSCode for all platforms. - Save your changes.
- Go back to your terminal window, and type in
git add .
. This tells git to look at all the changes you made, and prepare them for sharing. - Now type in
git commit -m "Added my name"
. This tells git that you're sure you want to make these changes, and tells git to tell other people that these changes pertain to adding your name. (The-m
stands for message. You must have a message with your changes, because git likes to enforce good coding practice.) - Now type
git push
. This tells git to pass your changes to github. You may have to login using your github username and password (or your email and password).- If you get
Permission Denied
, then you've copied the usydroboticsclub repository, not your fork! You need to delete thisintro
folder using your file explorer and start again :(
- If you get
- Visit your forked repository now on github, and check that the
Completed.md
file now has your name.
A summary of the steps we did:
1. Make changes
2. git add .
3. git commit -m "some meaningful message please"
4. git push
Now that you've made some changes to your local version of git, you'll want to make a pull request, so that we can sign off your work in our repository.
pull request: a polite way of asking a project owner to accept your changes.
- Press the
Pull requests
tab on your own github repository. - Click
New pull request
, then click the green button that saysCreate Pull Request
. - Wait for us to approve it. (Github will send us an email - and if you're in one of our live tutorials, we'll get to you pretty quick.)
- Great! Your name is now on our list. Welcome to the club! And congrats on learning
git
!
There's still plenty more to learn, but if you've gotten up to here then you'll know enough git
to follow on to more interesting things in our library. If you're in one of our weekly tutorials, see you next week! Otherwise, feel free to check out more below or in our library.
This file's getting pretty long! To unlock the other extras in this tutorial, let's teach you git branches
!
cd
to theintro
folder of either our original repository or your copy.- Type in
git branch
. This will list out the branches that are available. - Choose a branch that catches your eye, and type
git checkout <branch-name>
. I'd recommend, in order of usefulness:
git checkout making-a-repository
git checkout how-to-merge (how-to-merge-2 is related and intentional) [TODO]
git checkout merge-conflicts (merge-conflicts-2 is related and intentional) [TODO]
git checkout git-ignore [TODO]
git checkout git-history [TODO]
git checkout fun-with-terminal [TODO]
git checkout more-about-git [TODO]