git-cheatsheet
git-cheatsheet
Git is a version control system. It's like a history book for your project. It keeps
track of every change you make to your files over time. This lets you:
Go back to older versions: If you mess something up, you can easily undo
your changes and go back to a working version.
Collaborate with others: Multiple people can work on the same project
without overwriting each other's work.
Track changes: See exactly what was changed, when, and by whom.
Experiment safely: Create branches to try out new features without breaking
the main project.
Repository (Repo): This is like the main folder for your project that Git is
tracking. It contains all your project files and the history of changes. Think of it
as your project's "history book."
Working Directory: This is your local folder where you actually edit your
project files. It's where you make changes.
Staging Area (Index): This is a temporary area where you prepare changes
before committing them. You "stage" the files you want to include in your next
snapshot. Think of it like a "waiting room" for your changes.
Git 1
the main codebase. Think of it as creating a "new chapter" in your project's
book. The main branch is often called main or master .
Initializes a new
Git repository in
git init git init
the current
directory.
Downloads a
git clone repository from a git clone
<repository_url> remote URL to <https://github.com/user/repo.git >
your computer.
Stages changes
Working with git add <filename> or in a specific file
git add index.html or git add .
Changes git add . or all files for the
next commit.
git commit -m "Your Commits staged git commit -m "Fixing a bug in login
commit message" form"
changes with a
Git 2
descriptive
message.
Shows the
difference
between your
git diff git diff
working
directory and the
staging area.
Shows the
difference
git diff --staged between the git diff --staged
staging area and
the last commit.
Unstages a file,
git restore --staged
removing it from git restore --staged index.html
<filename>
the staging area.
Discards
changes in your
git restore <filename> working git restore index.html
directory for a
specific file.
Shows a
condensed one-
git log --oneline git log --oneline
line commit
history.
Shows details of
git show
a specific git show a1b2c3d4
<commit_hash>
commit.
Git 3
git checkout Switches to a
git checkout feature-login
<branch_name> different branch.
Creates a new
git checkout -b branch and
git checkout -b develop
<new_branch_name> switches to it
immediately.
Merges changes
from the
git merge
specified branch git merge develop
<branch_name>
into the current
branch.
Deletes a branch
git branch -d
(after it's git branch -d feature-login
<branch_name>
merged).
Adds a remote
Remote git remote add origin git remote add origin
repository
Repositories <repository_url> <https://github.com/user/repo.git >
named "origin".
Shows
configured
git remote -v git remote -v
remote
repositories.
Uploads local
commits to the
git push origin remote
git push origin main
<branch_name> repository (to
"origin" remote,
branch).
Downloads
changes from
git pull origin the remote
git pull origin main
<branch_name> repository to
your local
branch.
Downloads
objects and refs
git fetch origin git fetch origin
from another
repository
Git 4
Explanation of some key commands:
git init : Use this command in your project folder to start using Git. It creates a
hidden .git folder that stores all the version history.
(like GitHub).
git add : Tells Git to track changes you've made to files and prepare them for
the next commit. You need to "add" files to the staging area before you can
commit them.
git status : A very useful command to see what's going on with your repository.
It tells you about changes you've made, files you've staged, and your current
branch.
git branch and git checkout : Essential for working with branches. Branching allows
you to work on features or fixes in isolation.
git pull : Gets the latest changes from the remote repository and merges them
into your local branch. Use this to stay up-to-date with the work of others.
Important Notes:
Write Good Commit Messages: Your commit messages should be clear and
concise, explaining why you made the changes, not just what you changed.
This helps you and others understand the history of the project later.
Use Branches for Features and Fixes: Don't work directly on the main branch
for new features or bug fixes. Create branches to keep your work organized
and prevent accidentally breaking the main codebase.
Git 5
1. What is Git and why is it important in software development? (Hint: Version
control, collaboration, tracking changes)
2. Explain the difference between git add , git commit , and git push . (Hint: Staging
area, local commit, remote repository)
3. What is a branch in Git and why are branches useful? (Hint: Parallel
development, feature isolation, bug fixes)
4. How do you merge changes from one branch to another in Git? (Hint: git
merge )
7. What is a merge conflict and how do you resolve it? (Hint: Conflicting
changes, manual resolution)
This is a starting point for Git. There's much more to learn, but these basics and
commands will get you going! Practice using these commands in a test repository
to get comfortable with Git.
Git 6