git
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
git [2022/04/08 08:23] – Add instructions for rebasing dblume | git [2025/07/08 16:13] (current) – [Building Git] dblume | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== git ====== | ====== git ====== | ||
- | Make the clone at the remote repository. | + | ===== Building Git ===== |
- | < | + | In Ubuntu, |
- | git clone git@github.com: | + | |
- | </ | + | |
- | < | + | < |
- | git remote add upstream git:// | + | sudo apt-get install libssl-dev libcurl4-gnutls-dev gettext |
</ | </ | ||
+ | There was [[https:// | ||
+ | <code bash> | ||
+ | git clone --filter=blob: | ||
+ | # or: git clone --branch v2.50.1 --depth 1 https:// | ||
+ | # or even: wget https:// | ||
- | Enter a comment for the commit. | + | make prefix=$HOME/.local -j$(nproc) |
- | < | + | make prefix=$HOME/.local install |
- | git commit -a | + | |
</ | </ | ||
- | Double checking the alias names... | + | This will put git in '' |
- | < | + | ===== Using Git ===== |
- | $ git remote -v | + | |
- | origin | + | |
- | origin | + | |
- | upstream | + | |
- | upstream | + | |
- | </ | + | |
- | The push that worked from within | + | Make the clone of the remote repository. |
< | < | ||
- | git push origin | + | git clone git@github.com: |
</ | </ | ||
- | |||
- | ===== Make a local clone of an existing remote project ===== | ||
- | |||
- | From the parent directory: | ||
< | < | ||
- | git clone https:// | + | git remote add upstream git:// |
- | cd python-interview | + | |
</ | </ | ||
- | ===== When it' | + | If it' |
- | + | ||
- | **1.** Check which files are staged for commit. | + | |
< | < | ||
- | git status | + | git clone \ |
+ | --filter=blob: | ||
+ | -b main \ | ||
+ | --single-branch \ | ||
+ | --no-tags \ | ||
+ | --shallow-submodules \ | ||
+ | --recurse-submodules=os/ | ||
+ | --recurse-submodules=': | ||
+ | git@fake.github.com: | ||
</ | </ | ||
- | **2.** Then, when you're ready to commit locally, | + | Eventually, if you want to add another branch to a single-branch clone: |
- | < | + | |
- | git commit | + | |
- | </ | + | |
- | **3.** When you're ready to push a local commit to a remote one... | + | Or to de-single-branch-ize |
- | < | + | |
- | git push -u origin | + | |
- | </ | + | |
- | or | ||
- | |||
- | < | ||
- | git push https:// | ||
- | </ | ||
===== Creating a new remote repository from an existing local one ===== | ===== Creating a new remote repository from an existing local one ===== | ||
Line 76: | Line 64: | ||
git add README.md | git add README.md | ||
git commit -m "first commit" | git commit -m "first commit" | ||
- | git remote add origin | + | git remote add origin |
- | git push -u origin | + | git push -u origin |
- | </ | + | |
- | + | ||
- | ===== Creating a new remote repository from an existing local one ===== | + | |
- | + | ||
- | I created wine-tasting at [[https:// | + | |
- | + | ||
- | < | + | |
- | ~$ mkdir wine-tasting | + | |
- | ~$ cd wine-tasting/ | + | |
- | wine-tasting$ vim README.md | + | |
- | wine-tasting$ vim LICENSE.txt | + | |
- | wine-tasting$ git init | + | |
- | wine-tasting$ git add README.md | + | |
- | wine-tasting$ git add LICENSE.txt | + | |
- | wine-tasting$ git add test.py | + | |
- | wine-tasting$ git add wine_allocator.py | + | |
- | wine-tasting$ git commit -m "first commit" | + | |
- | wine-tasting$ git remote add origin https:// | + | |
- | wine-tasting$ git push -u origin master | + | |
</ | </ | ||
Line 113: | Line 82: | ||
<code bash> | <code bash> | ||
- | $ git fetch --all # TODO investigate why fetch | + | $ git fetch --all # Bring your copy of the remote up-to-date |
$ git remote prune origin | $ git remote prune origin | ||
</ | </ | ||
Line 120: | Line 89: | ||
==== Creating a branch (and possibly pushing to upstream origin) ==== | ==== Creating a branch (and possibly pushing to upstream origin) ==== | ||
- | $ git checkout | + | $ git switch |
Switched to a new branch ' | Switched to a new branch ' | ||
Line 129: | Line 98: | ||
$ git push --set-upstream origin new_branch | $ git push --set-upstream origin new_branch | ||
+ | ==== Creating a local branch from an existing remote ==== | ||
+ | |||
+ | After doing a fetch, and suppose " | ||
+ | |||
+ | $ git switch remote-branch | ||
==== Changing a local branch to a new remote branch ==== | ==== Changing a local branch to a new remote branch ==== | ||
Line 155: | Line 129: | ||
<code bash> | <code bash> | ||
- | git checkout | + | git switch |
- | # If main is getting updated, | + | |
- | # | + | # If main is getting updated, rebase like so: |
+ | # | ||
# git pull | # git pull | ||
- | # | + | # |
- | # git rebase main | + | # git rebase main # --dry-run to test first |
+ | |||
+ | # Consider whether you want to squash commits before pushing | ||
+ | # git reset --soft HEAD~3 | ||
git commit -m "fixed bug" | git commit -m "fixed bug" | ||
git push --set-upstream origin bugfix/ | git push --set-upstream origin bugfix/ | ||
- | (do a merge/pull request | + | # Do a MR/PR that deletes the original branch at the remote |
- | git checkout | + | git switch |
git branch -d bugfix/ | git branch -d bugfix/ | ||
git pull | git pull | ||
+ | </ | ||
+ | |||
+ | ====== Resolving a Merge Conflict ====== | ||
+ | |||
+ | < | ||
+ | git mergetool (possibly with filename) | ||
+ | |||
+ | # +----------+-----------+------------------+ | ||
+ | # | (others) | (common) | ||
+ | # | LOCAL | BASE | REMOTE | ||
+ | # +-----------------------------------------+ | ||
+ | # | | | ||
+ | # | temp file with <<< | ||
+ | # +-----------------------------------------+ | ||
+ | |||
+ | git commit -a -m " | ||
+ | </ | ||
+ | |||
+ | Possibly keep rebasing. | ||
+ | < | ||
+ | git rebase --continue | ||
+ | git pull | ||
+ | </ | ||
+ | |||
+ | ====== Alternative to Rebasing: Stash, Pull, Unstash ====== | ||
+ | |||
+ | **[[https:// | ||
+ | |||
+ | > I attempted to rebase my branch to main and end up pulling in all of the intermediate commits on main into my branch, and the merge request suddenly requires approval from unrelated code owners. | ||
+ | |||
+ | **Workaround**: | ||
+ | |||
+ | Instead of doing a rebase before commit, stash your changes, set the upstream to origin/ | ||
+ | |||
+ | <code bash> | ||
+ | git stash push -m "hold for pull" | ||
+ | git switch main | ||
+ | git pull | ||
+ | git stash pop # restores stash on top of main | ||
+ | git add/commit | ||
+ | git push -f origin < | ||
+ | </ | ||
+ | |||
+ | If others have made changes in the branch you're working on, you can try to rebase directly onto the latest from the remote: | ||
+ | |||
+ | <code bash> | ||
+ | git pull --rebase | ||
+ | </ | ||
+ | |||
+ | ====== Applying changes in a stash to a changed file ===== | ||
+ | |||
+ | When '' | ||
+ | |||
+ | < | ||
+ | git stash show -p | patch -p0 | ||
+ | </ | ||
+ | |||
+ | ====== When using Merge Commits instead of Rebasing ====== | ||
+ | |||
+ | Use '' | ||
+ | |||
+ | ====== Two Independent Remotes ====== | ||
+ | |||
+ | After you've already set up one remote, '' | ||
+ | |||
+ | <code bash> | ||
+ | git remote add other-origin ssh:// | ||
+ | git fetch other-origin | ||
+ | git branch --set-upstream-to=other-origin/ | ||
+ | git pull other-origin other-main --allow-unrelated-histories | ||
+ | git mergetool < | ||
+ | ... do your pushes and pulls, then to switch back to origin... | ||
+ | git branch --set-upstream-to=origin/ | ||
</ | </ | ||
Line 179: | Line 231: | ||
git$ cd testcode.git/ | git$ cd testcode.git/ | ||
testcode.git$ git init --bare | testcode.git$ git init --bare | ||
+ | testcode.git$ git config pack.threads 8 # Otherwise clone might fail with " | ||
</ | </ | ||
Line 190: | Line 243: | ||
... | ... | ||
testcode$ git remote add origin ssh:// | testcode$ git remote add origin ssh:// | ||
- | testcode$ git push origin | + | testcode$ git push origin |
</ | </ | ||
- | I could' | + | Options are: |
+ | |||
+ | * I chose GitHub-like [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | |||
+ | |||
+ | ====== Limit scope of huge repos ====== | ||
+ | |||
+ | Create a .gitconfig file at the base of your repo: | ||
+ | <file git .gitconfig> | ||
+ | [remote " | ||
+ | fetch = +refs/ | ||
+ | fetch = +refs/ | ||
+ | tagopt = --no-tags | ||
+ | </ | ||
+ | |||
+ | Or explicitly specify your flags: | ||
+ | <code bash> | ||
+ | git fetch --no-tags origin main | ||
+ | git pull --no-tags origin main | ||
+ | git submodule foreach git pull --no-tags origin main | ||
+ | </ | ||
+ | |||
+ | The submodule one is an optimization for the more general: | ||
+ | <code bash> | ||
+ | git submodule update --recursive | ||
+ | </ | ||
+ | |||
+ | ====== Submodules ====== | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | Cloning a repo doesn' | ||
+ | |||
+ | <code bash> | ||
+ | git submodule update --init --recursive | ||
+ | </ | ||
+ | |||
+ | Git pull and checkout don't update submodules. To actually update them, you have to run the following every time you switch branches or pull. | ||
+ | |||
+ | <code bash> | ||
+ | git submodule update --recursive | ||
+ | </ | ||
+ | |||
+ | ====== git vim mergetool on macOS ====== | ||
+ | |||
+ | File / | ||
+ | |||
+ | <file bash / | ||
+ | FINAL_CMD=" | ||
+ | </ | ||
+ | But vim has a problem with " | ||
git.1649431408.txt.gz · Last modified: 2023/04/12 20:44 (external edit)