Git. Most used commands

Most used git commands.

Create Git repository
git init

Set remote Git repository
git remote add repoName remoteRepositoryUrl
remoteRepositoryUrl example - https://github.com/userId/repoName.git

Show remote Git repositories
git remote -v

Remove remote Git repository
git remote rm repoName

Stage changes
git add -A - stage everything in the working directory
git add HelloWorld.java - stage a file

Status
git status

Show unstaged differences
git diff

Discard unstaged changes
git checkout -- . - discard all unstaged changes in the working directory
git checkout HelloWorld.java - discard unstaged changes in HelloWorld.java

Commit staged changes
git commit -m "Message"

Undo the last commit
git reset --soft HEAD^

Show branches
git branch

Switch to branch
git checkout branchName

Create a branch and switch
git checkout -b newBranchName

Fetch all remotes
git fetch --all

Pull remote branch
git pull repoName branchName

Push branch to remote
git push repoName branchName

Learn more about Git on git-scm.com