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
git push -u origin feature-xyz
git config --global push.autoSetupRemote always
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)
This command shows which remote repositories your local repository is connected to. For example, it might show the URL for GitHub or GitLab.git remote -v
-
Fetch changes from the remote repository without merging
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.git fetch
-
Pull changes (Fetch + Merge)
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.git pull origin main
-
Push changes to the remote repository
The git push command pushes your local commits to the remote repository, making your changes available to others or deploying them.git push origin main
6. Undoing Changes¶
Reset Staged Changes
git reset HEAD <file>
Undo Last Commit (Keep Changes)
git reset --soft HEAD~1
Undo Last Commit (Lose Changes)
git reset --hard HEAD~1
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:
- Git will show conflicting files. Open them and manually fix the conflicts.
- After resolving, stage the fixed files:
git add <file>
- 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:
- Create a Feature Branch:
git checkout -b feature-branch
- Push the Branch to Remote:
git push -u origin feature-branch
- Developers Work on Their Parts:
Each developer pulls the branch and commits their changes.
- 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:
- Fetch Latest Changes:
git fetch origin
- 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:
- Interactive Rebase:
git rebase -i HEAD~n
Replace n
with the number of commits to review.
- In the Editor:
Change pick
to squash
for commits you want to combine.
- 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:
- Checkout Target Branch:
git checkout main
- Cherry-Pick Commit:
git cherry-pick <commit-hash>
Finding Bug-Introducing Commits¶
Scenario: Identify the commit that introduced a bug.
Solution:
- Start Bisect:
git bisect start
git bisect bad # Current commit is bad
git bisect good <commit-hash> # Known good commit
- 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
- Reset Bisect:
git bisect reset
Working with Submodules¶
Scenario: Include another Git repository within your project.
Solution:
- Add Submodule:
git submodule add https://github.com/example/repo.git path/to/submodule
- Initialize and Update Submodules:
git submodule update --init --recursive
- 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