Skip to content

Git and Command Line Basics

sberkun edited this page Oct 6, 2021 · 6 revisions

Git and Command Line Basics

Everything you need to get started on the terminal!

Quick Reference

Command Description
git status See cool git information like current branch, staged files, etc.
git add [filename] Adds a file to be committed. [filename] can be a file/folder name, * for all files, or . for current folder.
git commit -m "[message]" Save all added files to a commit, and give the commit a helpful message.
git pull Download and merge in changes from the remote (Github) repo.
git push Upload changes to the remote (Github) repo.
ls List files in the current directory.
cd [foldername] Go to [foldername]; this can be a sub-folder in the current folder, or .. to go "back" to a parent folder.

Extended Quick Reference

Command Description
python3 [filename.py] Runs the python file [filename.py]. Depending on your computer, the commands might be python or py instead of python3.
touch [filename] Create a file with the name [filename]
mkdir [foldername] Create a folder with the name [foldername]
cat [filename] Print out all the contents of [filename]
pwd Tells you where you are; the name stands for "print working directory"
rm [filename] Deletes a file. Be careful when using this!
cp [oldfilename] [newfilename] Copy a file from [oldfilename] to [newfilename]. For example, cp folder1/cheese.txt folder2/cheese.txt copies cheese.txt from folder1 into folder2.
mv [oldfilename] [newfilename] Move or rename a file. Usage is the same as cp
git clone [repourl] Copies a remote (Github) repository from the given url, into a new git repo on your computer.
git init Turn the current folder into a new git repo. This repo will have no remotes, unless you add them.
git remote -v Lists the repo's remotes. Useful for debugging.

Exiting Vim

Sometimes if you forget the -m "[message]" when running git commit, or when running git pull, your terminal might suddenly throw you into a strange and unfamiliar environment that looks roughly like this:

a screenshot of vim

This is an ancient text editor called vim/Vim. Vim was made specifically to trap unwary young students in a never-ending nightmare that thwarts the best attempts at escape. However, there's a secret passcode passed down by ancient wizards, that will let you sneak out unharmed (usually):

  1. First, press Esc. This will ensure you're in Vim's "normal" mode, instead of "insert" mode.
  2. Then type :x. You will need to hold shift to type the colon. If successful, you should see :x near the bottom-left of the screen. If not, press Esc twice, then try again. In vim, :x means "save and quit".
  3. Press Enter. This should close vim, giving you back your normal terminal. Congratulations!

Why does Git do this? Although vim is primarily a trap, it can also be a powerful text editor. If you're curious, and would like to join the dark side, you can learn vim by running the command vimtutor, or by trying one of these intros: ocf, openvim.

Basic Git Workflows

Dolly the Sheep (Cloning a repository)

When you first start working on a project, often all the code will be in a repository on Github (for example, the Shepherd repo). How do you get it onto your own computer so you can edit it?

This process is called cloning a repo. In your terminal, cd into the folder where you want to store your copy of the repo. For example, if I wanted to clone Shepherd, I would run

cd Documents/pie

since I want my copy of the Shepherd repo to be the folder Documents/pie/shepherd. Now, go to your repository on Github, and look for the green button that says "Code":

code button

Click it, and copy the url it gives you. Now you can run the clone command:

git clone https://github.com/pioneers/shepherd.git

(replacing the url above with the url that you copied). If all goes well, a new folder should appear, with your copy of the repo. Congratulations! You can now start working in your local copy.

Saving/Uploading changes

Git will not automatically back up your changes, or upload them to Github. So how do we do that?

First run git status in your local repository. It should give an output similar to this: status with unstaged modified files

The red lines (modified:) are the files that got changed since the last backup/commit. Make sure they are files that you really intended to change! It's also a good idea to review changed files (either with git diff, or manually by comparing them with what's on Github) before moving on.

Next, git add all your files. You can do this one-by-one (best practice), or you can live life on the edge and use git add . to add all files in the current folder. Run git status again to make sure everything that you wanted to add is added: status with staged modified files

Now, run git commit -m "I like cheese", replacing "I like cheese" with a descriptive message about what you did. This message will remain on Github until the end of time, so if you ever wanted to make a mark on history, now is your chance. If all goes well, everything should be clean when you run git status again: clean status

Finally, upload your changes to Github by running git push. If you see your changes appear on Github, congratulations! You've successfully created and uploaded a commit.

Downloading Other People's Changes

First, run git status to see if you have changes to be committed. If so, commit your changes (refer to previous section), and then continue.

With your status clear, run git pull. There are 3 possible outcomes for this command:

  1. It could just work. Congrats!
  2. It could attempt to trap you in Vim. See the section "Exiting Vim"
  3. It could have a "merge conflict". See the section "Merge Conflicts and How to Panic Appropriately".

If all went well, your local repo should now be up-to-date with the remote. At this point, you can optionally run git status to make sure everything is good, and/or git push if you made any commits in the process of pulling.

Merge Conflicts and How to Panic Appropriately (TODO)

This is a tutorial on what to do if you see a merge conflict.

  1. Throw your computer out the window
  2. Eat cheese

Terminal Fun Facts (TODO)

  • Mice/touchpads don't really work on the terminal. Do everything with your keyboard.
  • If you start typing a file/folder name, you can press Tab to autocomplete.
  • Ctrl-C is used to "kill" a long-running process.
  • Ctrl-D is use to exit an environment, such as a python interpreter, an ssh session, or the terminal itself.
  • Copy-pasting: Mac: works like normal. Linux: use Ctrl-Shift-C/Ctrl-Shift-V. Windows: shift-insert?
  • Ctrl-A will bring your cursor to the beginning of the line
  • clear will clear your terminal window
  • You can press Ctrl-R to try to autocomplete from your history as you're typing.
  • history | tail -n 10 will print the last 10 commands you ran

Advanced Git Workflow (TODO)

this is a tutorial on branches and checkout and maybe more stuff

How Git Works under the hood (TODO - kinda optional)

https://sp21.datastructur.es/materials/proj/proj2/proj2