Git useful commands
Add alias for displaying the status
git config --global alias.s status
Display current configuration
git config --list
Add user configuration
git config --global user.name "Your Name"
git config --global user.email "name@mail.com"
Set pull/rebase behavior
git config --global pull.rebase true
Set the remote as upstream:
git config --global push.autoSetupRemote true
Remove remote branches from local repository
git remote prune origin
Delete the local branch that was pruned
git branch -D name-of-the-branch-to-delete
Show all branches
git branch -a
Discover changes between two branches
git diff --name-status branch_one..branch_two
Compare two branches
git diff branch_one..branch_two
Use another identity
git config core.sshcommand "ssh -i ~/.ssh/your_private_key -o IdentitiesOnly=yes -F /dev/null"
Remove last commit
git reset --hard HEAD^
git push origin -f
Working with remotes
View existing remotes
git remote
or with showing URLs:
git remote -v
Add a remote
git remote add <name> <url>
e.g.:
git remote add private https://github.com/username/private_repo.git
Remove a remote
git remote remove <name>
Rename a remote
git remote rename <old-name> <new-name>
Change remote URL
git remote set-url <name> <new-url>
Example usage
git remote add private https://github.com/username/private_repo.git
git push -u private main
Tip
-u,--set-upstreamFor every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull and other commands.