Getting & Creating Projects
-
Clone an existing repository
git clone <repository-url>
-
Create a new local repository
git init
Basic Snapshotting
-
Add file contents to the index (staging area)
git add <filename> git add .
-
Record changes to the repository
git commit -m "Commit message"
-
Ammend changes
git commit --amend
-
Show the current status of the repository
git status
-
Show changes between commits, commit and working tree, etc
git diff
Branching & Merging
-
List all branches
git branch
-
Create a new branch
git branch <branch-name>
-
Switch to a branch
git checkout <branch-name>
-
Create and switch to a new branch
git checkout -b <branch-name>
-
Merge a branch into the current branch
git merge <branch-name>
-
Delete a branch
git branch -d <branch-name>
Sharing & Updating Projects
-
Push local changes to the remote repository
git push origin <branch-name>
-
Push branch changes to another branch
git push origin <branch-name>:<other-branch-name> -f
-
Pull changes from the remote repository
git pull
-
Fetch changes from the remote repository
git fetch
Inspecting & Comparing
-
Show commit logs
git log
-
Show commit logs with a graphical representation
git log --graph --oneline --all
-
Show a specific commit
git show <commit-hash>
-
Show changes over time for a specific file
git log -p <filename>
Undoing Changes
-
Unstage a file (keep the changes)
git reset <filename>
-
Undo the last commit (keep the changes)
git reset --soft HEAD~1
-
Undo the last commit (discard the changes)
git reset --hard HEAD~1
-
Revert a commit by creating a new commit
git revert <commit-hash>
Stashing
-
Stash changes
git stash
-
List stashes
git stash list
-
Apply a stash
git stash apply <stash@{0}>
-
Apply and drop a stash
git stash pop
-
Drop a stash
git stash drop <stash@{0}>
Remote Repositories
-
Add a remote repository
git remote add <remote-name> <remote-url>
-
Show remote repositories
git remote -v
-
Rename a remote repository
git remote rename <old-name> <new-name>
-
Remove a remote repository
git remote remove <remote-name>
Advanced Commands
-
Rebase current branch onto another branch
git rebase <branch-name>
-
Squash commits
git rebase -i <commit-hash>
-
Cherry-pick a commit
git cherry-pick <commit-hash>
-
Create a tag
git tag <tag-name>
-
Push tags to remote
git push origin --tags
-
Delete a local tag
git tag -d <tag-name>
-
Delete a remote tag
git push origin :refs/tags/<tag-name>
-
Clean untracked files
git clean -f
-
Interactive rebase
git rebase -i HEAD~<number-of-commits>