GitHub basics

Using GitHub from the Command Line

To interact with GitHub from your terminal or command prompt, you’ll need to have Git installed. If you haven’t done this yet, refer to previous sections of this guide for installation instructions.

Access the Command Line Tool
  • For Windows users, to open Command Prompt, press the Windows key and type “Command Prompt”, and hit Enter.
  • For macOS users, open Spotlight Search by pressing and space bar simultaneously, then type “Terminal” and hit Enter.
  • For Linux users, you can use the search feature in your distribution’s application menu and type “Terminal”, or you can use the keyboard shortcut Ctrl + Alt+ T.

With Git installed, let’s cover some basic command line interaction with GitHub:

Setting Up Your Git Configuration (only the first time)

Before you start using Git, you should configure your name and email address as they’ll be attached to your commits:

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

Replace “Your Name” and “your-email@example.com” with your GitHub username and associated email address.

Creating a New Repository

  1. Go to GitHub’s website and click the + button in the upper-right corner, then New repository.
  2. Give your repository a name, optional description, choose to make it public or private, then click Create repository.

Cloning a Repository

To clone (download) a repository to your local machine:

git clone https://github.com/username/repository.git

Replace ‘username’ and ‘repository.git’ with the username and repository name from GitHub.

After cloning, navigate into the newly created directory by cd repository (replace ‘repository’ with your repository’s name).

Making Changes and Committing

Whenever you make changes to a file, Git recognizes that a file has changed in your repository. To commit these changes:

  1. Add the files to the staging area with git add . The . tells Git to add all the changed files.
  2. Commit the changes with a message describing the changes you’ve made using git commit -m "Your descriptive message here".

Pushing Changes to GitHub

To upload your changes to GitHub:

git push origin branch-name

Replace ‘branch-name’ with the name of the branch you’re pushing to.

More Git Commands

Two important Git commands that can help you better understand and manage your project’s changes are git status and git diff.

Git Status

git status shows you the status of changes as untracked, modified or staged. It’s a way to see what changes are queued for the next commit stage. When you run git status, Git will show you which branch you’re on, what changes exist in your working directory, and what changes have been staged.

Here’s how to use it:

  1. Navigate to your git directory using cd in your terminal or command prompt.
  2. Type git status and hit Enter.

You should now see a list of any modified or untracked files.

Git Diff

git diff shows you the differences between your staged changes and your last commit. By using git diff, you can see line-by-line what has been added or removed from each file since the last commit.

Here’s how to use it:

  1. Navigate to your git directory using cd in your terminal or command prompt.
  2. Type git diff and hit Enter.

Now, you’ll be able to see exactly what changes were made. Green lines represent additions to files, while red lines represent deletions.

Remember, if you want to see the differences between your working directory and the staging area (the changes that haven’t been staged yet), simply use git diff without any files specified. However, if you want to see the differences in the staging area or between commits (the changes that have been staged), you should use git diff --staged.

‘git status’ and ‘git diff’ are both essential commands for understanding your project’s changes and managing your staging area. Make sure to use these regularly to keep track of what’s going on in your repository.

Creating a New Branch

Branches allow you to work on different versions of your project simultaneously. To create a new branch:

git branch new-branch

Replace ‘new-branch’ with your desired branch name.

Switching Between Branches

To switch from your current branch to another branch:

git checkout branch-name

Replace ‘branch-name’ with the name of the branch you want to switch to.

Basic Exercises

If you run into any issues, remember: your best friends are often the documentation, a quick web search (StackOverflow) or ChatGPT. The answers are almost always out there!

(Advanced) Introduction to Pull Requests

A pull request is a important feature in collaborating with Git and GitHub. It’s the way you can suggest changes you’ve made in a branch to be reviewed and potentially merged into another branch, usually the main branch.

Usually, pull requests are made when you’ve finished working on a feature or fix and you believe it’s ready to be included in the main codebase. They are called ‘pull requests’ because you’re asking the repository maintainer to ‘pull’ your changes.

Here’s a basic flow of how to create a pull request on GitHub:

  1. Fork the repository or create a new branch in the repository where you want to make changes.
  2. Make your changes in your branch. This could involve editing files, adding files, or removing files.
  3. Push these changes to your GitHub repository.
  4. Go to the main page of the GitHub repository where you want to propose changes.
  5. Click on the Pull request button.
  6. Click on the New pull request button.
  7. Select your fork or branch on the right dropdown menu and the main repository or branch on the left.
  8. Review your changes and if everything looks good, click on the Create pull request button.
  9. Add a title to your pull request and describe the changes you’ve made, then click Create pull request.

Once a pull request has been opened, the repository owner or collaborators can review the changes, discuss potential modifications, and even push follow-up commits if necessary.

Then, when all parties agree that the changes are ready, the owner or authorized collaborator can merge your changes into the original branch.

Remember, properly communicating what changes you’ve made and why you made them in the pull request description is an integral part of effective collaboration.

Summary of the commands

Command Description
git config --global user.name "name" Set your name in git configuration (only once)
git config --global user.email "email" Set your email in git configuration (only once)
git clone https://github.com/username/repository.git Clone a GitHub repository to your local machine
git branch branch-name Create a new branch
git checkout branch-name Switch to another branch
git add . Track all changes in directory
git commit -m "commit message" Commit your tracked changes
git push origin branch-name Push your local commits to the GitHub
git pull origin branch-name Fetch the newest updates from the remote branch
git status Check the status of your local repository
git diff Show changes between your working directory and the last commit
git diff --staged Show changes between your staging area and the last commit

Above are the basic Git commands you’ll frequently use which cover most of the general use cases from cloning repositories to making changes and updating your remote branches. Each command is a powerful tool in Git, and they are designed to work together to create a seamless workflow.

Remember, the best way to become comfortable with Git is practice. Try using these commands to manage a test project and experiment until you’re confident with the functionality of each one.