Git Basics
These are the core Git commands you will use when starting a project, checking changes, creating commits, and viewing repository history.
git init
git initInitializes a Git repository in the current project directory. Run it from the root directory of the project you want Git to track.
git status
git statusShows the current state of the working directory and staging area.
git add
git add .Stages the current project changes so they can be included in
the next commit. Files matched by .gitignore are
normally excluded from staging.
git commit
git commit -m “message”Creates a commit containing the staged changes.
git diff
git diffShows changes in files that have not yet been staged.
git diff –staged
git diff –stagedShows changes that are currently staged for the next commit.
git log
git log –onelineDisplays a compact list of previous commits.
Basic workflow
git status git add . git diff --staged git commit -m "Describe the change" git log --oneline
Use git status frequently. It tells you which
branch you are on and what Git currently sees as changed.
Branch Commands
These commands are used to create, inspect, switch, and remove Git branches.
git branch
git branchLists the local branches and marks the currently active branch with an asterisk.
git branch -M
git branch -M mainRenames the current branch to main.
git switch
git switch mainSwitches the working directory to the specified existing branch.
Create and switch to a branch
git switch -c feature/menuCreates a new branch and immediately switches to it.
Delete a local branch
git branch -d feature/menuDeletes a local branch when Git considers its commits already
merged into the relevant branch. Git normally prevents
-d when it detects unmerged commits.
Force-delete a local branch
git branch -D feature/menuForce-deletes a local branch, even if Git determines that it contains unmerged work.
git branch -D can remove a branch containing work
that has not been merged.
Use it only when you are certain that the branch and its unmerged changes are no longer needed.
Local branch workflow
git branch git switch main git switch -c feature/menu git branch git switch main
Understand the branch commands
| Command | Purpose |
|---|---|
git branch | List local branches. |
git switch main | Switch to an existing branch. |
git switch -c feature/menu | Create and switch to a new branch. |
git branch -d feature/menu | Delete a safely merged local branch. |
These commands operate on your local Git repository.
Commands such as git push are used when you need to
send branches and commits to GitHub.
What comes next?
Once you understand local branch commands, the next group of commands deals with connecting your local repository to a remote repository such as GitHub.
Remote Commands
Remote commands are used to connect your local Git repository with a remote repository such as GitHub and synchronize branches and commits.
git remote -v
git remote -vDisplays the remote repositories configured for the current Git repository.
git remote add
git remote add origin URLAdds a remote repository and gives it the name
origin.
git remote remove
git remote remove originRemoves the configured remote named origin from
the local repository.
git push
git pushSends local commits to the remote branch associated with the current branch. If no upstream branch is configured, specify the remote and branch explicitly.
Push a new branch
git push -u origin feature/menuPushes the local feature branch to GitHub and establishes its upstream relationship with the remote branch.
git pull
git pullFetches changes from the configured remote and then integrates them into the current local branch according to your Git pull configuration.
git fetch
git fetchDownloads information about changes from the remote repository without automatically merging those changes into the current branch.
git fetch downloads remote information without
changing your current branch.
git pull fetches remote changes and then integrates
them into the current branch according to your Git pull
configuration.
Connect a local repository to GitHub
git remote add origin https://github.com/YOUR-USERNAME/project.git git remote -v
Push the main branch
git push -u origin main Push a feature branch
git push -u origin feature/menu Update your local main branch
git switch main git pull
| Command | Purpose |
|---|---|
git remote -v | Show configured remote URLs. |
git remote add origin URL | Connect the local repository to a remote. |
git push | Send local commits to the remote. |
git pull | Fetch and integrate remote changes. |
git fetch | Download remote information without integrating it. |
Push: local repository → remote repository
Pull: remote repository → local repository
What comes next?
The next group covers commands for creating, inspecting, and managing commits.
Commit Commands
Commit commands are used to create, inspect, modify, or safely undo changes recorded in Git history.
git commit -m “message”
Creates a new commit from the changes currently staged in the staging area.
git add . git commit -m "Add coffee menu"
The commit message should briefly describe what changed.
git commit -m “message”Creates a new commit using the staged changes.
git commit –amend
Updates the most recent commit instead of creating another commit.
git add . git commit --amend -m "Add coffee menu and pricing"
Amend changes the previous commit. Avoid amending commits that have already been shared with other people unless you understand the consequences.
git show
Displays information about a commit, including the commit message and the changes introduced by that commit.
git show By default, Git shows the most recent commit. You can also specify a particular commit.
git show a1b2c3d git revert <commit>
Creates a new commit that reverses the changes introduced by an earlier commit.
git revert a1b2c3d This is useful when a change has already been committed or pushed and you want to undo its effect without deleting the existing commit from history.
git revert does not remove the original commit. It creates
another commit that reverses it.
Typical Commit Workflow
git status git diff git add . git diff --staged git commit -m "Describe the change" git log --oneline
This workflow lets you inspect your changes before staging them, review what is staged, create the commit, and then confirm the commit in the project history.
Quick Reference
| Command | Purpose |
|---|---|
git commit -m “message” | Create a new commit. |
git commit –amend | Modify the most recent commit. |
git show | Inspect a commit and its changes. |
git revert <commit> | Create a new commit that reverses an earlier commit. |
History Commands
History commands help you inspect the commits that have been created in a Git repository and understand how the project has changed over time.
git log
Displays the commit history of the current branch.
git log The detailed output includes information such as the commit identifier, author, date, and commit message.
git log –oneline
Displays a compact version of the commit history. This is often easier to read when you want to quickly find a commit.
git log –oneline Example:
a1b2c3d Add coffee menu 8f7e6d5 Update homepage 4c3b2a1 Initial project
The short value at the beginning of each line is the abbreviated commit identifier.
git log –oneline –graph
Displays a compact commit history together with a simple graph showing branch structure.
git log –oneline –graph This becomes especially useful when a project contains multiple branches.
git log –oneline –all
Shows commits from all local branches instead of only the currently checked-out branch.
git log –oneline –all git log –oneline –graph –all
Combines the previous options to provide a compact view of commits and branch history.
git log –oneline –graph –all This is particularly useful when learning how feature branches were created, developed, and merged.
Finding a Commit
Once you find the commit you are interested in, you can use its commit
identifier with commands such as
git show or git revert.
git log --oneline git show a1b2c3d git revert a1b2c3d
A Git commit has a unique identifier. Git often allows you to use a
shortened identifier, such as a1b2c3d, when it uniquely
identifies the commit.
Quick Reference
| Command | Purpose |
|---|---|
git log | Show detailed commit history. |
git log –oneline | Show a compact commit history. |
git log –oneline –graph | Show compact history with branch structure. |
git log –oneline –all | Show commits reachable from all local references. |
git log –oneline –graph –all | Show all local history with branch structure. |
git show <commit> | Inspect a specific commit. |
Recovery Commands
Recovery commands help you safely recover from common mistakes while working with Git.
git restore <file>
Discards local changes in a file that have not been staged.
git restore index.html Once you restore a file, its unstaged changes are discarded. Use
git diff first if you are not sure what will be removed.
git restore –staged <file>
Removes a file from the staging area without removing the changes from your working directory.
git restore –staged index.html This is useful when you accidentally stage a file and want to make changes before committing.
git reset
Git reset can move the current branch to another commit and can also change what is staged.
Because different reset modes have different effects, beginners should use it carefully.
git reset HEAD~1 This moves the current branch back by one commit while keeping the changes from that commit in the working directory.
Avoid using reset on commits that have already been pushed to a shared branch unless you understand how rewriting history affects the repository.
git revert <commit>
If a commit has already been shared or pushed, reverting it is often a safer way to undo its effect.
git revert a1b2c3d Git creates a new commit that reverses the changes made by the selected commit.
Common Recovery Situations
| Situation | Command |
|---|---|
| Discard unstaged changes in a file | git restore <file> |
| Remove a file from staging | git restore –staged <file> |
Move the current branch back one commit and keep its changes as unstaged work. | git reset HEAD~1 |
| Undo an existing commit with a new commit | git revert <commit> |
Safe Recovery Habit
git status git diff git log --oneline
Before using a recovery command, first inspect the current repository state, review your changes, and check the recent commit history. This helps you choose the appropriate recovery method instead of guessing.
Use git restore for working-file changes,
git restore –staged for staging mistakes, and
git revert when you need to safely undo an existing shared
commit.
Quick Reference
| Command | Purpose |
|---|---|
git restore <file> | Discard unstaged changes in a file. |
git restore –staged <file> | Remove a file from the staging area. |
git reset HEAD~1 | Move the current branch back one commit. |
git revert <commit> | Create a new commit that reverses an earlier commit. |
