18 Apr 2021  Meta Code


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

Undo all unstaged changes since last commit:

git reset --hard

Undo all staged changes made via the git add command:

git restore --staged .

Switch to branch:

git checkout some-branch

Change local branch name (branch must be currently checked out):

git branch -m new-name

Change remote branch name (and push changes):

git push origin :"old-name" "new-name"
git push origin -u new-name

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
Webmentions & Comments

Copyright © Paramdeo Singh. Built with Jekyll and in Guyana. All Rights Reserved.

Last Site Build on Sun, 09 May 2021 20:55:38 -0400

1MB Club Badge