Managing Remotes

Add a named remote (you can have multiple remotes):

git remote add github

git remote add gitlab

git push github
git push gitlab

Listing remotes:

git remote -v

Overloading origin with another remote:

git remote set-url ––add origin

Deleting a remote:

git remote remove origin

Synchronizing a local fork with remote:

git remote add upstream
git merge upstream/master
git push origin master


Creating submodules in an existing repo:

git submodule add

Cloning a repository with submodules:

git clone
git submodule init
git sumbodule update

Staging and Commits

Remove a file or folder from the staging area:

git reset HEAD -- <file or folder>


Deleting a local branch:

git branch -d branch_name

Deleting a remote branch:

push --delete remote_name branch

Fetch all branches from a remote:

git fetch --all

Checkout specific files from another branch:

git checkout branch_name file file2

Update a single file from upstream:

git checkout origin/master --  file

Merge a specific commit to current branch:

git cherry-pick 63344f2

Hide branch changes:

git stash

Restore branch changes:

git stash apply


Fetch a PR for local testing

Checkout PR into a new branch.

git fetch upstream pull/{PR-NUMBER}/head:test-branch-name

Merge PR into local master branch, and test.

git checkout master
git merge test-branch-name

If code was bad, revert to previous commit.

git reset --hard HEAD@{1}

Move a commit to another repo

git show –pretty=email > patch git am --committer-date-is-author-date < patch