Mr. and Mrs. Git - tdkehoe/blog GitHub Wiki

This Monty Python episode explains what a git is:

https://www.youtube.com/watch?v=HtyO4tmpPdk

Git is British slang for a boorish, unpleasant person with poor social skills. When Linus Torvalds created a version control system in 2005, he called it "the information manager from hell," and named it Git.

GitHub was started in 2007 and launched in 2008. GitHub is the opposite of a git; its slogan is "Social Coding." The idea was to use Git to make open-source projects more like Facebook or Twitter.

Git can be run from the terminal or using the GitHub desktop app, or using many other desktop apps.

Basic Concepts

Git can be used locally or remotely. Used locally, Git maintains a branch of previous versions of your files, in case you need to go back to an old version. Time Machine also saves your files, but Git includes comments identifying each revision, and has features for branching so that other coders can collaborate on your project.

Used remotely with GitHub, your files are safely stored in "the cloud." Also, strangers can download your code and collaborate on your project.

When you copy someone else's GitHub repository to your GitHub account, this is called forking. When you download the forked repository

While you likely have only one GitHub account, you can have many repositories. If you want to use Git remotely, start by setting a GitHub account (free). Then install Git on your computer.

Git begins with initializing a local repository. This will track all the files in its directory and subdirectory.

Git has four types of stages of files:

  1. Unstaged or untracked files that you're working on, as well as files or subdirectories you've added to the directory.
  2. You then add the files you want tracked to the staging area.
  3. When you're ready to save files, you commit the files from the staging area to Git.
  4. When you're ready to upload the files to "the cloud," you push the files to GitHub.

Here's a video explaining this:

https://www.youtube.com/watch?v=1ffBJ4sVUb4

Two books about Git:

Using Git From the Terminal

To initiate a git repository in your current directory:

git init

At Galvanize we installed something in our terminal that changes the prompt to show the git status. (I don't remember how we did this.) My terminal prompt changes to

my_directory git:(master)

git:() is in purple and master is in red.

To check the status of your files:

git status

This will tell you what branch you're on (usually the master branch), then under "Changes to be committed:" you'll see your staged files in green, then under "Untracked files:" you'll see your unstaged files in red.

When I create a file, a yellow x appears in the prompt. I'm not sure what the yellow x indicates.

To add the unstaged file git.txt to the staging area:

git add git.txt

or

git add -A

or

git add *

to add all the files in the directory.

Then run git status again to see if your files are staged (green).

To save your staged files:

git commit -m "Fixed bug that was bugging me."

Is the author's name automatically added to the comment?

You can use setenv or export to make Git open your favorite text editor so you can write a long, clear comment.

Run git status again and you should see

On branch master nothing to commit, working directory clean

To upload your files to GitHub, you can connect to GitHub via HTTPS or SSH. HTTPS is a pain because it asks for your username and password every time you upload files. SSH is easier because it gives you a URL to include in your command, and no password is required. You will need to set up SSH and create an SSH passphrase. Then when you create a remote repository in GitHub (the + button to the right of your name in the upper right part of the GitHub screen)

Screen Shot 2015-04-26 at 1.59.02 PM

look down the right column of your new GitHub repository and you'll see the SSH clone URL:

Screen Shot 2015-04-26 at 1.59.23 PM

Click the clipboard icon to the right of the SSH clone URL to copy it to your clipboard. Now go back to your terminal and enter:

git remote add origin [email protected]:tdkehoe/my_repository.git

"Origin" can be any name you want. "Origin" is the standard name for the first or canonical repository. If you've forked someone else's project, you might want to choose another name.

As noted above, [email protected]:... is copied from your SSH clone URL.

Lastly,

git push origin master

You can then look at your GitHub repository and you should see your files.

Master is the latest commit. I'm not sure what HEAD refers to.

Pushing To an Existing Repository

To see what remote repository a directory is connected to:

git remote -v

If no remote repository is connected, then add a remote repository:

git remote add origin [email protected]:tdkehoe/my_repository.git

When your local repository is connected to your remote repository, this command uploads your files:

git push origin master

That command uploads your files to your master branch, which works great if you have only one branch in your remote repository. If you have more than one branch, add -u:

git push -u origin my_preferred_branch

If you make a mistake, you can remove the remote origin:

git remote rm origin

If that doesn't work you can remove the .git file from the directory. This will delete your version history so only do this if you just made the mistake.

Downloading From GitHub

To see what remote repository a directory is connected to:

git remote -v

To download a repository:

git pull origin master [email protected]:tdkehoe/my_repository.git

Why does git remote -v tell you to fetch instead of pull?

Forking Someone's Else's GitHub Repository

In GitHub, find the user you're looking for, then find the repository you want to work on. In the upper right you'll see a button to fork the repository:

Screen Shot 2015-04-26 at 2.22.07 PM

Now you should have a new repository in your GitHub account. Copy the SSH clone URL to your clipboard, and enter the git clone command:

git clone [email protected]:tdkehoe/their_repository.git

You don't need to make a directory for the repository, git will do this for you.

After you've cloned the repository, you can then download more stuff with git pull:

git pull origin master [email protected]:tdkehoe/their_repository.git

Now you should have a local repository (on your computer).

To send your changes to the origin (the original owner), make a pull request. Pull requests are not part of Git, they are unique to GitHub. (Anything involving social skills is GitHub, not Git.)

Pull requests are the green button on the left, in your repository (not in the other person's directory).

Screen Shot 2015-04-26 at 2.19.56 PM

Add a comment explaining your changes.

Deleting Repositories

There's no command to delete a local repository. Instead, go to the root-directory of your repository and enter

rm .git

You could use ls -a to show the hidden files.

To delete a remote directory, go to the repository's page in GitHub and look for Settings at the bottom of the right column:

Screen Shot 2015-04-26 at 3.31.43 PM

In the Settings page, look for the red Danger Zone on the bottom:

Screen Shot 2015-04-26 at 3.33.05 PM

Click Delete this repository and follow the instructions.

Don't Initialize Recursive Repositories

I wanted to clone a local directory:

cp -R project/ new_project

cd new_project

git init

git add -A

git commit -m "Initial commit"

git push origin master ...

The result on GitHub was this:

Screen Shot 2015-04-26 at 3.11.26 PM

Note that the folder is grayed out, can't be opened, and shows a folder inside a folder. (The blue folder just shows one folder.) git status showed the directory website twice, once green and listed as a file (not as a directory), and again in red as unstaged. (I couldn't reproduce this to get a screenshot.)

I googled a while and learned that I had an "unreachable submodule," whatever that meant. At that point I gave up and asked Evan, who told me that I had an older repository in the subdirectory website, inside the new repository for my directory project.

Undoing Stuff

To unstage a file that has not been committed, or has been committed but not uploaded:

git reset HEAD file.name

To revert to the last saved version of your file:

git checkout -- file.name

Be careful with checkout, it can't be undone.


That's a basic of Git and GitHub. More stuff later...

And then there's Repo Man:

https://www.youtube.com/watch?v=DLGrXGEMOSo

#Branching and Merging

To create a new local branch:

git branch dolphins

To move to the new branch:

git checkout dolphins

To move back to the master branch:

git checkout master

To show all branches:

glog

or

glog --all

To upload the new branch to GitHub:

git push origin dolphins
⚠️ **GitHub.com Fallback** ⚠️