Mini course about git- João Cláudio Nunes Carvalho

Introduction to Git Git is a distributed version control system (DVCS) that helps you track changes in your files, coordinate work among multiple people, and revert to earlier versions of your project if needed. It's incredibly powerful and widely used in the software industry. Why use Git? Version Control: Keep a history of every change made to your project. Collaboration: Multiple people can work on the same project simultaneously without overwriting each other's work. Branching & Merging: Experiment with new features without affecting the main project. Backup: Your entire project history is stored in a repository. Open Source: Git itself is open source and free to use. Installation Before we begin, you need to have Git installed on your system. Windows: Download the installer from the official Git website: https://git-scm.com/download/win macOS: Using Homebrew: brew install git Download from official website: https://git-scm.com/download/mac Linux (Debian/Ubuntu): sudo apt-get install git After installation, open your terminal or command prompt and type: git --version You should see the installed Git version. Initial Configuration Once Git is installed, you need to configure your user name and email address. This information will be attached to every commit you make. git config --global user.name "Your Name" git config --global user.email "your.email@example.com" You can check your configurations with: git config --list Creating a New Repository A "repository" (or "repo") is where Git stores all the project files and their history. Scenario: Let's create a new project called my_first_git_project. Create a new directory: mkdir my_first_git_project cd my_first_git_project Initialize a Git repository: This command creates a hidden .git directory inside your project, which Git uses to track changes. git init You should see output like: Initialized empty Git repository in /path/to/my_first_git_project/.git/ Basic Workflow: Add and Commit The core Git workflow involves making changes, "staging" them, and then "committing" them. Working Directory: Your actual project files. Staging Area (Index): A temporary area where you prepare changes before committing them. Local Repository: Where your committed changes are stored. Scenario: Let's create a simple README.md file and commit it. Create a file: echo "# My First Git Project" > README.md Check the status: This command shows you the current state of your working directory. git status You'll see README.md listed as "Untracked files." Add to the staging area: This tells Git that you want to include README.md in the next commit. git add README.md Alternatively, to add all changes in the current directory: git add . Check status again: git status Now README.md should be listed under "Changes to be committed." Commit the changes: This takes the staged changes and records them in your local repository. The -m flag is for the commit message, which should describe the changes you made. git commit -m "Initial commit: Added README.md" You'll see output confirming the commit. Viewing History You can see the history of your commits using git log. git log This will show you a list of commits, including the author, date, and commit message. Press q to exit the log view. Modifying and Committing Again Let's make another change and commit it. Scenario: Add a new line to README.md. Modify the file: echo "This is a mini-course about Git." >> README.md Check status: git status README.md will be listed as "modified." Add and commit: git add README.md git commit -m "Added a description to README.md" View history: git log You'll now see two commits in your history. Understanding Branches Branches are a fundamental concept in Git. They allow you to diverge from the main line of development and work on new features or bug fixes without affecting the stable version of your project. By default, you start on the main (or master) branch. Scenario: Create a new branch for a feature. List branches: git branch You'll see * main (or * master), indicating your current branch. Create a new branch: git branch feature/new-page Switch to the new branch: git checkout feature/new-page You can also combine these two steps: git checkout -b feature/new-page Verify current branch: git branch You should now see * feature/new-page. Working on a Branch and Merging Now that you're on a new branch, any commits you make will only affect this branch. Scenario: Add a new file on the feature/new-page branch. Create a new file: echo "This is a new page for our project." > new_page.txt Add and commit: git add new_page.txt git commit -m "Added new_page.txt on feature branch" Switch back to the main branch: git checkout main Notice that new_page.txt is no longer in your working directory. This is because it was committed on feature/new-page. Merge the feature branch into main: git merge feature/new-page Git will try t

Jun 14, 2025 - 13:50
 0
Mini course about git- João Cláudio Nunes Carvalho

Introduction to Git

Git is a distributed version control system (DVCS) that helps you track changes in your files, coordinate work among multiple people, and revert to earlier versions of your project if needed. It's incredibly powerful and widely used in the software industry.

