-
Notifications
You must be signed in to change notification settings - Fork 0
Git: In a nutshell
Git is a distributed version control system, originally developed by Linus Torvalds (in 2005) to track Linux kernel development.
To install Git, use the following command based on your OS:
- Windows: Download from git-scm.com
-
Mac: Install via Homebrew:
brew install git
Creates a new Git repository in your project folder.
git init
Downloads a copy of a repository from GitHub.
git clone <repository-url>
Example:
git clone https://github.com/user/repository.git
Displays modified files and staged changes.
git status
Display commit history as a visual tree.
git log --graph --oneline
branch, switch and checkout are all useful for branching. First one creates a new branch, second switches to another and third both creates and checks out a new branch.
git branch iss53
git switch iss53
git checkout iss53
- Git-reference categorizes common commands and gives clear descriptions of each command.
- Oh My Git! is an open source game about learning Git. With its visuals and carefully designed levels, it's a fun way to master Git.
- Git Cheat Sheet
git cherry-pick <commit-hash>
applies a specific commit from another branch onto the current branch. It’s useful when you want to apply a fix or feature without merging the entire branch.
git cherry-pick a1b2c3d
This applies the commit a1b2c3d
to the current branch.
Combines changes from another branch into the current branch, creating a new merge commit.
✅ Keeps commit history
✅ Preserves original branch structure
❌ Can cause merge commits (messy history)
git merge feature-branch
Moves the current branch on top of another branch, rewriting commit history.
✅ Creates a linear history
✅ No merge commits
❌ Can cause conflicts if used incorrectly
git rebase main
Squashing combines multiple commits into one, keeping the history cleaner.
- Run interactive rebase:
git rebase -i HEAD~3 # Squash last 3 commits
- Change
pick
tosquash (s)
for the commits you want to combine. - Save and close the editor.
- Edit commit message and finalize.
After squashing, push with force:
git push origin branch-name --force
git reset
is used to undo local changes. It can modify the staging area (index) and the working directory depending on the options used.
- Soft Reset
--soft
- Moves HEAD to the specified commit.
- Keeps changes in the staging area (index)
- Useful for undoing commits while retaining changes for further edits.
git reset --soft <commit-hash>
- Mixed Reset (default)
- Moves the HEAD pointer to the specified commit.
- Resets the staging area to match the commit but keeps changes in the working directory.
git reset <commit-hash> # Alternatively, git reset --mixed <commit-hash>
- Hard Reset
--hard
- Moves the HEAD pointer to the specified commit.
- Resets both the staging area and working directory to match the commit.
⚠️ All changes will be lost, so use with caution.git reset --hard HEAD~1 # Resets to the previous commit and discards changes
git reset --hard a1b2c3d # Resets to the commit `a1b2c3d` and discards changes
Reverting a commit creates a new commit that undoes a previous commit. Unlike reset, it doesn’t modify commit history.
git revert a1b2c3d # Creates a new commit that negates the changes of the commit `a1b2c3d`.
git revert
is safer because it does not rewrite history, making it useful for tracking our shared repository.
Allowing you to “stash” (temporarily save) changes that are not ready to be committed. This is extremely useful when you need to switch branches quickly, or pull in updates without losing your local modifications.
git stash # Save uncommitted changes to a new stash
git stash list # List all stashed changes
git stash apply stash@{2} # Apply a specific stash from the list.
#If you don't write stash@{number}, you will apply the most recent stash
git stash branch <new branchname> #Creates a branch from a stash
It takes all the changes in your working directory which can be checked by running 'git status' command and staging area (index) and saves them in a special stash “stack” inside .git/refs/stash. After stashing, your working directory and the staging area are reverted back to the state of the last commit, so it looks like you haven’t made any local changes.
Resource Link: Stashing and Cleaning
you may not want to stash some work or files in your working directory, but simply get rid of them; that’s what the 'git clean' command is for.
The command designed to remove files from your working directory that are not tracked. This means that if you need them again, There is often no retrieving the content of those files.
Resource Link for stashing and cleaning: Stashing and Cleaning
Both git fetch
and git pull
are used to update your local repository with changes from a remote repository, but they behave differently.
- Downloads new changes (commits, branches, tags) from the remote repository without modifying your working directory.
- You can review the updates before merging them.
- It is useful when you want to check for updates without automatically applying them.
git fetch origin
- Downloads changes and automatically merges them into your current branch.
- Equivalent to
git fetch
+git merge
. - Can lead to unexpected merge conflicts if changes are not reviewed beforehand.
git pull origin main
- When working in a team and you want to see updates before merging.
- When checking for remote changes without modifying your local branch.
- To avoid potential merge conflicts when pulling unknown changes.
- Shows changes between commits, branches, or working directory files.
- Useful for reviewing modifications before committing.
git diff # Show unstaged changes
git diff --staged # Show changes that are staged for commit
git diff main feature-branch # Compare two branches
git diff HEAD~1 HEAD # Compare last commit with the current state
- Displays detailed information about a specific commit, including its changes and metadata.
- Useful when investigating previous commits.
git show <commit-hash>
- Use
git diff
when you want to compare changes before committing. - Use
git show
when you want to examine a specific commit’s details.
Git provides .gitignore
and .gitkeep
to manage ignored files and track empty directories.
- Specifies files and folders that should not be tracked by Git.
- Useful for ignoring compiled files, environment variables, or temporary files.
# Ignore node_modules and logs
node_modules/
logs/
# Ignore all .env files
*.env
# Ignore system files
.DS_Store
After defining .gitignore
, you need to remove previously tracked files manually:
git rm --cached <file>
- Git does not track empty folders by default.
- If you want to keep an empty directory in your repo, create an empty
.gitkeep
file inside it.
touch path/to/directory/.gitkeep
git add path/to/directory/.gitkeep
You want to keep an empty logs/
directory in the repo, for example your program will create log files and print log messages automatically when it runs.
Create logs/.gitkeep
to ensure it is tracked.
Git hooks are used to automate scripts to run before or after specific Git commands. They are stored in the .git/hooks
directory.
Git hooks are categorized into client-side and server-side hooks. Below are some commonly used hooks:
- pre-commit: Runs before a commit is created. Useful for linting or running tests.
- commit-msg: Checks the commit message format.
- pre-push: Runs before pushing changes to a remote repository. Can be used to ensure all tests pass before pushing.
- post-commit: Runs after a commit is completed. Useful for notifications or logging.
- pre-receive: Runs on the remote repository before accepting pushed commits. Can enforce policies like branch protection.
-
update: Similar to
pre-receive
, but runs once per branch. - post-receive: Runs after changes are accepted. Useful for deployment or notifications.
Git templates allow you to enforce structured commit messages by predefining message formats.
You can define a standard commit message template that gets loaded into your commit editor. This helps using a clean and homogenous structure within the project.
git config --global commit.template ~/.gitmessage.txt
# Commit message template
# ----------------------
# Type (feat, fix, refactor, docs, test): Short description (max 50 chars)
#
# Body (Optional) - Explain what this commit changes
# - Use bullet points for clarity
# - Keep it concise
#
# Related Issue: #ISSUE_NUMBER (Optional)
You can use Git hooks (e.g., prepare-commit-msg
) to ensure all commits follow the template.
- Helps maintain consistent commit messages.
- Ensures better readability and organization in team projects.


-
Use Cases:
- Template
- Scenario 1: Community Sharing
- Scenario 2: For a Dietitian
- Scenario 3: Maintaining a Personal Inventory (as a Producer)
- Scenario 4: Proposing a Product and Adding a Recipe to Weekly Meal Plan
- Scenario 5: Creating a Meal Plan
- Scenario 6: Resetting Password
- Scenario 7: Moderator Actions
- Scenario 8: Searching for and Proposing a New Food Item
- Scenario 9: Budget-Conscious Grocery Shopping
- Scenario 10: Creating a New Store
- Scenario 11: User Profile Management
- Git: In a Nutshell
- Example GitHub Repositories
- System Modeling & UML
- Frontend Tutorial
- Frontend Roadmap
- Frontend Styling Guide
- Docker Summary
- Writing Endpoints
- Yusuf AKIN
- Arda SAYGAN
- Fatih Furkan Bilsel
- Berk GOKTAS
- Berkay BILEN
- Yusuf Anıl YAZICI
- Taha Topaloglu
- Nuri Basar
- Onur Kucuk
- Hasancan Keles
- Mete Damar