Commonly used Git commands.
View local branches:
$ git branch
View all local and remote branches:
$ git branch --all
Create and checkout to a new branch that doesn’t yet exist on local:
$ git checkout -b feature/22.7_fix
Check out to an existing local branch:
$ git checkout feature/22.7
Checkout and track a remote branch on local. Here, origin
is the name of your remote repository:
$ git checkout --track origin/feature/22.8
View local changes:
$ git status
Stage all local changes:
$ git add .
Stage changes containing java
in the file path:
$ git add *java*
Unstage all changes. This does not delete the local changes from disk, it only removes them from the staging area:
$ git reset .
Unstage changes containing java
in the file path:
$ git reset *java*
Delete all local changes. Note that this cannot be undone:
$ git restore .
Delete changes containing java
in the file path. Note that this cannot be undone:
$ git restore *java*
Commit staged changes:
$ git commit -m "JIRA-1 add README"
Push a new local branch to remote:
$ git push --set-upstream origin feature/22.7_fix
Push changes to an existing remote branch:
$ git push
Pull changes of local checkout branch from remote:
$ git pull
Pull changes from a remote branch to local checkout branch. This essentially merges from the remote branch to the local checkout branch:
$ git pull origin feature/22.8
Abort a merge in case of a merge conflict:
$ git merge --abort
Delete a local branch. -D
force deletes the branch even if it hasn’t been pushed or merged yet:
$ git branch -D feature/22.7
Delete a remote branch:
$ git push --delete origin feature/22.7
Stash local changes:
$ git stash -- *
View stack of stashes:
$ git stash list
View changes for stash 0
in stack:
$ git stash show 0
Pop stash from top of stack:
$ git stash pop
Pop stash 1
from stack:
$ git stash pop 1
Delete stash 0
from stack. Note that this cannot be undone:
$ git stash drop 0
Clear entire stack of stashes. Note that this cannot be undone:
$ git stash clear
When you have local changes but need to switch to another branch:
$ git add .
$ git stash -- *
$ git checkout feature/22.7_fix
When you are done and want to switch back to your previous local changes:
$ git checkout feature/22.7
$ git stash pop
When you no longer need the local changes:
$ git reset .
$ git restore .
Sometimes git restore
may not be able to fully remove your local changes (e.g. newly created files). In that case, try this instead:
$ git add .
$ git stash -- *
$ git stash drop 0
When you need to remotely merge a branch:
$ git checkout feature/22.8
$ git pull origin feature/22.7
$ git push
Sometimes a conflict may occur when merging branches:
$ git checkout feature/22.8
$ git pull origin feature/22.7
conflict
Steps to resolve
Get list of conflicted files:
$ git status
Resolve each conflicted file:
$
Commit the de-conflicted changes and continue with the merge:
$ git add .
$ git commit
Close notepad
$ git push