Understanding the fundamental Git commands — git init, git clone, git pull, git add, git commit, and git push — is essential for efficient version control and collaboration.

Before we dive into definitions, the picture above illustrates  how files move between different stages in Git. It shows:

  1. Local Workspace (Untracked/Modified Files):
    • Where you make changes to files in your project.
    • git add moves files to the staging area.
    • git reset unstaged files if needed.
  2. Stage (Staging Area):
    • Files are prepared for commit.
    • git commit moves staged changes to the local repository.
  3. Local Repository (Committed Changes):
    • Stores commit history locally.
    • git push uploads commits to a remote repository like GitHub or GitLab.
    • git fetch retrieves remote updates without merging.
    • git pull fetches and merges changes from the remote repository into the local workspace.

Key Git Commands

  1. git init: Initializes a new Git repository in your current directory.
  2. git clone: Creates a local copy of an existing remote repository.
  3. git pull: Fetches and merges changes from a remote repository into your current branch.
  4. git add: Stages changes (new, modified, or deleted files) for the next commit.
  5. git commit: Records staged changes along with a descriptive message.
  6. git push: Uploads local commits to a remote repository.

Start a new project with GitHub: 

1️⃣ Initialize a New Repository

Begin by creating a new directory for your project and initializing it as a Git repository:

mkdir my-new-project
cd my-new-project
git init

What happens here?

  • mkdir my-new-project: Creates a new directory named my-new-project.
  • cd my-new-project: Navigates into your new project directory.
  • git init: Initializes an empty Git repository in the current directory.

2️⃣ Clone an Existing Repository

Alternatively, if you’re contributing to an existing project, clone the repository to your local machine:

git clone https://github.com/username/existing-repo.git

What happens here?

  • git clone: Copies the remote repository to your local machine, including all files, commits, and branches.

3️⃣ Pull Latest Changes

Before making changes, ensure your local repository is up-to-date:

git pull origin main

What happens here?

  • git pull origin main: Fetches and merges changes from the remote repository’s main branch into your current branch.

4️⃣ Make Changes and Stage Them

Edit files as needed. After making changes, stage the modified files:

git add filename1 filename2

What happens here?

  • git add filename1 filename2: Stages the specified files, preparing them for commit.

To stage all changes at once:

git add .

5️⃣ Commit Your Changes

After staging, commit your changes with a descriptive message:

git commit -m "Add feature X to improve functionality"

What happens here?

  • git commit -m “message”: Records the staged changes with a message describing what was done.

Writing clear and concise commit messages is vital for project maintainability. Here are some tips:

  • Be Descriptive: Clearly state what changes were made and why.
  • Use Imperative Mood: Start with verbs like “Added,” “Fixed,” “Updated.”
  • Keep It Concise: Aim for a brief summary in the first line, followed by detailed information if necessary.

Example:

git commit -m "Fix crash on startup by initializing config variables"

For more detailed guidelines, refer to 🔗Conventional Commits


6️⃣ Push Changes to Remote Repository

Share your commits by pushing them to the remote repository:

git push origin main

What happens here?

  • git push origin main: Uploads your local main branch commits to the remote repository.

By mastering these commands and adhering to best practices, you’ll establish a strong foundation in Git, enabling efficient collaboration and version control. 

🎥 Watch this How To Clone Git Project (The right way) for a step-by-step guide!

💬 Have questions or feedback? Drop a comment below! 🚀