Wiki/git.md

1.7 KiB

Git

Submodule

  • Add submodule git submodule add <url> <submodulePath>
  • Remove submodule:
    • Remove the submodule entry from .git/config: git submodule deinit -f <submodulePath>
    • Remove the submodule directory from the .git/modules directory: rm -rf .git/modules/<submodulePath>
    • Remove the entry in .gitmodules and remove the submodule directory: git rm -f <submodulePath>

Merge repository B into a unrelated repository A

  • Open repo A
  • Add remote <repoB> to repo B: git remote add <RemoteRepoB> <localPathToRepoB>
  • Create new unrelated branch: git checkout --orphan <BranchRepoB>
  • Delete all local files: git rm -rf .
  • Pull whole history from repo B: git pull --allow-unrelated-histories <RemoteRepoB> <RemoteBranchToPull>
  • Optionally merge branch into main: git merge --allow-unrelated-histories <BranchRepoB> main

Undo accidental amend of two commits and create separate commit

  • git reset --soft @{1}
  • git commit -C @{1}

Create folder for specific branch

  • git worktree add <pathToFolder> <branchName>

Create empty branch

  • git checkout --orphan NEWBRANCH
  • git rm -rf .

Delete remote branch

  • git push <remote> --delete <branch>

Show upstream of branch

  • git branch -vv

  • Rebase first commit: git rebase -i --root

Add Commit to Foreign PR

  • Add new PRs remote: git remote add <someRemoteName> <remoteAddress>
  • Fetch: git fetch <someRemoteName>
  • Checkout branch git checkout -b <someRemoteName>/<branch> <fullRemoteBranchName>
  • Commit changes
  • Push: git push <someRemoteName> HEAD:<branch>

Checkout Github PR

  • Checkout with same name as on remote: git fetch origin pull/$ID/head
  • Checkout with same name <NAME>: git fetch origin pull/$ID/head:<NAME>