Why use Git?

  • Version Control: Keep a history of every change made to your project.
  • Collaboration: Multiple people can work on the same project simultaneously without overwriting each other's work.
  • Branching & Merging: Experiment with new features without affecting the main project.
  • Backup: Your entire project history is stored in a repository.
  • Open Source: Git itself is open source and free to use.
  1. Installation Before we begin, you need to have Git installed on your system.
    • Windows: Download the installer from the official Git website: https://git-scm.com/download/win
    • macOS:
    • Using Homebrew: brew install git
    • Download from official website: https://git-scm.com/download/mac
    • Linux (Debian/Ubuntu): sudo apt-get install git After installation, open your terminal or command prompt and type: git --version

You should see the installed Git version.

  1. Initial Configuration Once Git is installed, you need to configure your user name and email address. This information will be attached to every commit you make. git config --global user.name "Your Name" git config --global user.email "your.email@example.com"

You can check your configurations with:
git config --list

  1. Creating a New Repository A "repository" (or "repo") is where Git stores all the project files and their history. Scenario: Let's create a new project called my_first_git_project.
    • Create a new directory: mkdir my_first_git_project cd my_first_git_project
  • Initialize a Git repository: This command creates a hidden .git directory inside your project, which Git uses to track changes. git init

You should see output like: Initialized empty Git repository in /path/to/my_first_git_project/.git/

  1. Basic Workflow: Add and Commit The core Git workflow involves making changes, "staging" them, and then "committing" them.
    • Working Directory: Your actual project files.
    • Staging Area (Index): A temporary area where you prepare changes before committing them.
    • Local Repository: Where your committed changes are stored. Scenario: Let's create a simple README.md file and commit it.
    • Create a file: echo "# My First Git Project" > README.md
  • Check the status: This command shows you the current state of your working directory. git status

You'll see README.md listed as "Untracked files."

  • Add to the staging area: This tells Git that you want to include README.md in the next commit. git add README.md

Alternatively, to add all changes in the current directory: git add .

  • Check status again: git status

Now README.md should be listed under "Changes to be committed."

  • Commit the changes: This takes the staged changes and records them in your local repository. The -m flag is for the commit message, which should describe the changes you made. git commit -m "Initial commit: Added README.md"

You'll see output confirming the commit.

  1. Viewing History You can see the history of your commits using git log. git log

This will show you a list of commits, including the author, date, and commit message. Press q to exit the log view.

  1. Modifying and Committing Again Let's make another change and commit it. Scenario: Add a new line to README.md.
    • Modify the file: echo "This is a mini-course about Git." >> README.md
  • Check status: git status

README.md will be listed as "modified."

  • Add and commit:
    git add README.md
    git commit -m "Added a description to README.md"

  • View history:
    git log

You'll now see two commits in your history.

  1. Understanding Branches Branches are a fundamental concept in Git. They allow you to diverge from the main line of development and work on new features or bug fixes without affecting the stable version of your project.
    • By default, you start on the main (or master) branch. Scenario: Create a new branch for a feature.
    • List branches: git branch

You'll see * main (or * master), indicating your current branch.

  • Create a new branch:
    git branch feature/new-page

  • Switch to the new branch:
    git checkout feature/new-page

You can also combine these two steps: git checkout -b feature/new-page

  • Verify current branch: git branch

You should now see * feature/new-page.

  1. Working on a Branch and Merging Now that you're on a new branch, any commits you make will only affect this branch. Scenario: Add a new file on the feature/new-page branch.
    • Create a new file: echo "This is a new page for our project." > new_page.txt
  • Add and commit:
    git add new_page.txt
    git commit -m "Added new_page.txt on feature branch"

  • Switch back to the main branch:
    git checkout main

Notice that new_page.txt is no longer in your working directory. This is because it was committed on feature/new-page.

  • Merge the feature branch into main: git merge feature/new-page

Git will try to integrate the changes from feature/new-page into main. In this simple case, it will likely be a "fast-forward" merge.

  • Check the status and files on main: git status ls

