Git Commands
- git
init
- Initialize
a new Git repository.
- Example:
Run this in an empty project folder to start version control.
- git
clone <repo_url>
- Clone
an existing repository from a remote source.
- Example:
git clone https://github.com/user/repo.git to copy a project from GitHub.
- git
status
- Show
the status of changes in the working directory and staging area.
- Example:
Run this to check which files have been modified or staged.
- git
add
- Add
a specific file to the staging area.
- Example:
git add index.html to stage index.html for commit.
- git
add .
- Add
all changes in the working directory to the staging area.
- Example:
Run this when you want to stage all modified and new files.
- git
commit -m "message"
- Commit
the staged changes with a descriptive message.
- Example:
git commit -m "Fixed login issue" to save changes with a
meaningful note.
- git
commit --amend -m "new message"
- Modify
the last commit message.
- Example:
Run this if you made a typo in the previous commit message.
- git
log
- Show
commit history.
- Example:
Use this to review past commits and their messages.
- git
log --oneline
- Show
commit history in a compact format.
- Example:
Use this for a quick summary of commit history.
- git
diff
- Show
differences between working directory and staging area.
- Example:
Run this to see what changes have been made before staging.
- git
diff --staged
- Show
differences between the staged files and the last commit.
- Example:
Use this before committing to review staged changes.
- git
branch
- List
all branches in the repository.
- Example:
Run this to check which branches exist in your project.
- git
branch <branch_name>
- Create
a new branch.
- Example:
git branch feature-xyz to create a branch for a new feature.
- git
checkout <branch_name>
- Switch
to a different branch.
- Example:
git checkout develop to move to the develop branch.
- git
checkout -b <branch_name>
- Create
and switch to a new branch.
- Example:
git checkout -b bugfix-101 to create and switch to a new bugfix branch.
- git
merge <branch_name>
- Merge
a branch into the current branch.
- Example:
git merge feature-xyz to merge changes from feature-xyz into the current
branch.
- git
rebase <branch_name>
- Reapply
commits on top of another base branch.
- Example:
git rebase main to move feature branch commits on top of main.
- git
stash
- Temporarily
save changes that are not committed.
- Example:
Run this before switching branches when you have uncommitted changes.
- git
stash pop
- Reapply
the latest stashed changes and remove them from the stash list.
- Example:
Use this to bring back previously stashed changes.
- git
stash list
- Show
a list of stashed changes.
- Example:
Run this to check saved stashes.
- git
stash drop
- Delete
the latest stashed change.
- Example:
Run this to remove an unwanted stash.
- git
reset
- Unstage
a specific file from the staging area.
- Example:
git reset index.html to remove index.html from staging but keep changes.
- git
reset --hard
- Reset
the working directory and staging area to the last commit.
- Example:
Run this if you want to discard all uncommitted changes.
- git
reset --soft HEAD~1
- Undo
the last commit but keep changes in the staging area.
- Example:
Use this if you want to redo the last commit with a different message.
- git
revert <commit_hash>
- Create
a new commit that undoes the changes from a specific commit.
- Example:
git revert abc123 to undo commit abc123 safely.
- git
pull
- Fetch
and merge changes from a remote repository.
- Example:
git pull origin main to update the local branch with the latest remote
changes.
- git
fetch
- Download
changes from a remote repository without merging.
- Example:
Run this to check for remote updates before merging.
- git
push
- Push
local changes to a remote repository.
- Example:
git push origin main to upload local commits to the main branch.
- git
push -u origin <branch_name>
- Push
a branch to the remote repository and track it.
- Example:
git push -u origin feature-xyz to push a new branch and set tracking.
- git
remote -v
- List
all remote repositories linked to the local repository.
- Example:
Use this to check remote connections.
- git
remote add
- Add
a new remote repository.
- Example:
git remote add origin https://github.com/user/repo.git to link a GitHub
repository.
- git
remote remove
- Remove
a remote repository.
- Example:
git remote remove origin to delete a remote link.
- git
tag <tag_name>
- Create
a new tag.
- Example:
git tag v1.0 to mark a version release.
- git
tag -a <tag_name> -m "message"
- Create
an annotated tag with a message.
- Example:
git tag -a v1.0 -m "Version 1.0 release" to add a tag with a
description.
- git
show <tag_name>
- Show
details of a tag.
- Example:
git show v1.0 to see information about a tag.
- git
cherry-pick <commit_hash>
- Apply
a specific commit from another branch.
- Example:
git cherry-pick abc123 to apply a single commit from another branch.