Skip to content

Git Tutorial & Practical Use Cases

This tutorial provides a structured guide to Git, covering basic commands, advanced features, and practical scenarios commonly encountered in software development.


1. Basic Setup

Before using Git, configure your name and email:

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

Check your configuration:

git config --list

2. Initializing & Cloning Repositories

Initialize a New Repository

git init

Creates a new Git repository in the current directory.

Clone an Existing Repository

git clone <repository_url>

Copies a remote repository to your local machine.


3. Basic Workflow

Check Status

git status

Shows changed files and the current branch.

Add Changes to Staging

git add <file>  # Add a specific file
git add .       # Add all changes

Commit Changes

git commit -m "Commit message"

Saves changes locally.

View Commit History

git log --oneline --graph --decorate --all

4. Branching & Merging

Create a New Branch

git branch new-feature

Creates a new branch called new-feature.

Switch Branch

git switch new-feature

Create and Switch to a New Branch

git switch -c new-feature
Push the New Branch to the Remote Repository
git push -u origin feature-xyz
To avoid specifying -u on every push, configure Git to automatically set up tracking for new branches:

git config --global push.autoSetupRemote always
With this configuration, after creating and switching to a new branch, you can simply use git push, and Git will automatically set up the upstream tracking.

Merge Branches

git switch main
git merge new-feature

Merges new-feature into main.

Delete a Branch

git branch -d new-feature

5. Working with Remote Repositories

  • View connected remote repositories (e.g., GitHub, GitLab)

    git remote -v
    
    This command shows which remote repositories your local repository is connected to. For example, it might show the URL for GitHub or GitLab.

  • Fetch changes from the remote repository without merging

    git fetch
    
    The git fetch command retrieves the latest updates from the remote repository but does not merge them into your local branch. It's useful to inspect what has changed on the remote before deciding to merge.

  • Pull changes (Fetch + Merge)

    git pull origin main
    
    This command is a combination of git fetch and git merge. It fetches the latest updates from the main branch of the remote repository and automatically merges them into your current local branch.

  • Push changes to the remote repository

    git push origin main
    
    The git push command pushes your local commits to the remote repository, making your changes available to others or deploying them.


6. Undoing Changes

Reset Staged Changes

git reset HEAD <file>
This command removes a file from the staging area but leaves the changes in the working directory, allowing you to unstage a file without losing any modifications.

Undo Last Commit (Keep Changes)

git reset --soft HEAD~1
This command undoes the last commit but keeps the changes in your working directory and staging area. It's useful when you want to amend or rework the changes without losing them.

Undo Last Commit (Lose Changes)

git reset --hard HEAD~1
This command completely removes the last commit, including the changes in the working directory. Use it with caution as it will permanently delete the commit and any associated changes.


7. Stashing Work

Save Uncommitted Changes

git stash

List Stashes

git stash list

Restore Stashed Changes

git stash pop

8. Rebasing & Fixing Commits

Rebase a Branch

git switch feature-branch
git rebase main

Applies feature-branch commits on top of main.

Squash Commits (Interactive Rebase)

git rebase -i HEAD~3

Allows combining commits.

Amend Last Commit

git commit --amend -m "New commit message"

9. Handling Merge Conflicts

When a conflict occurs:

  1. Git will show conflicting files. Open them and manually fix the conflicts.
  2. After resolving, stage the fixed files:
git add <file>
  1. Continue the merge:
git merge --continue

or

git rebase --continue

10. Tags & Releases

Create a Tag

git tag -a v1.0 -m "Version 1.0"

Push Tags to Remote

git push origin --tags

Delete a Tag

git tag -d v1.0
git push origin --delete v1.0

11. Inspecting & Debugging

Show File Changes in a Commit

git show <commit-hash>

See Differences Between Branches

git diff main feature-branch

Blame (Find Who Changed a Line)

git blame <file>

12. Working with Multiple Repositories

Add a New Remote Repository

git remote add upstream <repo-url>

Fetch and Merge from Another Repository

git fetch upstream
git merge upstream/main

13. Practical Use Cases

Collaborative Feature Development

Scenario: Multiple developers are working on different aspects of a new feature.

Solution:

  1. Create a Feature Branch:
git checkout -b feature-branch
  1. Push the Branch to Remote:
git push -u origin feature-branch
  1. Developers Work on Their Parts:

Each developer pulls the branch and commits their changes.

  1. Merge Feature Branch into Main:
git checkout main
git merge feature-branch

Managing Diverged Branches

Scenario: Your local branch and the remote branch have diverged.

Solution:

  1. Fetch Latest Changes:
git fetch origin
  1. Rebase Your Changes:
git rebase origin/main

Resolve any conflicts and continue:

git add <resolved-files>
git rebase --continue

Cleaning Up Commit History

Scenario: You have multiple small commits that you want to combine into one.

Solution:

  1. Interactive Rebase:
git rebase -i HEAD~n

Replace n with the number of commits to review.

  1. In the Editor:

Change pick to squash for commits you want to combine.

  1. Finalize Commit Message:

Edit the commit message as desired, then save and exit.


Cherry-Picking Specific Commits

Scenario: Apply a specific commit from one branch to another.

Solution:

  1. Checkout Target Branch:
git checkout main
  1. Cherry-Pick Commit:
git cherry-pick <commit-hash>

Finding Bug-Introducing Commits

Scenario: Identify the commit that introduced a bug.

Solution:

  1. Start Bisect:
git bisect start
git bisect bad   # Current commit is bad
git bisect good <commit-hash>   # Known good commit
  1. Test Commits:

Git will checkout commits between good and bad. Test each and mark as good or bad:

git bisect good
# or
git bisect bad
  1. Reset Bisect:
git bisect reset

Working with Submodules

Scenario: Include another Git repository within your project.

Solution:

  1. Add Submodule:
git submodule add https://github.com/example/repo.git path/to/submodule
  1. Initialize and Update Submodules:
git submodule update --init --recursive
  1. Cloning Repository with Submodules:
git clone --recursive https://github.com/your/repo.git

Tips & Tricks

Recovering from Mistakes

Scenario: You made a mistake and need to revert to a previous state.

Solution:

git reflog
# Find the commit before the mistake
git reset --hard HEAD@{index}

Amending the Last Commit

Scenario: You want to modify the last commit.

Solution:

# Make your changes
git add .
git commit --amend

Note: Avoid amending commits that have been pushed to shared branches.


End of Tutorial

Author : Younes IKLI

Last update : 2025-05-04T17:55:36Z