Git Commands
Git Commands
1. Configuration
git config --global user.name "Your Name"
Set your name in Git configuration.
git config --global user.email "your.email@example.com"
Set your email in Git configuration.
git config --list
View all configuration settings.
git config --global core.editor "code --wait"
Set a default text editor (e.g., VS Code).
2. Repository Setup
git init
Initialize a new Git repository.
git clone <repository_url>
Clone an existing repository.
3. Basic Commands
git add <file>
Stage a specific file for commit.
git add .
Stage all changes in the current directory.
git commit -m "Commit message"
Commit staged changes with a message.
git commit -a -m "Commit message"
Stage and commit all tracked files in one step.
4. Branching
git branch
List all branches in the repository.
git branch <branch_name>
Create a new branch.
git checkout <branch_name>
Switch to a specific branch.
git switch <branch_name>
Alternate way to switch branches.
git branch -d <branch_name>
Delete a branch.
git merge <branch_name>
Merge a branch into the current branch.
5. Viewing Changes
git status
View the status of your working directory.
git diff
Show differences in unstaged changes.
git diff --staged
Show differences in staged changes.
git log
View the commit history.
git log --oneline
View a concise commit history.
6. Remote Repositories
git remote -v
View remote repositories.
git remote add <name> <url>
Add a new remote repository.
git push <remote_name> <branch_name>
Push changes to a remote repository.
git pull <remote_name> <branch_name>
Pull changes from a remote repository.
git fetch <remote_name>
Fetch changes from a remote repository.
7. Stashing
git stash
Save changes temporarily without committing.
git stash list
View all stashed changes.
git stash apply
Reapply stashed changes.
git stash drop
Remove a specific stash.
8. Tagging
git tag
List all tags in the repository.
git tag <tag_name>
Create a new tag.
git tag -a <tag_name> -m "Message"
Create an annotated tag.
git push origin <tag_name>
Push a specific tag to a remote repository.
9. Undo Changes
git checkout -- <file>
Discard changes in a file.
git reset <file>
Unstage a file without discarding changes.
git reset --soft HEAD~1
Undo the last commit but keep changes staged.
git reset --hard HEAD~1
Undo the last commit and discard all changes.
11. Cleaning Up
git clean -f
Remove untracked files.
git clean -fd
Remove untracked files and directories.
12. Help
git help
Get help for Git commands.
git <command> --help
Get help for a specific command.