new_page.txt should now be present in your main branch.

  • Delete the merged branch (optional): Once a feature branch is merged and no longer needed, you can delete it. git branch -d feature/new-page
  1. Cloning an Existing Repository Often, you'll want to work on an existing project hosted on platforms like GitHub, GitLab, or Bitbucket. You can "clone" these repositories. Scenario: Let's imagine you want to clone a public repository (e.g., a simple Git tutorial project).
    • Go to a directory where you want to clone the repo: cd .. # Go up one directory from my_first_git_project
  • Clone the repository: (Replace the URL with an actual Git repository URL) git clone https://github.com/git/git.git # This will clone the Git's own source code! For a smaller example, you can use a simple hello-world repo.

This command will download the entire project and its history into a new directory named after the repository.
Note: For a practical example, you might try cloning a smaller, well-known public repository or one you create yourself on GitHub.

  1. Remote Repositories (Push and Pull) When working with others, you'll interact with "remote" repositories. These are typically hosted on services like GitHub.
    • git push: Uploads your local commits to the remote repository.
    • git pull: Downloads changes from the remote repository to your local repository. Scenario: After making changes locally, push them to a remote. (This requires you to have a remote repository set up, e.g., on GitHub).
    • Go back to my_first_git_project: cd my_first_git_project
  • Create a repository on GitHub (or similar service):
    • Go to GitHub.com, log in, and click "New repository."
    • Give it a name (e.g., my_first_git_project).
    • Do NOT initialize with a README.
    • After creation, GitHub will provide you with commands to link your local repository.
  • Link your local repo to the remote: git remote add origin https://github.com/your-username/my_first_git_project.git

(Replace your-username and my_first_git_project.git with your actual details.)

  • Push your local main branch to the remote: git push -u origin main

The -u flag sets the upstream branch, so you can simply use git push and git pull in the future.
Scenario: Pulling changes from a remote.
Imagine a collaborator pushes changes to the remote repository.

  • Simulate a change on GitHub directly (for demonstration): Go to your my_first_git_project on GitHub, click on README.md, and then the "Edit this file" pencil icon. Add a line like "Added on GitHub." Commit directly to main.
  • Pull the changes to your local machine: git pull origin main

You should see Git fetching and merging the changes from the remote.

  1. Undoing Changes (Basic) Sometimes you make mistakes. Git provides ways to undo them.
    • git reset: To unstage changes.
    • git checkout: To discard local changes. Scenario: You accidentally added a file you didn't want to commit.
    • Create a temporary file: echo "This file shouldn't be here." > temp_file.txt git add temp_file.txt git status

temp_file.txt is staged.

  • Unstage the file: git reset HEAD temp_file.txt git status

Now temp_file.txt is untracked again. You can then delete it if you wish.
Scenario: You made changes to a file but want to discard them and revert to the last committed version.

  • Modify README.md:
    echo "This is a temporary change I want to discard." >> README.md

  • Discard the changes:
    git checkout -- README.md

Now, open README.md and you'll see the temporary change is gone.
Caution: git checkout -- discards all local unstaged changes in that file. Use with care.
Conclusion and Next Steps
This mini-course covers the absolute essentials of Git. You've learned how to:

  • Initialize a repository
  • Add and commit changes
  • View commit history
  • Create and manage branches
  • Merge branches
  • Clone remote repositories
  • Push and pull changes to/from remotes
  • Perform basic undo operations Where to go from here?
  • Practice, practice, practice! The best way to learn Git is by using it regularly.
  • Explore more advanced topics:
    • Merging conflicts: What happens when two branches change the same line of code?
    • Rebasing: An alternative to merging for cleaner history.
    • git stash: Temporarily save your work without committing.
    • git reflog: A powerful command to recover lost commits.
    • Interactive rebase: Rewriting commit history.
  • Official Git Documentation: https://git-scm.com/doc
  • Atlassian Git Tutorial: https://www.atlassian.com/git/tutorials Good luck on your Git journey!