Request-Response

The Full-Stack Blog

Getting Started with Git

November 14, 2023
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.

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 "lernantino@email.com," you would type the following command:

    git config --global user.email "lernantino@email.com"

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:

    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 oftenevery 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:

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

    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

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 directoryor more precisely the original repository's URLfollowed 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

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 .

Resources

This page was updated 4 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