๐ Useful Git Commands¶
A categorized reference of common and essential Git commands for daily use, including explanations of when and why to use them.
๐ง Initial Configuration¶
git config --global user.name "Your Name" # Set your name for all repos
git config --global user.email "your.email@example.com" # Set your email for all commits
git config --global core.editor nano # Set your preferred text editor
git config --list # Check your current Git configuration
๐ฆ SSH Configuration¶
Use these commands to set up your identity and preferences when working with Git, found it in:
๐ Repository Initialization & Cloning¶
git init # Start a new Git repository in the current folder
git clone <REPO_SSH_URL> # Create a local copy of a remote repository
init when starting a new project from scratch. Use clone when collaborating on an existing project.
๐ Status and Diffs¶
git status # See the current state of the working directory and staging area
git diff # View changes in files not yet staged
git diff --staged # View changes staged for the next commit
โ Staging and Committing Changes¶
git add file.txt # Stage a file for the next commit
git add . # Stage all modified and new files
git commit -m "Message" # Save changes with a message describing the update
git commit -am "Message" # Add and commit changes in tracked files in one step
โช Undoing Changes¶
git restore file.txt # Revert changes in working directory
git restore --staged file.txt # Unstage a file without changing its contents
git reset --hard HEAD # Discard all changes and return to last commit
๐ Viewing History¶
git log # View full commit history
git log --oneline # View condensed history
git log --graph --oneline --all # Visualize branches and commits as a graph
๐ฟ Branching¶
git branch # List all local branches
git branch new-branch # Create a new branch
git checkout new-branch # Switch to a different branch
git checkout -b new-branch # Create and switch to a new branch
git switch new-branch # (Modern) Switch branches
git switch -c new-branch # (Modern) Create and switch
git merge feature-branch # Merge a branch into the current one
git branch -d old-branch # Delete a branch that is no longer needed
๐ Working with Remotes¶
git remote -v # Show the remotes for this repository
git remote add origin <REPO_URL> # Add a remote called origin
git fetch # Fetch changes from the remote without merging
git pull # Fetch and merge changes from the remote
git push # Upload your local changes to the remote
git push -u origin main # Push current branch and set upstream
๐งช Stashing (Temporary Saves)¶
git stash # Save your changes temporarily
git stash list # View saved stashes
git stash apply # Reapply the latest stash
git stash drop # Remove a stash
๐ Tags¶
git tag # List tags
git tag v1.0 # Create a tag for a specific commit
git push origin v1.0 # Share the tag with others
๐งผ Cleanup¶
git clean -n # Preview which untracked files would be removed
git clean -f # Actually remove untracked files
๐ฆ Checkout (Detailed)¶
git checkout branch-name # Switch to an existing branch
git checkout -b new-branch # Create and switch to a new branch
git checkout commit_hash # View the code at a specific commit (detached HEAD)
git checkout file.txt # Restore file to last committed version
checkout to navigate between versions of your project or recover files.