Git serves as a distributed version control system enabling efficient project tracking. This reference consolidates fundamental and advanced Git operations for repository management.
Initial Setup and Configuration
Initialize a local repository:
git init
Configure user identity:
git config user.name 'Your Name'
git config user.email 'email@example.com'
Use --global flag for system-wide settings.
Associate local repository with remote:
git remote add origin 'remote-repository-URL'
To disassociate:
git remote remove origin
Stage files for commit:
git add 'file-path'
Commit staged changes:
git commit -m "Commit message"
Synchronizing Changes
Retrieve remote updates without merging:
git fetch origin
Pull remote branch changes:
git pull origin main
Use --rebase to resolve conflicts:
git pull --rebase origin main
Push local changes to remote:
git push -u origin main:main
Clone specific brench:
git clone -b branch-name remote-repo-URL
Change Inspection
Compare working directory with staging area:
git diff
Compare staged changes with last commit:
git diff --cached
Compare working directory with last commit:
git diff HEAD
Commit History
Display commit log:
git log
Show limited commits:
git log -n 5
Filter merge commits:
git log --merges
View file-specific history:
git log -- filename
Display commit differences:
git log -p filename
Show recent diffs:
git log -p -2
View commit statistics:
git log --stat
File Operations
Rename tracked file:
git mv oldname newname
Move tracked file:
git mv filename target-directory/
Force move/overwrite:
git mv -f source existing-file
Branch Management
List branches:
git branch -a # All branches
git branch # Local branches
git branch -r # Remote branches
Create branch:
git branch new-feature
Rename branch:
git branch -m old-name new-name
Delete local branch:
git branch -d branch-name
Delete remote branch:
git push origin --delete branch-name
Set branch description:
git branch branch-name --edit-description
Establish upstream tracking:
git branch --set-upstream-to=origin/remote-branch
Switch branches:
git checkout branch-name # Existing branch
git checkout -b new-branch origin/remote-branch # Create and track
Return to previous brench:
git checkout -
Merging and Rebasing
Reset staged file to last commit (preserves working changes):
git reset filename
Reset staging and working area to last commit:
git reset --hard
Move branch pointer (preserves working directory):
git reset commit-hash
Hard reset to specific commit:
git reset --hard commit-hash
Reset HEAD while keeping changes:
git reset --keep commit-hash
Reverse changes via new commit:
git revert commit-hash
Revert last commit:
git revert HEAD
Stash uncommitted changes:
git stash
Restore stashed changes:
git stash pop
List stashes:
git stash list
Apply specific stash:
git stash apply stash@{n}
Discard stash:
git stash drop stash@{n}
Tag Operations
List tags:
git tag
Create lightweight tag:
git tag tag-name
Tag specific commit:
git tag tag-name commit-hash
Delete local tag:
git tag -d tag-name
Delete remote tag:
git push origin :refs/tags/tag-name
Inspect tag details:
git show tag-name
Push specific tag:
git push origin tag-name
Push all tags:
git push origin --tags
Create brench from tag:
git checkout -b branch-name tag-name
Commit History Manipulation
Rebase current branch onto target:
git rebase target-branch
Interactive rebase:
git rebase -i target-branch
Apply specific commits to current branch:
git cherry-pick commit-hash
Cherry-pick multiple commits:
git cherry-pick commit1 commit2