Using Git through Command Line

Quick reference

Detailed walk-through

1) The key thing to working with Git is always knowing where to find your files in the Finder (on Mac) / File Explorer (on Windows) and in the Terminal/Command Line Shell. So you need to save them in a place where you can easily see them.

2) You interact with your local directory the way you would any other. You can drag files into it using the Finder/File Explorer, save files into it, and check in with the remote repository to pull new files in.
So this is what you should do: Open Terminal/Command Line Shell, and step down into your GitHub directory, and then down into the DHClass-Hub (or desired repo), and check that you are where you want to be by typing ls. The ls command will show the contents of the directory in which you’ve positioned yourself. You can cd up or down (cd means change directory), by moving like this:

cd ..

This moves up to a parent directory.

cd directoryName

This moves down into a directory.

Then to pull in any changes from the remote “mothership” repo, type:

git pull

Watch your shell (Git Bash or Terminal) and make sure the pull completes: It should show you lots of lines and indication that new files or changes are coming in. You’ll see plus signs marking additions to files, and - signs marking deletions in files or of whole files—all of these are changes you can expect to see. Alternatively if there are not changes, you’ll see a message about that, too.

If something is wrong, GitHub may alert you that pulling in changes will overwrite your local changes to particular files. If this happens, you should add and commit your changes to your files (following instructions below), and then afterwards git pull. (You will still need to git pull any changes before you git push your local changes up. )

3) When you want to share you local changes to the repo with the remote mothership and other collaborators, you need to add, commit, and push those changes. Here’s how you do it:

Dealing with merge issues

When you and other team members are working in a repo at the same time, and each of you is pulling, committing, and pushing files, you may find that you cannot pull in changes to your local repo, because GitHub warns that changes might write over the state of your work.

esc : w q

That’s the escape key then colon, w, and q. This combination let’s you escape out of the commit-editing window, then :wq is to write and quit the vi editor. This will bring you back to the terminal / command line prompt where you normally write git commands.

Merge conflicts

A variation on the merge issue above involves multiple team members working on a file and pushing at nearly the same time. The edits may be in locations where it is difficult for GitHub to automatically figure out how to merge them. When you attempt to pull these in, you will be told that GitHub cannot automatically complete the merge. Don’t panic, but do be patient.

>>>>>>>>>
>>>>>>>>>
code of one version

============
============
code of the other version
>>>>>>>>>
>>>>>>>>>

You’ll need to read this carefully and delete the version that’s not wanted, as well as the GitHub “tiretrack” marks. Save the file and return to your command line / terminal.

Type git add and git commit -m "your message about resolving the merge conflict here". Then try a git status to make sure all is well, and if GitHub approves, go ahead and type git push to resolve the conflict.

Your team members should now do a git pull and pull in your resolution of the merge conflict, and all may continue their work as before.

Further Reading: Becca Parker’s “Explain Git Shell” tutorial