Back to Tutorials

Git Basics: Clone, Add, Commit, Fetch, and Push

This tutorial will guide you through the basic Git operations: cloning a repository, making changes, adding and committing those changes, fetching updates, and pushing your changes back to GitHub.

Prerequisites

Before you begin, make sure you have:

  • A GitHub account
  • A repository on GitHub (follow our Create a Repository tutorial if needed)
  • Git installed on your computer (download from git-scm.com)
  • A text editor or IDE of your choice

Step 1: Clone the Repository

Cloning creates a local copy of your repository on your computer.

Step 1.1: Get the repository URL

Navigate to your repository on GitHub and click the green "Code" button. Copy the HTTPS or SSH URL.

Screenshot showing the Code button on GitHub

Step 1.2: Open your terminal

Open your terminal (macOS/Linux) or command prompt (Windows). Navigate to the directory where you want to clone the repository.

cd ~/Documents/GitHub

Step 1.3: Clone the repository

Run the git clone command followed by the repository URL you copied.

git clone https://github.com/yourusername/your-repository.git
Screenshot: GitHub repository page showing the updated files and commit history

Step 1.4: Navigate into the cloned repository

After cloning is complete, navigate into the repository directory.

cd your-repository

Step 2: Make Changes to Your Files

Now that you have a local copy of your repository, let's make some changes.

Step 2.1: Open the repository in your text editor

Open the repository folder in your preferred text editor or IDE.

Screenshot: GitHub repository page showing the updated files and commit history

Step 2.2: Modify an existing file or create a new file

Let's modify the README.md file or create a new file in your repository.

# my-first-repo
My First Repository
# This is the repository for my GitHub tutorial.

After making changes, save the file(s).

Step 3: Add and Commit Changes

Now that you've made changes, you need to add them to the staging area and commit them to your local repository.

Step 3.1: Check the status of your changes

Run the git status command to see which files have been modified.

git status
Screenshot: GitHub repository page showing the updated files and commit history

Step 3.2: Add files to the staging area

Use the git add command to add your changes to the staging area. You can add specific files or all changed files.

Add a specific file:

git add README.md

Or add all changes:

git add .

Step 3.3: Verify files are staged

Run git status again to verify that your files are staged for commit.

git status

Step 3.3: Commit your changes

Use the git commit command to commit your staged changes with a descriptive message.

git commit -m "Update README with project description"
Screenshot: GitHub repository page showing the updated files and commit history

The commit message should be clear and describe what changes you made.

Step 4: Fetch and Pull Updates

Before pushing your changes, it's good practice to fetch and pull any updates from the remote repository.

Step 4.1: Fetch changes from the remote repository

The git fetch command downloads changes from the remote repository but doesn't integrate them into your working files.

git fetch origin

Step 4.2: Pull changes from the remote repository

The git pull command fetches changes from the remote repository and integrates them into your local branch.

git pull origin main
Screenshot: GitHub repository page showing the updated files and commit history

Note: Handling Merge Conflicts

If you get an error about "Merge (Safe & Common for Beginners)", you can use the following alternative:

git pull --no-rebase origin main

Or permanently set the behavior for all future pulls in this repository:

git config pull.rebase false

(Or use --global to apply it to all repositories.)

Step 5: Push Changes

Now that you've committed your changes locally and pulled any remote changes, it's time to push your changes to GitHub.

Step 5.1: Push your changes to GitHub

Use the git push command to upload your local commits to the remote repository.

git push origin main
Screenshot: GitHub repository page showing the updated files and commit history

Step 5.2: Verify your changes on GitHub

Navigate to your repository on GitHub and verify that your changes have been pushed successfully.

Screenshot: GitHub repository page showing the updated files and commit history

Next Steps

Congratulations! You've successfully cloned a repository, made changes, committed them, and pushed them back to GitHub. Here are some next steps to continue your Git journey:

Pro Tip

Create a .gitignore file in your repository to specify files and directories that Git should ignore. This is useful for excluding build artifacts, dependency directories, and sensitive information.