Request-Response: Getting Started With Git
Request-Response: Getting Started With Git
In this activity, we will explore configuring the local Git default branch to `main`. We will
also review another way to create a repository, using the `git init` command. Lastly, we will
review the `git pull` command.
Git is an important tool that allows developers to track and store versions of content. It also
enables you to collaborate and share code with others. During this boot camp, you'll use Git to
share code via a class repository, which you'll be required to pull down before each class. In
addition, for each Challenge, you'll provide the grading team with a link to the GitHub
repository that contains your code.
For more information, review the [Full-Stack Blog guide on getting started with Git]
(https://coding-boot-camp.github.io/full-stack/git/getting-started-with-git).
Request-Response
The Full-Stack Blog
Swiss
Git has a steep learning curve, but with practice, using it can become second nature to you. To help you get
started, this guide explains how to set up your Git environment and introduces some commonly used Git and
terminal commands.
First, we'll walk through how to set up your Git username, which enables Git to associate commits with your
identity.
To set up your Git username and email address, you'll use the git config command and set it globally so
that it is applied to an operating system user.
1
Important: You only need to do this once.
For example, if you wanted to set your Git username to "lernantino," you would type the following
command:
2. To set your Git email address, type the following Git command:
For example, if you wanted to set your Git email address to "[email protected]," you would
type the following command:
So what's the connection between this local Git user account and GitHub? GitHub uses the email address
that you set in your local Git configuration to associate commits pushed from the command line to your
GitHub account.
To check your current Git configurations, type the following Git command:
Note: You can use the git config command to change your Git username and email address at any time,
but any commits you made previously will still be associated with your previous username and email
address.
In order to sync with GitHub's default branch naming convention, we need to manually set our local default
branch to main. Initially, this will have been set to master.
git --version
If you have Git version 2.28 or later, you first need to update Git.
For Windows users, visit Downloading Git and download and install the latest "64-bit Git for
Windows Setup" file.
For macOS users, use Homebrew to update your version of Git:
2
brew upgrade git
You will not get a confirmation message. If the configuration is successful, it will simply return back to the
command-line prompt.
Create the remote repository on GitHub first and clone it to your local machine.
Initialize a local repository on your local machine first and then connect it to a remote repository on
GitHub.
You've successfully created the remote repository in GitHub. Now you need to clone that repository on your
local machine in order to work in it. Cloning a repository pulls down a full copy of all the repository data
that GitHub has at that point in time.
1. Navigate to the main page of the new repository that you just created in your browser.
2. Click the green "Code" button and select the HTTPS option to copy the URL ending in .git. Or if
you have GitHub SSH keys set up, you can select the SSH option to copy that URL.
3. On your local machine, in the command line, navigate to the parent directory where you want to
store this project.
4. Use the git command git clone followed by the URL that you copied from GitHub, as in the
following example:
3
git clone <the HTTPS or SSH URL ending in .git>
The git clone command creates a new directory with the same name as the repository.
Now you are all set to work on this repository on your local machine!
1. Start by creating a new project directory on your local machine. For example, if you want to create a
new repository called "git-init-sample", you would type the following command:
mkdir git-init-sample
2. Next, use cd to navigate into the new directory, and add a README.md file using the touch command:
cd git-init-sample
touch README.md
3. To initialize version control in this project, use git init to initialize it, which means you are
turning the directory into a Git repository. It is important that you are in the root directory of the
project when you run this command!
git init
This creates a new subdirectory named .git that contains all of the necessary repository files—a Git
repository skeleton. However, at this point, nothing in your project is tracked yet. You will need to add and
commit the files now.
git status
2. You should see that your README.md file is currently untracked. Add that file to be tracked by typing
the following command:
git add .
3. Now if you run git status again, you should see that the file is being tracked and is ready to be
committed:
Now you are ready to connect your local repository to a remote repository on GitHub! To do so, follow
these steps:
1. Follow the same steps as above to create a new repository on GitHub and use the same project
name, git-init-sample, in the "Repository Name" box.
4
Important: You are importing an existing repository, so do not click any of the checkboxes!
git remote add origin <the HTTPS or SSH URL ending in .git>
git branch -M main
git push -u origin main
Note: If you have successfully set your local default branch to main already, you do not have to run
the git branch -M main command, which sets the local default branch to main.
git status
The git status command shows the current state of the working directory and the staged changes. You
can see which changes have been staged, which changes aren't staged, and which files aren't being tracked.
You should get in the habit of using this command frequently, especially before adding and committing
files.
git status
git add
The git add command adds a change in the working directory to be staged and ready to be committed. If
you add a period to the end of the command (for example, git add .), it will add any untracked or
modified files in the current directory (the current directory is represented by .) and all subdirectories to be
staged.
git add .
git commit
The git commit command captures a snapshot of the currently staged changes. You should always include
a descriptive commit message for the changes that are about to be saved. You can use the shortcut
command git commit -m "commit message" to create a commit with a commit message.
git push
5
The git push command pushes up the local repository content to a remote repository. After the files have
been added and committed, they must be pushed up to the remote repository on GitHub. In our case, the
remote repository is origin (GitHub), and we want to update the origin's main branch.
git pull
The git pull command is used to pull down the remote repository content to a local repository. When
collaborating with others on a project, it is important to always be working with the most updated code. In
order to ensure that your local repository has the latest changes, you would frequently pull from the remote
repository on GitHub. Just like when we did a git push, we use origin to represent the original directory
—or more precisely the original repository's URL—followed by the name of the branch, which in main.
Change directory
cd <path/to/desired/directory>
Delete file
rm <name of file to remove>
Delete directory
rm -r <name of directory to remove>
Copy file
cp <filename1> <filename2>
Move/rename file
mv <filename1> <filename2>
macOS Commands
This section contains commands that are specific to macOS users.
Windows Commands
This section contains commands that are specific to Windows users.
Open all files and folders in the current directory (Windows only)
explorer .
7
Resources
For more information about Git and GitHub, refer to the GitHub Docs on getting started with Git and
GitHub.
For detailed instructions and more commonly used Git commands, refer to the Atlassian guide on
setting up a repository.
Category: git
Tagged under: git, git commands, terminal commands, bash commands, guide,
All Posts
Localhost Loopback Issues Troubleshooting Guide
The Science and Research Behind Our Unconventional Educational Approach
Deploy with Heroku and MongoDB Atlas
How to Install MongoDB
How to Install the Heroku CLI
Web Literacy
Heroku Deployment Guide
HTML Cheatsheet
API Resources
MySQL Installation Guide
MySQL Reference Guide
How to Use API Keys
Using the GraphQL Playground in a MERN application
Getting Started with Git
Regular Expression Tutorial
Professional README Guide
Deploy with Heroku and MySQL
How to Install NodeJS
Set Up MongoDB Atlas
What Makes Up a Web Development Project?
Video Submission Guide
Developer Resources
A Growth Mindset for Life
Introduction to Computer Structure and Organization
Advanced Computer Skills
Introduction to Computer Skills
Historically, the most common name for the main body of a codebase has been `master`. However,
`main` has been gaining in popularity. In fact, GitHub now uses `main` as the default name for
its repositories - as do the projects in this course.
8
While GitHub has changed their default branch conventions, your local machine will still
initialize projects using the `master` branch. Therefore, you'll need to manually change it to
`main`.
> **Important:** If you've already configured your local default branch to `main`, you don't
need to repeat this step.
To check the version of Git that you have installed on your local machine, enter the following
command in the command line:
git --version
If your version of Git is 2.28 or older, you'll first need to update Git.
To set the default branch to `main`, both Windows and Mac users will run the following command:
You will not get a confirmation message. If the configuration is successful, it will simply
return to the command-line prompt.
We already learned how to create a remote repository on GitHub and clone it onto the local
machine. This time, let's initialize a new repository locally using the `git init` command.
Using `git init` also allows us to turn any existing project into a Git repository easily.
Start by creating a new project directory named `git-init-sample` on your local machine.
Ideally, you should have a parent directory to store all of your projects for this course:
```bash
mkdir git-init-sample
```
Next, use `cd` to navigate into the new directory and add an `index.html` file using the
`touch` command:
```bash
cd git-init-sample
touch index.html
```
To initialize this folder as a Git repository, use `git init`. We need to be in the project
folder when we run this command!
```bash
git init
9
```
This creates a new subdirectory named `.git` that contains all of your necessary repository
files—a Git repository skeleton. At this point, nothing in your project is tracked yet.
To start version-controlling the existing files in your project, you need to start tracking
those files and do an initial commit.
First let's run `git status` to check the status of the files:
```bash
git status
```
We should see that `index.html` is currently untracked. Let's add that file to be tracked:
```bash
git add -A
```
Now if we run `git status` again, we should see that the file is being tracked and is ready to
be committed:
```bash
git commit -m "initial commit"
```
Now we are ready to connect the local repository to a remote repository on GitHub!
> **Important**: Because we are importing an existing repository, make that sure none of the
options are checked!
Click the "Create Repository" button. Then copy the code under the header "…or push an existing
repository from the command line". It should look like the following example:
```bash
git remote add origin <the HTTPS or SSH URL ending in .git>
git branch -M main
git push -u origin main
```
> **Note**: If you successfully set your local default branch to `main`, you do not have to run
the `git branch -M main` command.
Paste the commands into the command line and press Enter.
If successful, you should see a message that looks like the following image:
![A message indicates that the project directory has been successfully imported.]
(./assets/image-8.png)
To perform a `git pull`, first navigate to the corresponding project directory, which in this
case will be `git-init-sample`:
```bash
cd git-init-sample
```
Next, use `git pull` to pull down any changes from the remote `git-init-sample` repository.
Just like when we did a `git push`, we use `origin` to represent the original
directory—or more precisely the original repository's URL—followed by the name of
the branch, which is `main`:
```bash
git pull origin main
```
Right now, the local repository is up to date with the remote repository, so you'll get a
message that says "Already up to date". This means that the local version of your repository is
up to date with the remote version being hosted on GitHub.
If the remote repository has changes that you don't have locally, you will get a message that
lists the changes made, similar to the one shown in the following image:

Both messages indicate that the `git pull` command has been successfully performed.
For each module's Challenge, you'll be required to share the URL of your repository.
To share the URL, navigate to your repository on GitHub. Then copy the URL from the address bar
and share it with your partner in Slack. To view your partner's repository, simply click on the
shared link.
## Notes
---
11
© 2022 Trilogy Education Services, LLC, a 2U, Inc. brand. Confidential and Proprietary. All
Rights Reserved.
x------x-----x-----x------x------
------x-----x-----x-----x-----x-----x-----x------x-----x-----x------x------
------x-----x-----x-----x-----x-----x-----x------x-----x-----x------x------
------x-----x-----x-----x-----x-----x-----x------x-----x-----x------x------
------x-----x-----x-----x-----x-----x-----x------x-----x-----x------x------
12