Introduction to Git

AI Bootcamp


Mork Mongkul

Table of Contents

  1. What is Git?
  2. Key Concepts
  3. Setting Up Git
  4. Cloning an Existing Repository
  5. Git Branch
  6. Reverting Changes
  7. Using .gitignore
  8. Additional Resources

What is Git?

What is Git?

  • Distributed version control system
  • Tracks changes in source code during software development
  • Created by Linus Torvalds in 2005
  • Used for coordinating work among programmers, but can track changes in any set of files

Key Concepts

Term Description
Repository (Repo) A database storing all files and history of changes
Commit A snapshot of the repository at a specific point in time
Branch A separate line of development
Merge Combining changes from different branches
Clone Copying a repository to your local machine
Push / Pull Syncing changes between local and remote repositories

Setting Up Git

Setting Up Git

Step 1 — Install Git:

Download from https://git-scm.com/

Step 2 — Configure your identity:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Cloning an Existing Repository

Creating a New Repository

Navigate to your project directory:

cd /path/to/your/project

Initialize a new Git repository:

git init

Cloning an Existing Repository

Clone a repository from a URL:

git clone https://github.com/user/repo.git

This creates a directory named repo containing all the files and history.

Checking the Status

Check the status of your files:

git status

This shows which files are staged, unstaged, or untracked.

Adding Files to Staging Area

Add a specific file / multiple files:

git add filename
git add filename_1 filename_2

Add all files:

git add .

Add all files of the same type:

git add *.extension

Renaming & Moving Files

Rename a file:

git mv old_filename new_filename

Move a file to a different directory:

git mv filename path/to/new_directory/

Deleting Files

Delete a file from the repository:

git rm filename

Forcefully remove a file:

git rm -f filename

Forcefully remove a directory and its contents:

git rm -rf directoryname

⚠️ Use with caution — permanently deletes from your working directory and staging area.

Committing Changes

Commit staged changes with a message:

git commit -m "Your commit message"

Amend the last commit message:

git commit --amend

Viewing Commit History

Full commit history:

git log

Shows a list of commits with their messages, authors, and dates.

Compact one-line view:

git log --oneline

Git Branch

Git Branch — Concept

A branch is a separate line of development.

  • Work on features or fixes in isolation
  • Merge back into main when ready
  • Multiple developers can work simultaneously

Branching

Create and switch to a new branch:

git checkout -b new-branch

Switch to an existing branch:

git checkout existing-branch

Modern alternative using git switch:

git switch branch-name        # switch to existing
git switch -c new-branch-name # create and switch

Merging Branches

Merge a branch into the current branch:

git merge branch-to-merge

Resolve any conflicts that arise after merging.

Deleting Branches

Delete a branch locally:

git branch -d branch-to-delete

Delete a branch on the remote:

git push origin --delete branch-to-delete

Syncing with Remote Repository

Push changes to remote:

git push origin branch-name

Pull changes from remote:

git pull origin branch-name

Swapping the Main Branch

# 1. Rename current main to old-main
git branch -m main old-main

# 2. Rename feature-branch to main
git branch -m feature-branch main

# 3. Update the remote
git push origin -u main

# 4. Delete old main from remote
git push origin --delete old-main

If the main branch is protected, update the default branch in repository settings first.

Resolving Conflicts

Resolving Conflicts

  • Occur when changes in different branches are incompatible
  • Git marks the conflicts in the affected files
  • Edit the files to resolve, then add and commit the changes
# After resolving conflicts:
git add resolved-file
git commit -m "Resolve merge conflict"

Conflict markers look like:

<<<<<<< HEAD
your changes
=======
their changes
>>>>>>> branch

Edit the file, remove markers, keep what you need.

Reverting Changes

Reverting Changes

Revert a specific file to a previous commit:

git checkout commit-id -- filename

Reset the entire repository to a previous commit:

git reset --hard commit-id

Safely undo a commit (creates new inverse commit):

git revert commit-id

git revert is preferred — it undoes changes without altering the commit history.

Stashing Changes

Stash current work-in-progress:

git stash
git stash push -m "Your stash message"

Apply / view / drop stashes:

git stash list            # view all stashes
git stash apply           # apply latest stash
git stash apply stash@{1} # apply specific stash
git stash pop stash@{1}   # apply and remove
git stash drop            # remove latest stash
git stash clear           # remove all stashes

Using .gitignore

Using .gitignore

The .gitignore file tells Git which files or patterns to ignore.

Common uses:

  • OS-generated files (.DS_Store, Thumbs.db)
  • Log and temporary files
  • Build artifacts and dependencies

Generate for Python:

curl -L -o .gitignore \
  https://www.toptal.com/developers/gitignore/api/python

Example .gitignore:

# Python
__pycache__/
*.pyc
*.pyo
.env
venv/

# Jupyter
.ipynb_checkpoints/

# OS
.DS_Store
Thumbs.db

# IDE
.vscode/
.idea/

Getting Help & Common Problems

Getting Help:

git --help
git <command> --help

# Examples
git clone --help
git status --help

Common Problems:

Problem Fix
Merge conflict Edit file → git addgit commit
Detached HEAD git checkout branch-name
Forgotten file git add filegit commit --amend

Tips & Team Workflow

Best Practices:

  • Write meaningful commit messages
  • Commit often with small, incremental changes
  • Use branches to isolate features and fixes
  • Pull regularly to stay in sync
  • Rebase before merging for a clean history
  • Communicate with teammates about changes

Typical Team Push Flow:

git status
git add filename
git commit -m "Your message"
git pull
# resolve conflicts if needed
git add filename
git commit -m "Resolve conflict"
git push

Full Team Workflow Example

# 1. Clone the repository
git clone https://github.com/user/repo.git

# 2. Create a feature branch
git switch -c feature-branch

# 3. Make changes and commit
git add .
git commit -m "Add new feature"

# 4. Push branch to remote
git push origin feature-branch

# 5. Open a Pull Request on GitHub → review → merge

# 6. Update your local main
git switch main
git pull origin main

Additional Resources

Resource Link
📘 Git Guide Book git-scm.com/book
🌿 Learning Branching learngitbranching.js.org
🔷 Atlassian Git Tutorials atlassian.com/git
🏫 W3Schools Git w3schools.com/git

Exercise 1 — Your First Repository

Tasks:

  1. Configure Git with your name and email
  2. Create a folder my-first-repo and initialize Git inside it
  3. Create a README.md describing the project
  4. Stage and commit with message "Initial commit"
  5. Create a repo on GitHub and push your local repo to it
git config --global user.name "Your Name"
git config --global user.email "you@email.com"

mkdir my-first-repo && cd my-first-repo
git init

echo "# My First Repo" > README.md
git add README.md
git commit -m "Initial commit"

git remote add origin https://github.com/yourname/my-first-repo.git
git push -u origin main

Exercise 2 — Branching & Merging

Tasks:

  1. Inside my-first-repo, create a new branch called feature/add-info
  2. Add a new file info.txt with any content and commit it
  3. Switch back to main
  4. Merge feature/add-info into main
  5. Delete the feature branch after merging
  6. Push the updated main to GitHub
git switch -c feature/add-info

echo "Some info about me" > info.txt
git add info.txt
git commit -m "Add info.txt"

git switch main
git merge feature/add-info

git branch -d feature/add-info

git push origin main

Questions?

Instinct Institute

Mork Mongkul