Git is a version control system majorly used for code management. It is a distributed version control system. It is a free and open source software.
Sync Branches
git clone <url>
: Clone a repository into a new directorygit pull
: Fetch from and integrate with another repository or a local branchgit pull --rebase
: Rebase local commits on top of the remote branchgit pull --rebase --force
: Force rebase remote branch, discarding local commits
git push
: Update remote refs along with associated objectsgit push --force
: Force push to remote branch
git fetch
: Download objects and refs from another repositorygit fetch --tags
: Prune remote-tracking branches no longer on remotegit fetch --all
: Fetch all remotes
git merge
: Merge another branch to current branch\git merge --abort
: Abort the merge process and try to reconstruct the pre-merge stategit merge --continue
: Continue the merge after resolving conflictsgit merge --squash
: Perform the merge and commit the result in a single commit
git rebase
: Reapply commits on top of another base tipgit rebase --abort
: Abort the rebase operation and reset HEAD to the original branchgit rebase --continue
: Continue a rebase after resolving conflictsgit rebase --skip
: Skip the current patch and continue rebase operation
Save Work
git add <file>
: Add file contents to the indexgit add .
: Add all files to the index
git commit
: Record changes to the repositorygit commit -m "message"
: Commit with messagegit commit --amend --no-edit
: Amend the last commit without editing last commit messagegit commit --amend -m "message"
: Amend the last commit with editing last commit message
git stash
: Stash the changes in a dirty working directory awaygit stash pop
: Remove a single stashed state from the stash list and apply it on top of the current working tree stategit stash apply
: Like pop, but do not remove the state from the stash listgit stash drop
: Remove a single stashed state from the stash listgit stash clear
: Remove all the stash entries
Discard Work
git restore -- <file>
: Discard changes in working directory
Working with Commits
git log
: Show commit logsgit cherry-pick <from-commit>
: Apply the changes introduced by some existing commitsgit reset <to-commit>
: Reset current HEAD to the specified stategit reset --hard
: Reset current HEAD to the specified state and discard all changes in working directory and indexgit reset --soft
: Reset current HEAD to the specified state and retain all changes in working directory and indexgit reset --mixed
: Reset current HEAD to the specified state and retain changes in working directory but discard changes in index
Inspect
git status
: Show the working tree statusgit rev-list --count <branch>
: Show the number of commits in the current branch