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 directory
  • git pull: Fetch from and integrate with another repository or a local branch
    • git pull --rebase: Rebase local commits on top of the remote branch
    • git pull --rebase --force: Force rebase remote branch, discarding local commits
  • git push: Update remote refs along with associated objects
    • git push --force: Force push to remote branch
  • git fetch: Download objects and refs from another repository
    • git fetch --tags: Prune remote-tracking branches no longer on remote
    • git 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 state
    • git merge --continue: Continue the merge after resolving conflicts
    • git merge --squash: Perform the merge and commit the result in a single commit
  • git rebase: Reapply commits on top of another base tip
    • git rebase --abort: Abort the rebase operation and reset HEAD to the original branch
    • git rebase --continue: Continue a rebase after resolving conflicts
    • git rebase --skip: Skip the current patch and continue rebase operation

Save Work

  • git add <file>: Add file contents to the index
    • git add .: Add all files to the index
  • git commit: Record changes to the repository
    • git commit -m "message": Commit with message
    • git commit --amend --no-edit: Amend the last commit without editing last commit message
    • git commit --amend -m "message": Amend the last commit with editing last commit message
  • git stash: Stash the changes in a dirty working directory away
    • git stash pop: Remove a single stashed state from the stash list and apply it on top of the current working tree state
    • git stash apply: Like pop, but do not remove the state from the stash list
    • git stash drop: Remove a single stashed state from the stash list
    • git stash clear: Remove all the stash entries

Discard Work

  • git restore -- <file>: Discard changes in working directory

Working with Commits

  • git log: Show commit logs
  • git cherry-pick <from-commit>: Apply the changes introduced by some existing commits
  • git reset <to-commit>: Reset current HEAD to the specified state
    • git reset --hard: Reset current HEAD to the specified state and discard all changes in working directory and index
    • git reset --soft: Reset current HEAD to the specified state and retain all changes in working directory and index
    • git 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 status
  • git rev-list --count <branch>: Show the number of commits in the current branch