Global Configuration
git config --global credential.helper store
git config --global user.email "[email protected]"
git config --global user.name "Paramdeo Singh"
Push existing Git repository to a remote (e.g. GitLab):
git remote add origin https://gitlab.com/paramdeo/example-repository.git
git add --all
git commit -m "Initial commit"
git push -u origin master
Change URL of remote repository:
# via HTTPS
git remote set-url origin https://gitlab.com/paramdeo/new-repository.git
git remote -v
# via SSH
git remote set-url origin [email protected]:paramdeo/new-repository.git
git remote -v
Changes
Undo all unstaged changes since last commit:
git reset --hard
Undo all staged changes made via the git add
command:
git restore --staged .
Branches
Create branch:
git branch example-branch
List branches:
git branch -a
Delete local branch:
git branch -d example-branch
Delete remote branch:
git push origin --delete example-branch
Push local branch to remote repository:
git push origin example-branch
Switch to a branch:
git checkout example-branch
Change local branch name (branch must be currently checked out):
git branch -m old-name new-name
Merge branches:
git merge source-branch target-branch
Housekeeping
Remove all commits from master
branch:
git checkout --orphan temp-branch
git add --all
git commit
git branch -D master # Delete master branch
git branch -m master # Rename current branch to master
git push -f origin master # Force push master branch (must un-protect master first)
git gc --aggressive --prune=all # Clean local Git history