Basic Command¶
Overview¶
The following is a list of basic Git commands with explanations for each. These commands are often used when working with Git and are a good starting point for understanding the basics of Git.
Commands¶
Git Command Introduction¶
# Git Configuration
git config --global user.name "Your Name" # Set your name for commits and tags
git config --global user.email "you@example.com" # Set your email for commits and tags
git config --global color.ui auto # Enable colored Git output
# Starting a Project
git init [project_name] # Create a new local repository
git clone [project_url] # Download a project and its history from a remote repository
Day-to-Day Work¶
git status # Show the status of your working directory
git add [file] # Add file(s) to the staging area
git diff [file] # Show unstaged changes
git diff --staged [file] # Show staged changes
git checkout -- [file] # Discard local changes (irreversible)
git reset [file] # Unstage a file without discarding changes
git commit # Commit staged changes with a message
Branching Model¶
git branch [-a] # List local branches; use -a to show remote branches too
git branch [branch_name] # Create a new branch
git checkout [-b] [branch] # Switch to (or create with -b) a branch
git merge [branch_name] # Merge specified branch into current branch
git branch -d [branch_name] # Delete a branch (use -D to force delete)
File Management & Stashing¶
git rm [file] # Remove file from working directory and staging area
git stash # Temporarily store changes for later use
git stash pop # Reapply stashed changes and clear the stash
git stash drop # Delete a specific stash
Reviewing Work¶
git log [-n count] # Show commit history; limit with -n
git log --oneline --graph --decorate # Show condensed graph view of commits
git log ref.. # Show commits not merged into ref
git log ..ref # Show commits in ref not in current branch
git reflog # Show local operation history (checkouts, commits, etc.)
Tagging Commits¶
git tag # List all tags
git tag [name] [commit_sha] # Create a lightweight tag
git tag -a [name] [commit_sha] # Create an annotated tag
git tag -d [name] # Delete a local tag
Reverting & Synchronizing¶
git reset [--hard] [target_ref] # Move branch to target; discard changes with --hard
git revert [commit_sha] # Create a commit that reverses a specific commit
git fetch [remote] # Download objects but don’t merge
git fetch --prune [remote] # Remove stale remote-tracking references
git pull [remote] # Fetch and merge changes
git push [--tags] [remote] # Push commits (and optionally tags)
git push -u [remote] [branch] # Push branch and set upstream
Ignoring Files¶
Place .gitignore in your project root to exclude specific files or directories from version control.
Reference¶
Some process usage
--Initation for new project