0% found this document useful (0 votes)
45 views12 pages

Request-Response: Getting Started With Git

1) The document discusses how to set up Git configuration on a local machine, including setting the username, email, and default branch (main). 2) It explains how to create a new remote repository on GitHub and clone it to the local machine to work on locally. Alternatively, a local repository can be initialized and then linked to a remote GitHub repository. 3) Detailed steps are provided to configure Git, set the default branch to main, clone or initialize a local repository, and connect it to a remote GitHub repository.

Uploaded by

anuraag saini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views12 pages

Request-Response: Getting Started With Git

1) The document discusses how to set up Git configuration on a local machine, including setting the username, email, and default branch (main). 2) It explains how to create a new remote repository on GitHub and clone it to the local machine to work on locally. Alternatively, a local repository can be initialized and then linked to a remote GitHub repository. 3) Detailed steps are provided to configure Git, set the default branch to main, clone or initialize a local repository, and connect it to a remote GitHub repository.

Uploaded by

anuraag saini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Week 1 – Evr_GIT GUIDE

# Set Up a Local Repository 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

Getting Started with Git


October 27, 2022
Disponible en español

Getting Started with Git


Code files are typically collaborative, as it is common for developers to build on each other's work. To
facilitate this collaboration and ensure code integrity, many developers use Git for version control, and
GitHub to securely store and share their code online.

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.

Set Up Your Git Username and Email Address


Git associates commits with an identity that is determined by a username and email address. This Git
username is not the same as your GitHub username, but you should use the same email address that is
associated with your GitHub account so that your commits there are attributed to you.

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.

To set your Git username, follow these steps:

1. At the command line, type the following Git command:

git config --global user.name "<your-username>"

For example, if you wanted to set your Git username to "lernantino," you would type the following
command:

git config --global user.name "lernantino"

2. To set your Git email address, type the following Git command:

git config --global user.email "<your-email-address>"

For example, if you wanted to set your Git email address to "[email protected]," you would
type the following command:

git config --global user.email "[email protected]"

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:

git config --list

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.

Set the Local Git Default Branch to main


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 branch for its repositories.

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.

Check and Update Git Version


First, check the current version of your local copy of Git by typing the following command:

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

Set the Default Branch to main


To set the default branch to main, both Windows and macOS users will run the following command:

git config --global init.defaultBranch main

You will not get a confirmation message. If the configuration is successful, it will simply return back to the
command-line prompt.

Create a New Repository


Creating a repository is something you'll need to do often—every time you start working on a new project,
in fact. You can create a repository in the following two ways:

 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.

Read on for instructions on how to do both.

Clone a Remote Repository


Follow these steps to create a new remote repository on GitHub and clone it to your local machine:

1. In your browser, navigate to GitHub and log in to your account.


2. To create a new repository, click the green New button on the top-left or the plus + icon on the top-
right of the screen.
3. Type the name of your project/repository in the "Repository Name" box. You can also add a
description, which is optional.
4. Choose the repository visibility. You will mostly likely want to keep it Public, but you can choose to
make it Private.
5. Check the "Add a README file" checkbox to add a README.md file to the new repository. You can
edit the content of this README later in your code editor. You can also create a .gitignore file
and/or a license for your project.
6. Click the "Create repository" button.

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.

Follow these steps to clone the repository:

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!

Initialize a New Local Repository


The other way to create a repository is to initialize one locally by using the git init command and then
push it up to GitHub. You can use git init to turn any existing project into a Git repository.

Follow these steps to initialize a local repository:

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.

To add and commit the files, follow these steps:

1. Run git status to check the status of your files:

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:

git commit -m "initial commit"

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!

2. Click the "Create Repository" button.


3. Then copy the code under the header "or push an existing repository from the command line" to the
clipboard. It should look similar to the following:

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.

4. Paste the commands in the command line and press Enter.

That's it! You've successfully connected your local repository to GitHub!

Common Git Commands


This section contains some commonly used Git commands. The more familiar you can become with these,
the shorter your learning curve with Git will be.

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 commit -m 'initial commit'

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 push origin main

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.

git pull origin main

Common Terminal/Bash Commands


This section contains some basic commands that you're likely to encounter as you work in the command
line.

Commands for Moving Around in Directories


This section contains commands that let you move among files and folders.

Change directory
cd <path/to/desired/directory>

Change to home directory


cd ~

Move one directory up


cd ..

View folders and files in the current directory


ls

Show the current directory


pwd

Autocomplete a file name in the current directory


Press the tab key once to autocomplete after you have typed a unique portion of a file name.

Commands to Manipulate Files


6
This section contains commands that help you manipulate files and folders, such as creating or deleting
them.

Make new file


touch <name of file to create>

Make new folder


mkdir <name of directory to create>

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.

Open a file or folder (masOS only)


open <name of file>

Open all files and folders in current directory (macOS only)


open .

Windows Commands
This section contains commands that are specific to Windows users.

Open a file or folder (Windows only)


explorer <name of file>

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.

This page was updated 7 months ago


© 2022 edX Boot Camps LLC. Confidential and Proprietary. All Rights Reserved.

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

## Configure Local Git Default Branch to main

First we need to set the local Git default branch to `main`.

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.

* Windows users can visit the [Downloading Git website](https://git-scm.com/download/win) and


download the latest "64-bit Git for Windows Setup" file.

* Mac users can use Homebrew to update their version of Git:

brew upgrade git

To set the default branch to `main`, both Windows and Mac users will run the following command:

git config --global init.defaultBranch main

You will not get a confirmation message. If the configuration is successful, it will simply
return to the command-line prompt.

## Initialize a Local Repository

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&mdash;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!

To create a remote repository to store the code, navigate to [GitHub](https://github.com/) and


create a new repository by clicking on the green New button at the top left and entering the
project name, `git-init-sample`, in the "Repository Name" box.

> **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)

## Pull Changes from the Remote Repository


10
Before each class, it is important that we do a `git pull` to pull down any changes and
activity files from the student repository to the local machine. Let's review how to do just
that.

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&mdash;or more precisely the original repository's URL&mdash;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:

![A message indicates that changes have been made from the remote repository.](./assets/image-
9.png)

Both messages indicate that the `git pull` command has been successfully performed.

You can review the changed files by opening them in VS Code.

## Share Remote Repository URL

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

Ask an instructor or TA if you get stuck or have any questions!

For more information, refer to the [Atlassian guide on setting up a repository]


(https://www.atlassian.com/git/tutorials/setting-up-a-repository).

For more information, read the [Atlassian guide on git pull]


(https://www.atlassian.com/git/tutorials/setting-up-a-repository/git-config).

---

11
© 2022 Trilogy Education Services, LLC, a 2U, Inc. brand. Confidential and Proprietary. All
Rights Reserved.

---x-----x-----x-----x-----x----- Week 2 – GIT

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

You might also like