Git Command Crash Course
2 min readNov 15, 2024
Git command crash course for easy reference and practice:
Setup Commands
git config --global user.name "Your Name"
Set your name for commits.git config --global user.email "your.email@example.com"
Set your email for commits.git config --global core.editor "editor"
Set the default editor for Git (e.g.,vim
,nano
).
Repository Basics
git init
Initialize a new Git repository in the current directory.git clone [URL]
Clone a remote repository to your local machine.
File Management
git add [file]
Stage a specific file for commit.git add .
Stage all changes (new, modified, deleted files).git rm [file]
Remove a file from both the working directory and staging area.git mv [old-name] [new-name]
Rename or move a file.
Committing Changes
git commit -m "Your message"
Commit staged changes with a descriptive message.git commit --amend -m "Updated message"
Amend the last commit with a new message or additional changes.
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 checkout -b [branch-name]
Create and switch to a new branch.git merge [branch-name]
Merge a branch into the current branch.
Pull, Push, and Fetch
git pull
Fetch and merge changes from the remote repository.git push
Push your changes to the remote repository.git push -u origin [branch-name]
Push a branch and set it to track the remote branch.git fetch
Download changes from the remote repository without merging.
Viewing History
git log
View commit history.git log --oneline
View a compact, single-line commit history.git log --graph
View a graphical representation of the commit history.git diff
Show unstaged changes in your working directory.git diff [commit] [commit]
Compare two commits.
Stashing
git stash
Save changes without committing them (clean working directory).git stash pop
Reapply stashed changes and remove from stash list.git stash list
View all stashed changes.
Undo Changes
git checkout -- [file]
Discard changes in a working directory file.git reset HEAD [file]
Unstage a staged file.git reset --hard
Reset the working directory and staging area to the last commit.git revert [commit]
Create a new commit to reverse a specific commit.
Working with Remote
git remote -v
View all remote repositories.git remote add [name] [URL]
Add a new remote repository.git remote remove [name]
Remove a remote repository.
Other Useful Commands
git status
Show the status of changes in the repository.git blame [file]
View who last modified each line in a file.git tag [tag-name]
Create a tag for a specific commit.git show [commit/tag]
View details about a specific commit or tag.
These commands should help you quickly start practicing Git like DOS commands!
These commands should help you quickly start practicing Git like DOS commands!