[TUTORIAL]Git & Github
Step 1: Install Git
Windows:It's recommended to download GitHub for Windows, which includes Git1 and has an easier install : windows.github.comUse the Git Shell for your terminal. Linux2: Debian-like-Distro: -
$ sudo apt install git RPM-like-Distro: -
$ sudo dnf install git(Fedora23/24) or -
sudo yum install git(CentOS)
Step 2: Configure Git Once it is installed, open terminal (aka Bash, aka Shell, aka Prompt). You can verify that it's really there by typing: -
$ git --version
This will return the version of Git that you're running and look something like this: -
Next, configure Git so it knows who to associate your changes to:
Set your name: -
$ git config --global user.name ""
Now set your email: -
$ git config --global user.email "<email@example.com>"
Repositories A repository is essentially a >project. You can imagine it as a >project's folder with all the >related files inside of it. You tell Git what your project is and Git will start tracking all of the changes to that folder. Files added or subtracted or even a single letter in a single file changed -- all of it's tracked and time stamped by Git. That's version control.
Step 3: Create a Repository
You're going to create a new folder and initialize it as a Git repository.
To make things easier, name your folder what you'd name the project. How about 'hello-world'.
You can type these commands one at a time into your terminal window.
To make a new folder: -
$ mkdir hello-world
To go into that folder: -
$ cd hello-world
To create a new Git instance for a project: -
$ git init
That's it! It will just return you to a new line. If you want to be double-sure that it's a Git repository, type
git status and if it doesn't return 'fatal: Not a git repository...', you're done with initializing a new Git Repository.
Commit To It
Step 4: Create a New File
Now that you've got a repository started, add a file to it.
Open a text editor. Now write a couple of lines of text, perhaps say hello, and save the file as
readme.txt in the 'hello-world' folder you created earlier.
Step 5: Status, Add and Commit Changes
Next check the status of your repository to find out if there have been changes. Below in this terminal, you should still be within the 'hello-world' you created. See if there are changes listed:
$ git status
Then add the file you just created to the files you'd like to commit (aka save) to change.
$ git add readme.txt
Finally, commit those changes to the repository's history with a short description of the updates.
$ git commit -m ""
Step 6: Make more Changes
Now add another line to
readme.txt and save.
In terminal, you can view the difference between the file now and how it was at your last commit. -
$ git diff
Now with what you just learned above, commit this latest change.
The repository you've created so far is just on your computer, which is handy, but makes it pretty hard to share and work with others on. No worries, that's what GitHub is for!
Step 7: Create a Github Account
GitHub is a website that allows people everywhere to upload what they're working on with Git and to easily work together.
Visit github.com and sign up for a free account. Welcome to Github.
Step 8: Add username to Git
Add your GitHub username to your Git configuration, which will be needed in order to verify.
Save it exactly as you created it on GitHub — capitalize where capitalized, and remember you don't need to enter the "<" and ">" .
Add your GitHub username to your configuration: -
$ git config --global user.username "
When you put something on GitHub, that copy lives on one of GitHub's servers. This makes it a remote repository because it is not on your computer, but on a server, "remote" and somewhere else. By pushing your local (on your computer) changes to it, you keep it up to date.
Others can always get the latest from your project by pulling your changes down from the remote (and onto their computer). This is how everyone can work on a project together without needing access to your computer where your local copy is stored.
Step 9: Create a Remote Repository
You want to sync your local version with one stored on GitHub.com called the remote version. So first create an empty remote repository on GitHub.com.
- Go to github.com, log in, and click the '+' in the top right to create a new repository.
- Give it a name that matches your local repository's name, 'hello-world', and a short description.
- Make it public.
- Don't initialize with a README because we already have a file, locally, named 'readme.txt'.
- Leave .gitignore and license on 'none'.
- Click create repository!
Readmes, .gitignores and Licenses
These are common files in open source projects.
A readme explains what the project is, how to use it, and often times, how to contribute (though sometimes there is an extra file, CONTRIBUTING.md, for those details).
A .gitignore is a list of files that Git should not track, for instance, files with passwords!
A license file is the type of license you put on your project, information on the types is here: choosealicense.com.
We don't need any of them, however, for this example.
Step 10: Connect your Local to your Remote
Now you've got an empty repository started on GitHub.com. At the top you'll see 'Quick Setup', make sure the 'HTTP' button is selected and copy the address — this is the location (address) of your repository on GitHub's servers.
Back in your terminal, and inside of the 'hello-world' folder that you initialized as a Git repository in the earlier challenge, you want to tell Git the location of the remote version on GitHub's servers. You can have multiple remotes so each requires a name. For your main one, this is commonly named
$ git remote add origin
Your local repository now knows where your remote one named 'origin' lives on GitHub's servers. Think of it as adding a name and address on speed dial — now when you need to send something there, you can.
A note: If you have GitHub for Windows on your computer, a remote named 'origin' is automatically created. In that case, you'll just need to tell it what URL to associate with origin. Use this command instead of the 'add' one above:
$ git remote set-url origin
Step 11: Push Work to your Remote
Next you want to push (send) everything you've done locally to GitHub. Ideally you want to stay in sync, meaning your local and remote versions match.
Git has a branching system so that you can work on different parts of a project at different times. We'll learn more about that later, but by default the first branch is named 'master'. When you push (and later pull) from a project, you tell Git the branch name you want and the name of the remote that it lives on.
In this case, we'll send our branch named 'master' to our remote on GitHub named 'origin'.
$ git push origin master
Now go to GitHub and refresh the page of your repository. WOAH! Everything is the same locally and remotely. Congrats on your first public repository!