Introduction to Git: A Beginner’s Guide

Welcome to the Git crash course! Git is a powerful version control system used by developers worldwide to manage and track changes to their codebase efficiently. Whether you’re a seasoned developer or just starting your coding journey, understanding Git is essential for collaborating on projects and maintaining a well-organized codebase.

In this crash course, we’ll cover everything you need to know to get started with Git, from setting up your environment to mastering basic and advanced commands. By the end, you’ll have the knowledge and confidence to leverage Git for your projects effectively.

Let’s dive in and explore the world of version control with Git!


Setting Up Git

  • Install Git on your system if you haven’t already.
  • Configure your username and email:
    git config --global user.name "Your Name"
    git config --global user.email "your.email@example.com"
    

Initializing a Repository

  • Create a new repository:
    git init my_project
    cd my_project
    

Basic Commands

  • Check the status of your repository:
    git status
    
  • Add changes to the staging area:
    git add <file>
    
  • Commit changes to the repository:
    git commit -m "Commit message"
    
  • View commit history:
    git log
    

Working with Branches

  • Create a new branch:
    git branch new_feature
    
  • Switch to a different branch:
    git checkout new_feature
    
  • Merge branches:
    git merge <branch_name>
    

Remote Repositories

  • Add a remote repository:
    git remote add origin <remote_url>
    
  • Push changes to a remote repository:
    git push -u origin <branch_name>
    
  • Clone a repository:
    git clone <repository_url>
    

Resolving Conflicts

  • When merging branches, conflicts may arise. Resolve conflicts manually in the affected files, then:
    git add <resolved_file>
    git commit -m "Resolved conflicts"
    

Undoing Changes

  • Discard changes in the working directory:
    git checkout -- <file>
    
  • Undo the last commit:
    git reset HEAD~1
    

Miscellaneous

  • Create a .gitignore file to specify files to ignore:

    touch .gitignore
    echo "file_to_ignore.txt" >> .gitignore
    
  • View the difference between files:

    git diff <file>
    

Example Workflow

Initialize a repository

git init my_project
cd my_project

Create a new file and add some content

Add the file to the staging area:

git add new_file.txt

Commit the changes

git commit -m "Added: new_file.txt"

Create a new branch for a new feature

git branch new_feature

Switch to the new branch

git checkout new_feature

Make changes, add them, and commit

git add modified_file.txt
git commit -m "Implemented new feature"

Switch back to the main branch and merge the new feature

git checkout main
git merge new_feature

Resolve any conflicts if they occur

Push changes to a remote repository

git remote add origin <repository_url>
git push -u origin main

This crash course covers the fundamental concepts and commands in Git to get you started. Practice these commands to become comfortable with Git’s workflow.

Last updated 22 Sep 2024, 12:15 CEST . history