Pushing and Pulling Changes with GitHub

Learn how to push local commits to a remote GitHub repository (`git push`) and pull changes from GitHub to your local machine (`git pull`).


Learn Git and GitHub - Basic Operations

Creating a Local Repository

A local repository is where your project's files and their history are stored on your computer. Think of it as your own private copy of the project. To create one, follow these steps:

  1. Choose a Project Directory: Decide where you want to store your project files. This could be a new folder or an existing one.
  2. Open Your Terminal: Open your terminal (Command Prompt on Windows, Terminal on macOS/Linux) and navigate to your project directory using the cd (change directory) command. For example:
    cd /path/to/your/project
  3. Initialize the Repository: Run the following command to turn the directory into a Git repository:
    git init
    This command creates a hidden directory named .git inside your project directory. This .git directory is where Git stores all the information about your repository, including your commit history, configurations, and other metadata. Do not modify this directory directly! Let Git manage it.

After running git init, you should see a message similar to: "Initialized empty Git repository in /path/to/your/project/.git/" This confirms that the repository has been successfully created.

Initializing a New Git Repository, Creating Files, Staging Changes, and Committing Them Locally

Now that you have a repository, let's add some files, stage them, and commit them. This is the basic workflow for tracking changes in Git.

  1. Create Files: Create some files in your project directory. These can be any type of file (text files, code files, images, etc.). For example, you could create a file named README.md using a text editor or the command line:
    touch README.md  # On macOS/Linux
    notepad README.md # On Windows (opens Notepad)
    Then add some text to the file like "My awesome project".
  2. Check the Status: Use the git status command to see which files Git is tracking. Initially, Git will show that your new file is untracked.
    git status
    You will see output similar to:
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            README.md
  3. Stage Changes: Staging is the process of selecting which changes you want to include in your next commit. Use the git add command to stage your new file.
    git add README.md
    To stage all changes in your project (including new and modified files), you can use:
    git add .
    After staging, git status will show the file as staged for commit.
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            new file:   README.md
  4. Commit Changes: A commit is a snapshot of your project at a specific point in time. Use the git commit command to create a commit. You must provide a commit message to describe the changes you made. The message should be concise and informative.
    git commit -m "Add README.md file with project description"
    The -m flag allows you to specify the commit message directly in the command. You'll get an output like:
    [master (root-commit) a1b2c3d] Add README.md file with project description
     1 file changed, 1 insertion(+)
     create mode 100644 README.md

Congratulations! You have successfully created a local Git repository, added a file, staged the changes, and committed them. You've taken your first steps towards using version control!