Quick & Dirty -- Git

Aus RZ-Amper Wiki
Zur Navigation springen Zur Suche springen

Setting Up GitHub And Git For The First Time

First, you’ll need to sign up for an account on GitHub.com. It’s as simple as signing up for any other social network. You could stop there and GitHub would work fine. But if you want to work on your project on your local computer, you need to have Git installed. In fact, GitHub won’t work on your local computer if you don’t install Git. Install Git for Windows, Mac or Linux as needed.

Now it’s time to go over to the command line. On Windows, that means starting the Git Bash app you just installed, and on OS X, it’s regular old Terminal. It’s time to introduce yourself to Git. Type in the following code:

git config --global user.name "Your Name Here"
git config --global user.name "Your Name Here"
git config --global user.email "your_email@youremail.com"

Of course, you’ll need to replace “Your Name Here” with your own name in quotations. It should be your legal name. Git doesn’t care, it just needs to know to whom to credit commits and future projects.

Creating Online Repository on Github

First order of business, we're going to create a repository. Head on over to [1] and click the "New Repository" button on the top right of your account page. (Note: If you're still displaying the GitHub bootcamp section, it'll show up underneath it.)

When creating a repository you have a few things to decide including it's name and whether it'll be publicly accessible or not. Choosing a name should be pretty simple because you likely already have a name for your project. If you're just following along for learning purposes, use "Rep01." Why "Rep01" and not "Rep 01"? Because spaces and special characters will cause problems. Keep it simple and easy to type in the command line. If you want to include a more complex name, you can add it to the optional description field beneath the name field.

When you're all done, you can click the "Create repository" button but you might want to do one other thing first: check the "Initialize this repository with a README" checkbox. Why? All repositories require a README file. Ideally that file would contain a little information about your project, but you might not want to deal with that right now. By initializing the repository with a README, you'll get an empty README file that you can just deal with later.

Creating Local Repository

So we just made a space for your project to live online, but that’s not where you’ll be working on it. The bulk of your work is going to be done on your computer. So we need to actually mirror that repository we just made as a local directory.

This—where we do some heavy command line typing—is the part of every Git tutorial that really trips me up, so I’m going to go tediously, intelligence-insultingly slow.

mkdir ~/Rep01
cd ~/Rep01
git init

git init stands for “initialize.” Remember how the previous two commands we typed were general command-line terms? When we type this code in, it tells git to recognize this directory as a local Git repository. If you open up the folder, it won’t look any different, because this new Git directory is a hidden file inside the dedicated repository. However, Git now realizes this directory is Git-ready, and you can start inputting Git commands. Now you’ve got both an online and a local repo for your project to live inside.

Commit, Push And Go

Now that these steps have been accomplished, let's add the first part of your project now by making your first commit to GitHub. When we last left off, we'd created a local repository called MyProject, which, when viewed in the command line, looks like the following

fenrjer~# mkdir ~/Rep01
fenrjer~# cd ~/Rep01
fenrjer~/Rep01# git init
Initialized empty Git repository in /root/Rep01/.git/
fenrjer~/Rep01#

On your next line, type:

touch Readme.txt

This, again, is not a Git command. It’s another standard navigational command prompt. touch really means “create.” Whatever you write after that is the name of the thing created. If you go to your folder using Finder or the Start menu, you’ll see an empty Readme.txt file is now inside. You could have also made something like “Readme.doc” or “Kiwi.gif,” just for kicks.

You can clearly see your new Readme file. But can Git? Let’s find out. Type:

git status

The command line, usually so passive up to this point, will reply with a few lines of text similar to this:

# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       Readme.txt
nothing added to commit but untracked files present (use "git add" to track)
fenrjer~/Rep01#

What’s going on? First of all, you’re on the master branch of your project, which makes sense since we haven’t “branched off” of it. There’s no reason to, since we’re working alone. Secondly, Readme.txt is listed as an “untracked” file, which means Git is ignoring it for now. To make Git notice that the file is there, type:

git add Readme.txt

Notice how the command line gave you a hint there? All right, we’ve added our first file, so it’s time to take a “snapshot” of the project so far, or “commit” it:

git commit -m “Add Readme.txt”

The -m flag simply indicates that the following text should be read as a message. Notice the commit message is written in present tense. You should always write your commands in present tense because version control is all about flexibility through time. You’re not writing about what a commit did, because you may always revert to earlier. You’re writing about what a commit does. Now that we’ve done a little work locally, it’s time to “push” our first commit up to GitHub. “Wait, we never connected my online repository to my local repository,” you might be thinking. And you’re right. In fact, your local repository and your online one are only connecting for short bursts, when you’re confirming project additions and changes. Let’s move on to making your first real connection now.

Connect Local Repository To GitHub Repository

Having a local repository as well as a remote (online) repository is the best of both worlds. You can tinker all you like without even being connected to the Internet, and at the same time showcase your finished work on GitHub for all to see. This setup also makes it easy to have multiple collaborators working on the same project. Each of you can work alone on your own computers, but upload or “push” your changes up to the GitHub repository when they’re ready. So let’s get cracking. First, we need to tell Git that a remote repository actually exists somewhere online. We do this by adding it to Git’s knowledge. Just like Git didn’t acknowledge our files until we used the git add command, it won’t acknowledge our remote repo yet, either. Assume that we have a GitHub repo called “Rep01” located at https://github.com/username/Rep01.git. Of course, username should be replaced with whatever your GitHub username actually is, and myproject should be replaced with the actual title you named your first GitHub repository.

