Eric Brauer
When we are collaborating on a document/file/codebase, we need to track changes. Creating new copies of a file is confusing. Adding more collaborators and files will increase the complexity and introduce lots of confusion.
Git is a program we can install on our Linux machine. It tracks changes on a directory.
GitHub is a website that hosts many repositories, usually for free. It has become a popular choice for programmers when they are trying to choose a place to store their work.
In addition, it has a lot of features such as issues, team management, Wikis, Continuous Integration tools, etc.
Not every git repo is stored on GitHub!
Use git config
to specify your name and your email
address (so that we know who to blame for a specific change!)
Use your myseneca email address, otherwise you may not get full credit for your work.
In order to sync with GitHub, we need to generate a public/private keypair, and then store the public key on GitHub.
This is the same as using SSH to log into a remote Server.
ssh-keygen
to generate keys on the machine
you will be using to complete labs. (Fedora VM)We are ready to grab the contents of an existing repository and copy it to our local machine.
Use git clone
+ the URL from GitHub (Ctrl+Shift+V will
paste into a terminal)
You should now be able to cd
into the directory you have
created.
Use git status
to check your work as you go. Reading and
understanding this output will help you a lot!
On branch master
up to date with origin/master
origin/master
is the remote repository. This tells
you that remote and local versions are the same.
nothing to commit
example.py
, save the file.git status
to see what has changed.git add example.py
to include this file in what you
want to track.git status
once again! What has changed?git commit -m "made my first change"
git status
yet again, believe it or not..Any significant change should have its own commit. This helps if your change broke something important and we need to roll back the change.
Using git push
will sync your commits with the remote
repository.
git log
to view all your
commits.git checkout <hash>
where hash refers to the
commit you wish to view.git checkout master
to return to the
"head" of your master branch.If you break your code, you may need to undo your latest commits.
There are several ways to do this, but the most direct is to use
git reset --hard <hash>
This will remove the commits that came after the hash commit. Be warned!
For other (less destructive but more complicated) options, check this page
Warning: origin/master has diverged
Your local version of the code doesn’t yet have Chris’s commit!
git pull
to sync Chris’s changes
before you create your commits. This helps prevent 90% of
problems!git pull --rebase
. This
eliminates the need for a mergewhat happens if Chris and Michael change the same code?
instead of any person’s change overwriting the other, git will force you to merge your changes
On GitHub, find the top of your repo page. Click the drop-down to create a new branch based off of main:
From the terminal, you can use a command:
git checkout -b <new branch>
The checkout
command is also used to switch between
branches.
On GitHub, once you’ve created (and pushed) commits, you can choose to merge a branch by finding this message at the top of the repo:
This will begin a pull request, where your branch undergoes final approval.