Command Line git

git is an excellent version control system that was designed for managing the Linux kernel source tree. Our git repositories will be hosted on GitHub.

Before You Start

If you haven’t already, you will need to configure git with your user information (on your VM):

$ git config --global user.email "you@example.com"
$ git config --global user.name "Your name goes in these quotes"

# For example:
$ git config --global user.email "mmalensek@some.school.edu"
$ git config --global user.name "Matthew Malensek"

Setting up SSH Access

To access your repositories and commit changes, you’ll need to configure SSH with GitHub. Your already has an SSH key ready to go, so you can just do this:

# View the contents of the key (make sure the key ends in .pub):
$ cat ~/.ssh/id_ed25519.pub
ssh-ed25519 0AA1C3Nzfiewjiowlfl5A29Uu5298jzbZVXc8uaHtXjyJc4oabcdefg6S0rnlBBpx8PJ mmalensek@magical-unicorn

Now copy the public key that you just printed with cat to your clipboard. Note: It is one very long line – make sure you get all of it!

Next, navigate to your GitHub settings -> ‘SSH and GPG Keys’ -> ‘New SSH Key’ and paste your key in the text box provided.

When you want to clone a repo to your machine, make sure you’ve selected the ‘ssh’ tab on the GitHub UI’s ‘code’ or ‘clone’ menu, and then:

$ git clone git@github.com:usf-csXYZ-spAB/L2-malensek.git

cd into the directory that was just created, and you’re all set to work!

Command Line Guide

Here’s the basics for pushing your changes to GitHub from the command line.

# Clones the repository to the current directory
$ git clone https://github.com/username/your-repo

$ cd ./your-repo
# Here's where the magic happens -- you write/modify some code!
(make some changes to your file(s))

# See what's changed
$ git status

# Let's check in our change
$ git add filename

# ..or, add multiple changes:
$ git add file1.c file2.c

# ..or, add all .c files that have changed:
$ git add file*.c

# you can also add everything (but be careful doing this)
$ git add .

# Let's see what changes are ready to be committed
$ git status

# Great! Now, let's commit the changes with a nice commit message
$ git commit -m "Modified function z() to do a, b, c."

# (If the commit fails and asks for your user information, follow
# the steps at the beginning of the guide.)

# Finally, push the changes back up to GitHub:
$ git push

More Advanced: Branches

Once you’re comfortable pushing and pulling changes, it is often helpful to create separate branches in your repository rather than just committing all your changes to the main branch (the default).

# Create a new branch (called 'new-feature'):
$ git branch new-feature

# Switch to the branch:
$ git checkout new-feature

... work happens ...

# Once you're finished, commit your changes as usual:
$ git add xyz
$ git commit

# This is important! You need to configure an upstream location to push to.
# Usually 'origin' will do (the original remote repo) and then we provide
# the name of the remote branch:
$ git push --set-upstream origin new-feature

# Now let's say you want to go back to the main branch and work there:
$ git checkout main

# Note how your files are now exactly as they were BEFORE you created
# the 'new-feature' branch.

... more work happens ...

# Now, say you want to merge in the changes from 'new-feature' and also
# keep your work on main. Merge from the main branch with:
$ git merge new-feature

# Usually the merge can be done automatically... But if it can't you'll
# need to resolve the merge conflicts. This is where a GUI tends to be
# nicer, but you can do it from the command line. Use `git status`
# to see what merge conflicts there are:
$ git status
$ edit x y z (resolve the conflicts)
$ git add x y z
$ git commit -m "Merge new feature branch"

# Finally, you can delete the branch now that it's merged:
$ git branch -d new-feature