Git Commands with Explanations
Basic Configuration
git config --global user.name "Your Name"
-> Set your name for Git commits.
-> Set your email for Git commits.
git config --global core.editor "editor"
-> Set default editor (e.g., VSCode, Vim).
git config --list
-> View all current Git configurations.
Repository Initialization
git init
-> Initialize a new Git repository in the current directory.
git clone <repo_url>
-> Clone a repository from a remote URL to your local machine.
Staging and Committing
git status
-> Show the status of changes (staged, unstaged, untracked).
git add <file>
-> Stage a specific file for commit.
git add .
-> Stage all changes in the current directory.
git commit -m "message"
-> Commit staged changes with a message.
git commit -a -m "message"
-> Stage and commit all tracked files in one step.
Branching
Git Commands with Explanations
git branch
-> List all local branches.
git branch <branch-name>
-> Create a new branch.
git checkout <branch-name>
-> Switch to a specific branch.
git checkout -b <branch-name>
-> Create and switch to a new branch.
git merge <branch-name>
-> Merge the specified branch into the current branch.
git branch -d <branch-name>
-> Delete a local branch.
Remote Repositories
git remote -v
-> View remote repository URLs.
git remote add origin <url>
-> Add a remote repository.
git push -u origin <branch>
-> Push branch to remote and set upstream.
git push
-> Push committed changes to the remote.
git pull
-> Fetch and merge changes from the remote.
git fetch
-> Fetch changes from remote but don't merge.
Viewing History
git log
-> Show commit history.
Git Commands with Explanations
git log --oneline
-> Condensed, one-line log view.
git log --graph --all
-> Visual graph of all branches and history.
Undoing Changes
git restore <file>
-> Discard changes in the working directory.
git restore --staged <file>
-> Unstage a file (move from staged to unstaged).
git reset HEAD <file>
-> Unstage changes.
git reset --soft HEAD~1
-> Undo last commit but keep changes staged.
git reset --mixed HEAD~1
-> Undo last commit and unstage changes.
git reset --hard HEAD~1
-> Remove last commit and discard all changes.
Stashing
git stash
-> Save current changes temporarily.
git stash list
-> View list of stashed changes.
git stash apply
-> Reapply stashed changes.
git stash drop
-> Delete the latest stash.
Tagging
Git Commands with Explanations
git tag
-> List all tags.
git tag <tagname>
-> Create a lightweight tag.
git tag -a <tagname> -m "message"
-> Create an annotated tag.
git push origin <tagname>
-> Push a tag to remote.
git push --tags
-> Push all tags to remote.
Others & Utilities
git diff
-> Show differences between files.
git diff --staged
-> Show differences in staged files.
git show <commit>
-> Show details of a specific commit.
git blame <file>
-> Show who made each line change in a file.
git clean -f
-> Remove untracked files from working directory.