-
Notifications
You must be signed in to change notification settings - Fork 2
Git and Command Line Basics
Everything you need to get started on the terminal!
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. |
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. |
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:
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):
- First, press
Esc
. This will ensure you're in Vim's "normal" mode, instead of "insert" mode. - Then type
:x
. You will need to holdshift
to type the colon. If successful, you should see:x
near the bottom-left of the screen. If not, pressEsc
twice, then try again. In vim,:x
means "save and quit". - 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.
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":
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.
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:
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:
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:
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.
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:
- It could just work. Congrats!
- It could attempt to trap you in Vim. See the section "Exiting Vim"
- 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.
This is a tutorial on what to do if you see a merge conflict.
- Throw your computer out the window
- Eat cheese
- 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
this is a tutorial on branches and checkout and maybe more stuff