Skip to content

Instantly share code, notes, and snippets.

@spdrman
Last active August 10, 2023 22:03
Show Gist options
  • Save spdrman/59e667d7a627f5ee4060e3c84af6116d to your computer and use it in GitHub Desktop.
Save spdrman/59e667d7a627f5ee4060e3c84af6116d to your computer and use it in GitHub Desktop.
GIT OSS Contribution Guide
# AUTHENTICATION:
cat ~/.netrc
machine github.com login <login-id> password <token-password> // This is dangerous and not recommended -- use SSH Auth instead
// Github SSH Auth Guide: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
# SUBMIT BUG FIX:
git clone https://github.com/***.git
git checkout -b bug-fix-brokenthingdescription //git switch -c <new-branch> if moving changed from master
# **DO CODING CHANGES**
git commit -am "BUG FIX - description"
git reset HEAD~ // Undo most recent local git commit (undo reset -> git reflog + git checkout -b someNewBranchName shaYouDestroyed)
git rebase master // re-base your branch to the changes made by another user that you pulled into master -- from inside your branch
git merge master // Alternative to git rebase, and it's much faster
git push origin bug-fix-brokenthingdescription //same as branch name created above
# Additional change
git checkout master
git checkout -b bug-fix2-brokenthingdescription
# **DO CODING CHANGES**
git commit -am "BUG FIX - description"
git push origin bug-fix2-brokenthingdescription //same as branch name created above
# REVIEW A PR:
git fetch origin pull/ID/head:BRANCHNAME
git checkout BRANCHNAME
# **EDIT PR**
git push origin BRANCHNAME
# UNDO MISTAKE PUSH/COMMIT:
# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard <commit ID to reset to> && git push --force
# This will create one separate revert commit (undoing a previous push with commit_sha_id):
git revert --no-commit <commit_sha_id> && git commit
OR
git revert --no-commit <commit_sha_id_to_start_with>..HEAD # Back out all changes from commit_sha to current state
# Reverting a merge commit (commit of a re-base / merge with master)
git revert -m 1 <merge_commit_sha>
# SUB-MODULES
git submodule add <remote_url> <destination_folder> // To add a Git submodule
# Whenever you are cloning a Git repository having submodules, you need to execute an extra command in order for the submodules to be pulled.
git submodule update --init --recursive // To pull a Git submodule
# In some cases, you are not pulling a Git submodule but you are simply look to update your existing Git submodule in the project.
git submodule update --remote --merge // To update an existing Git submodule
# REMOVE SUB-MODULE
mv path/to/submodule path/to/submodule_tmp
# if you want to completely remove the folders and files associated with sub-module from working tree
git submodule deinit -f -- path/to/submodule
rm -rf .git/modules/path/to/submodule
git rm -f path/to/submodule // no trailing slash
# or, if you want to leave it in your working tree and have done the first command to mv submodule to submodule_tmp
git rm --cached path/to/submodule
mv path/to/submodule_tmp path/to/submodule
# BRANCHES
git branch -a // List all local branches
git branch --delete <branchname> // Delete LOCAL branch named <branchname>
git push origin --delete <branchname> // Delete REMOTE branch named <branchname>
// make sure "origin" matches from -a output of remote/origin/<branchname>
- HOW TO UNDO DELETE LOCAL BRANCH:
git branch --delete example-branch
warning: deleting branch 'example-branch' that has been merged to
'refs/remotes/origin/example-branch', but not yet merged to HEAD.
Deleted branch example-branch (was 44643b0).
❯ git branch example-branch 44643b0 // example-branch sha was 44643b0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment