Notes
00. Terminal Basics
01. Git & GitHub
Terminal Basics
The terminal is a program that gives you a command line interface or (CLI) to the files and programs on your computer. You are probably more familiar with a graphical user interface or (GUI), which entails using the mouse to open files and folders. But you can do the same things via the command line using the keyboard to input text-based commands.
Understanding the prompt
- The prompt is what a terminal will display before you enter a command. On the school comptuers, it will look like this:
dw@cslab1-4:~$
- There is a lot of information packed into that line of text. You can decipher it as:
USERNAME@COMPUTER:CURRENT_DIRECTORY$
- It tells you who is currently logged in, what computer you are on and what directory is currently open.
- The
:
separates the computer name from the current directory. - The
~
is a shorcut for the user’s home directory. (Without it my prompt would look like this instead:dw@cslab1-4:/home/support/dw$
) - The
$
is there to signify the end of the prompt. Whenever you see instructions from me that start with a$
, that means I am providing you with terminal based commands.
- The
Using programs via the command line
- In a GUI, you open programs by clicking on an icon. Notably, you need to find that icon in order to click on it.
- In a CLI, if the program is installed in a normal location, you only need to type the name of the program at the prompt and hit enter. For example, you can launch processing with
$ processing
or firefox with$ firefox
. Both of those programs do not run in the terminal, but can still be launched from it. - Programs that are meant to be run in the terminal are often referred to as commands.
- The 3 Most Important Command Line Programs
$ ls
: List the files in the current directory$ cd DIRECTORY
: Change the current directory.$ cat FILE
: View the contents of a file in the Terminal. This works very well for text-based files (.pde, .md, .txt etc.). But not for things like images.
Like Real Estate, It’s All About Location
- You’ll notice that the first 2 most important command line programs listed above deal with the “current directory”. In a CLI, the current directory is used at the starting point for any command you enter. For example, if you want to open a file and it is in the current directory, you just need to enter
$ open FILENAME
. If the file is in a different directory, then you need to provide more information about the file’s location. - You can see the current directory in your command prompt, but you can also run
$ pwd
to see the full path to your current directory as well. We often say that a Terminal session is in the current directory. - Using
cd
to change directories.- You can manipulate your current directory with the
cd
command (short for change directory). - If you want to change into a directory that is inside your current directory, you can enter
$ cd SUB_DIRECTORY
. If the directory you want to change into is not in the current directory, you will have to provide more information about the location of that directory. - If you are in a directory, and want to go back up a level to the parent directory, you can use
..
like so:$ cd ..
. - If you want to go back to your home directory, you can enter
cd
without any arguments (i.e.$ cd
).
- You can manipulate your current directory with the
Back to top
Git and GitHub
Some background information:
- Git is a program designed to help people keep track of changes to their work over time, sync copies of the same work on multiple computers, and help multiple people work on the same project. When you use git, you put your work in a specific folder called a repository and use git commands to maintain that repository.
- All of the commands that you enter on the terminal are specifically git commands.
- GitHub is a website that (among other things) provides a central place to store git repositories. You do not need to host your repository on GitHub to use git, but it makes it easier to keep things in sync across multiple computers (like at school and home).
- All of the git-related commands should be entered in a command line environment. On the school computers, you access the command line via the Terminal program. At home:
- If you have a Macintosh or Linux computer, you also have a program called Terminal, it will work the same way Terminal works at school.
- If you have a Windows computer, install git for windows. This will install a program called Git Bash. You can use Git Bash the same way you use Terminal at school.
- When working in the terminal, make sure you are in the correct directory. You can always see the current directory you are in by using the
pwd
(short for print working directory) command in the Terminal or Git Bash.
Git vocabulary:
- Repository: A directory containing work to be tracked by
git
. - GitHub: A website that provides a central place to store git repositories.
- Clone : Make a copy of a git repository on a computer.
- Once a repository is cloned, it will be forever linked to any other copies (including the one on GitHub).
- The cloned copy of a repository on a specific computer is called the local copy.
- Add : Tell git to keep track of a new file.
- All new files must be added, or git will not recognize them.
- Commit : Keep track of any new changes you have made to files within the repository.
- Committing changes only effects the local copy of the repository.
- Push : Upload any committed changes to GitHub.
- Pull : Download any changes from GitHub to your local copy of a repository.
Installing GIT At Home
Mac and linux machines probably have git installed already.
- To test, open your
terminal
program, and type$ git
. Ifgit
is installed, you should see a list of various options for thegit
command.
Windows users will want to install git and the shell gitbash from here: https://gitforwindows.org/ This includes git, gitbash, and has shell integration so that you can right-click on a folder in Windows Explorer to access gitbash at that location!.
- Although it comes with GitGUI, please learn to use the terminal commands until you understand exactly what you are doing.
Configure git on each machine you plan on using
Configure your local git program using these commands. Important: Use your name and your git related email.
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
git config --global core.editor nano
git config --global pull.rebase false
Git/GitHub Workflow
- Make a repository on GitHub (for class, this will be done for you when you accept a GitHub classroom assignment). Get the ssh link from the green code button on the repository webpage.
- Once Per Computer
- Clone the repository via the command line (either in terminal or gitbash).
$ git clone REPOSITORY_SSH_LINK
- Once you’ve done this, you will have a copy of the repository on your computer. It will be an exact copy of the repository at the time of cloning. It will not keep up to date automatically.
- Clone the repository via the command line (either in terminal or gitbash).
- Every time you work in your repository
- Before doing anything else, get any potential updates from GitHub’s version of the repository. Every if you’re 100% certain there are no changes, do this, it won’t hurt.
$ git pull
- Whenever you create a new file, let git know it should keep track of it by adding it:
$ git add FILE
- If you don’t do this, git will ignore the file entirely.
- When you are done working, commit your changes, and then push them to GitHub.
$ git commit -am "RELEVANT MESSAGE"
$ git push
- You should run all these commands from within the directory for your repository. See the Terminal Basics notes for more information on keeping track of your current directory.
- Before doing anything else, get any potential updates from GitHub’s version of the repository. Every if you’re 100% certain there are no changes, do this, it won’t hurt.
Merges & Conflicts
A merge conflict occurs when the states of your local and remote (github) repositories are out of sync. This can happen in two instances:
- The remote (github) repository has work that you have not pulled, and you attempt to push commits made on your local repository. (this is more common).
- The local repository has commits that you have not yet pushed, and you attempt to pull work from the remote (github) repository.
Most likely, the action that will trigger a merge conflict is $ git push
.
- At this point, you will see an error message saying
Updates were rejected because the remote contains work that you do not have locally...
- To fix this merge conflict, you will need to
$ git pull
, at which point git will either fix the conflict automatically, or you will have to fix it manually.
Dealing With Merges
- You will not know a merge conflict has happened until you
push
. - First,
pull
the remote changes.- If git can fix the merge automatically, it will, and create a new commit with the change - you will be prompted to enter a message using
nano
. - If git cannot fix the merge automatically, you will have to manually fix the merge and commit from the command line.
- If git can fix the merge automatically, it will, and create a new commit with the change - you will be prompted to enter a message using
- Push the changes.
Handling Merge Conflicts - Easy Mode
Sometimes, git
can handle the merge conflict itself, but we must configure git
to do so:
$ git config --global pull.rebase false
If git can fix the conflict, then it will:
- Create a new
commit
that includes the merge action. - Ask you to enter a commit message for this commit.
- When this happens,
git
will open a terminal-based text editor callednano
. You will type your message and hit ctrl-x to exit.nano
will ask you if you want to save the file, follow the prompts to save using the provided file name.
- When this happens,
Once you have provided a commit message, you have to $ git push
to push the merge fix to github.
Handling Merge Conflicts - Less Easy Mode
If git
cannot automatically merge, it will edit the effected files dennoting the differences between the remote and local files. The changes will look like this:
here is some content not affected by the conflict
<<<<<< HEAD
this is conflicted text from local version
=======
this is conflicted text from remote version
>>>>>>> commit 52a3ba6;
The =======
separates the conflicting content.
In order to fix the merge, you must:
- Figure out which code you want to keep (this may be a combination of both versions).
- Remove the lines git added to the file.
- Create a new commit and then push.