Git Commands
Top Git Commands with Examples
-
git init
-
Initializes a new Git repository.
-
Example: git init
-
-
git clone [repository-url]
-
Clones a repository from a URL.
-
Example: git clone
-
https://github.com/user/repo.git
-
git status
-
Shows the working directory status.
-
Example: git status
-
-
git add [file]
-
Stages a file for commit.
-
Example: git add file.txt
-
-
git commit -m "[message]"
-
Commits staged changes with a message.
-
Example: git commit -m "Initial commit"
-
-
git log
-
Shows the commit history.
-
Example: git log
-
-
git diff
-
Shows differences between working directory and index.
-
Example: git diff
-
-
git branch
-
Lists all branches.
-
Example: git branch
-
-
git branch [branch-name]
-
Creates a new branch.
-
Example: git branch feature-branch
-
-
git checkout [branch-name]
-
Switches to a different branch.
-
Example: git checkout feature-branch
-
-
git merge [branch-name]
-
Merges a branch into the current branch.
-
Example: git merge feature-branch
-
-
git pull
-
Fetches and merges changes from the remote repository.
-
Example: git pull origin main
- git push
-
Pushes changes to the remote repository.
-
Example: git push origin main
- git remote -v
-
Lists remote repositories.
-
Example: git remote -v
- git fetch
-
Fetches changes from the remote repository.
-
Example: git fetch origin
- git reset [file]
-
Unstages a file.
-
Example: git reset file.txt
- git rm [file]
-
Removes a file from the working directory and staging area.
-
Example: git rm file.txt
- git stash
-
Stashes changes in a dirty working directory.
-
Example: git stash
- git stash apply
-
Applies stashed changes.
-
Example: git stash apply
- git tag [tag-name]
-
Creates a new tag.
-
Example: git tag v1.0
- git tag -d [tag-name]
-
Deletes a tag.
-
Example: git tag -d v1.0
- git rebase [branch-name]
-
Re-applies commits on top of another base tip.
-
Example: git rebase feature-branch
- git cherry-pick [commit-hash]
-
Applies the changes from a specific commit.
-
Example: git cherry-pick a1b2c3d
- git log --oneline
-
Shows the commit history in a simplified format.
-
Example: git log --oneline
- git log --graph
-
Displays a graphical representation of the commit history.
-
Example: git log --graph
- git log --pretty=format:"%h - %an, %ar : %s"
-
Customizes the format of log output.
-
Example: git log --pretty=format:"%h - %an,
%ar : %s"
-
git diff --staged
-
Shows differences between staged changes and the last commit.
-
Example: git diff --staged
-
-
git diff [branch1] [branch2]
-
Shows differences between two branches.
-
Example: git diff main feature-branch
-
-
git config --global user.name "[name]"
-
Sets the Git username.
-
Example: git config --global user.name "John Doe"
-
-
git config --global user.email "[email]"
-
Sets the Git email address.
-
Example: git config --global user.email "john.doe@example.com"
-