git remote add origin https://github.com/ttiinc/Rep01.git

The first part is familiar; we’ve used git add already with files. We’ve tacked the word origin onto it to indicate a new place from which files will originate. remote is a descriptor of origin, to indicate the origin is not on the computer, but somewhere online. Git now knows there’s a remote repository and it’s where you want your local repository changes to go. To confirm, type this to check:

git remote -v

This command gives you a list of all the remote origins your local repository knows about. Assuming you’ve been with me so far, there should only be one, the Rep01.git one we just added. It's listed twice, which means it is available to push information to, and to fetch information from.

fenrjer~$ mkdir ~/Rep01
fenrjer~$ cd ~/Rep01
fenrjer~/Rep01$ git init
Initialized empty Git repository in /home/wschroeer/Rep01/.git/
fenrjer~/Rep01$ touch Readme.txt
fenrjer~/Rep01$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       Readme.txt
nothing added to commit but untracked files present (use "git add" to track)
fenrjer~/Rep01$ git add Readme.txt
fenrjer~/Rep01$ git remote add origin https://github.com/ttiinc/Rep01.git
fenrjer~/Rep01$ git remote -v
origin  https://github.com/ttiinc/Rep01.git (fetch)
origin  https://github.com/ttiinc/Rep01.git (push)
fenrjer~/Rep01$

Now we want to upload, or “push,” our changes up to the GitHub remote repo. That’s easy. Just type:

git push

The command line will chug through several lines on its own, and the final word it spits out will most likely be “everything up-to-date.”

fenrjer~/Rep01$ git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Username for 'https://github.com': WSchroeer
Password for 'https://WSchroeer@github.com':
No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
Everything up-to-date
fenrjer~/Rep01$

Git's giving me a bunch of warnings here since I just did the simple command. If I wanted to be more specific, I could have typed git push origin master, to specify that I meant the master branch of my repository. I didn't do that because I only have one branch right now. Log into GitHub again. You’ll notice that GitHub is now tracking how many commits you’ve made today. If you’ve just been following this tutorial, that should be exactly one. Click on your repository, and it will have an identical Readme.txt file as we earlier built into your local repository.

Push to Remote Repository

Let's see how simple it is to change files at your local repository on Computer I and then push it to the remote repository.

fenrjer~/Rep01$ printf "\n\nThis Repo ist really just for testing\n\n" >> Readme.txt
fenrjer~/Rep01$ git add *
fenrjer~/Rep01$ git commit -m "Add additional Text to Readme.txt"
[master f6481c6] Add additional Text to Readme.txt
 Committer: Waldemar Schroeer <wschroeer@fenrjer.de.ttiinc.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 4 insertions(+)
fenrjer~/Rep01$ git push origin master
Username for 'https://github.com': WSchroeer
Password for 'https://WSchroeer@github.com':
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 322 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/ttiinc/Rep01.git
   312f98b..f6481c6  master -> master
fenrjer~/Rep01$

Pull from Remote Repository

Let's how simple it is to pull the remote repository to Computer II.

euprx101~/Rep01# git pull origin master
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/ttiinc/Rep01
 * branch            master     -> FETCH_HEAD
Updating 312f98b..f6481c6
Fast-forward
 Readme.txt | 4 ++++
 1 file changed, 4 insertions(+)
euprx101~/Rep01# cat Readme.txt
This is a Test Repo


This Repo ist really just for testing

euprx101~/Rep01#

Clone a repository

Cloning an existing remote repository is as simple as:

git clone https://github.com/ttiinc/Rep01

Git-Specific Commands

Since Git was designed with a big project like Linux in mind, there are a lot of Git commands. However, to use the basics of Git, you’ll only need to know a few terms. They all begin the same way, with the word “git.”

git init
Initializes a new Git repository. Until you run this command inside a repository or directory, it’s just a regular folder. Only after you input this does it accept further Git commands.

git config
Short for “configure,” this is most useful when you’re setting up Git for the first time.

git help
Forgot a command? Type this into the command line to bring up the 21 most common git commands. You can also be more specific and type “git help init” or another term to figure out how to use and configure a specific git command.

git status
Check the status of your repository. See which files are inside it, which changes still need to be committed, and which branch of the repository you’re currently working on.

git add
This does not add new files to your repository. Instead, it brings new files to Git’s attention. After you add files, they’re included in Git’s “snapshots” of the repository.

git commit
Git’s most important command. After you make any sort of change, you input this in order to take a “snapshot” of the repository. Usually it goes git commit -m “Message here.” The -m indicates that the following section of the command should be read as a message.

git branch
Working with multiple collaborators and want to make changes on your own? This command will let you build a new branch, or timeline of commits, of changes and file additions that are completely your own. Your title goes after the command. If you wanted a new branch called “cats,” you’d type git branch cats.

git checkout
Literally allows you to “check out” a repository that you are not currently inside. This is a navigational command that lets you move to the repository you want to check. You can use this command as git checkout master to look at the master branch, or git checkout cats to look at another branch.

git merge
When you’re done working on a branch, you can merge your changes back to the master branch, which is visible to all collaborators. git merge cats would take all the changes you made to the “cats” branch and add them to the master.

git push
If you’re working on your local computer, and want your commits to be visible online on GitHub as well, you “push” the changes up to GitHub with this command.

git pull
If you’re working on your local computer and want the most up-to-date version of your repository to work with, you “pull” the changes down from GitHub with this command.

Web Links

http://readwrite.com/2013/09/30/understanding-github-a-journey-for-beginners-part-1