git
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
git [2022/10/11 14:04] – git switch instead of git checkout dblume | git [2023/05/08 19:57] – [Resolving a Merge Conflict] dblume | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== git ====== | ====== git ====== | ||
- | Make the clone at the remote repository. | + | Make the clone of the remote repository. |
< | < | ||
Line 11: | Line 11: | ||
</ | </ | ||
- | Enter a comment for the commit. | + | If it' |
- | < | + | |
- | git commit -a | + | |
- | </ | + | |
- | + | ||
- | Double checking the alias names... | + | |
< | < | ||
- | $ git remote | + | git clone \ |
- | origin | + | |
- | origin | + | -b main \ |
- | upstream | + | --single-branch \ |
- | upstream | + | --no-tags \ |
- | </code> | + | --shallow-submodules \ |
- | + | --recurse-submodules=os/components/toolchain \ | |
- | The push that worked from within the local directory: | + | --recurse-submodules=':(exclude)**/porting_kit: |
- | + | git@fake.github.com:project/project.git | |
- | < | + | |
- | git push origin | + | |
- | </code> | + | |
- | + | ||
- | ===== Make a local clone of an existing remote project ===== | + | |
- | + | ||
- | From the parent directory: | + | |
- | + | ||
- | < | + | |
- | git clone https:// | + | |
- | cd python-interview | + | |
- | </ | + | |
- | + | ||
- | ===== When it's time to commit ===== | + | |
- | + | ||
- | **1.** Check which files are staged for commit. | + | |
- | + | ||
- | < | + | |
- | git status | + | |
- | </code> | + | |
- | + | ||
- | **2.** Then, when you're ready to commit locally, | + | |
- | + | ||
- | < | + | |
- | git commit -a -m "Some useful comment" | + | |
- | </ | + | |
- | + | ||
- | **3.** When you're ready to push a local commit to a remote one... | + | |
- | + | ||
- | < | + | |
- | git push -u origin main | + | |
- | </ | + | |
- | + | ||
- | or | + | |
- | + | ||
- | < | + | |
- | git push https://github.com/ | + | |
</ | </ | ||
Line 76: | Line 34: | ||
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 main | git push -u origin main | ||
</ | </ | ||
Line 94: | Line 52: | ||
<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 110: | Line 68: | ||
$ 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 139: | Line 102: | ||
# If main is getting updated, rebase like so: | # 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 | # Consider whether you want to squash commits before pushing | ||
- | # git reset --soft HEAD~3 | + | # 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 | ||
+ | </ | ||
+ | |||
+ | ====== 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 | git pull | ||
</ | </ | ||
Line 159: | Line 144: | ||
**[[https:// | **[[https:// | ||
- | > I attempted to rebase my branch to main and end up pulling in all of the intermediate commits on main into my branch, | + | > 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**: | **Workaround**: | ||
Line 167: | Line 152: | ||
<code bash> | <code bash> | ||
git stash push -m "hold for pull" | git stash push -m "hold for pull" | ||
- | git branch --set-upstream-to origin/main | + | git switch |
git pull | git pull | ||
- | git stash pop (restores stash on top of main) | + | git stash pop # |
git add/commit | git add/commit | ||
git push -f origin < | 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 | ||
</ | </ | ||
Line 177: | Line 176: | ||
Use '' | 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/ | ||
+ | </ | ||
====== git at dlma.com ====== | ====== git at dlma.com ====== | ||
Line 203: | Line 216: | ||
I could' | I could' | ||
+ | |||
+ | ====== 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 | ||
+ | </ | ||
+ | |||
+ | ====== git vim mergetool on macOS ====== | ||
+ | |||
+ | File / | ||
+ | |||
+ | <file bash / | ||
+ | FINAL_CMD=" | ||
+ | </ | ||
+ | But vim has a problem with " | ||
git.txt · Last modified: 2024/04/11 16:58 by dblume