Git – Common Usage

Add SubModule

git submodule add [url] [destination]

Checkout Hard a Specific Commit

# Locate the desired commit-id.
git reflog

# Reset hard.
git reset --hard [commit-id]

Create Non-Bare Remote Repository

Make the remote repository as a working tree.

# Create the repository as usual on the remote location.
git init

# Setup the repository to update when pushing to it.
git config --local receive.denyCurrentBranch updateInstead

Remove File from History


git filter-branch --force --index-filter "git rm -rf --cached --ignore-unmatch [Files2Delete]" --prune-empty --tag-name-filter cat -- --all

Remove SubModule

# (1) Delete the relevant section from the .gitmodules file.
# (2) Stage the .gitmodules changes
git add .gitmodules

# (3) Delete the relevant section from .git/config.
# (4) Remove cache and files.
git rm --cached [Path2SubModule]
git rm -rf .git/modules/[Path2SubModule]

# (5) Commit changes.
git commit -m ".... comments ...."

# (6) Delete untracked submodule.
rm -rf [Path2SubModule]

Reset Last Commit

# Command
git reset [--soft]|[--hard] [Revision]

# Reset last commit and keep the local changes.
git reset --soft HEAD~1

# Reset last commit and discard local changes.
git reset --hard HEAD~1

Set Upstream Name

git push --set-upstream [RepoName] [BranchName]

Unset Local User Data

git config --local --unset
git config --local --unset