User Tools

Site Tools


git

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
git [2022/02/04 15:02] – created dblumegit [2023/06/21 09:22] (current) – [git] dblume
Line 1: Line 1:
 ====== git ====== ====== git ======
  
-Make the clone at the remote repository.+Make the clone of the remote repository.
  
 <code> <code>
Line 11: Line 11:
 </code> </code>
  
-Enter comment for the commit.  See section 2.7.2 [[http://cworth.org/hgbook-git/tour/|here]]+If it'huge repo, [[https://github.blog/2020-12-21-get-up-to-speed-with-partial-clone-and-shallow-clone/|consider blobless and single branch and no tags]], like so...
-<code> +
-git commit -a +
-</code> +
- +
-Double checking the alias names...+
  
 <code> <code>
-git remote -v +git clone \ 
-origin  git@github.com:dblume/get-shit-done (fetch) +  --filter=blob:none \ 
-origin  git@github.com:dblume/get-shit-done (push) +  -b main \ 
-upstream        git://github.com/icambridge/get-shit-done (fetch+  --single-branch \ 
-upstream        git://github.com/icambridge/get-shit-done (push)+  --no-tags \ 
 +  --shallow-submodules \ 
 +  --recurse-submodules=os/components/toolchain \ 
 +  --recurse-submodules=':(exclude)**/porting_kit:' \ 
 +  git@fake.github.com:project/project.git
 </code> </code>
  
-The push that worked from within the local directory:+Eventually, if you want to add another branch to a single-branch clone:
  
-<code> +  git remote set-branches --add origin another-branch
-git push origin +
-</code>+
  
-===== Make local clone of an existing remote project =====+Or to de-single-branch-ize a clone:
  
-From the parent directory:+  git remote set-branches origin "*"
  
-<code> 
-git clone https://github.com/realpython/interview-questions.git python-interview 
-cd python-interview 
-</code> 
- 
-===== When it's time to commit ===== 
- 
-**1.** Check which files are staged for commit. 
- 
-<code> 
-git status 
-</code> 
- 
-**2.** Then, when you're ready to commit locally, 
- 
-<code> 
-git commit -a -m "Some useful comment" 
-</code> 
- 
-**3.** When you're ready to push a local commit to a remote one... 
- 
-<code> 
-git push -u origin master 
-</code> 
- 
-or 
- 
-<code> 
-git push https://github.com/dblume/netflix-dvd-feed 
-</code> 
  
 ===== Creating a new remote repository from an existing local one ===== ===== Creating a new remote repository from an existing local one =====
Line 76: Line 43:
 git add README.md git add README.md
 git commit -m "first commit" git commit -m "first commit"
-git remote add origin https://github.com/dblume/hexbright-factory.git +git remote add origin git@github.com:dblume/hexbright-factory.git 
-git push -u origin master +git push -u origin main
-</code> +
- +
-===== Creating a new remote repository from an existing local one ===== +
- +
-I created wine-tasting at [[https://github.com/new]].  Then, to create a new repository on the command line at the local computer: +
- +
-<code> +
-~$ 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://github.com/dblume/wine-tasting.git +
-wine-tasting$ git push -u origin master+
 </code> </code>
  
Line 113: Line 61:
  
 <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  # (where origin is the name of the shared repo) $ git remote prune origin  # (where origin is the name of the shared repo)
 </code> </code>
Line 120: Line 68:
 ==== Creating a branch (and possibly pushing to upstream origin) ==== ==== Creating a branch (and possibly pushing to upstream origin) ====
  
-  $ git checkout -new_branch+  $ git switch -new_branch
   Switched to a new branch 'new_branch'   Switched to a new branch 'new_branch'
  
Line 129: Line 77:
   $ 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 "origin/remote-branch" exists, then just:
 +
 +  $ git switch remote-branch
 ==== Changing a local branch to a new remote branch ==== ==== Changing a local branch to a new remote branch ====
  
Line 150: Line 103:
 git branch -u origin/main main git branch -u origin/main main
 git remote set-head origin -a git remote set-head origin -a
 +</code>
 +
 +====== Fixing a bug in its own branch ======
 +
 +<code bash>
 +git switch -c bugfix/JIRA-1-new-bugfix
 +
 +# If main is getting updated, rebase like so:
 +#   git switch main
 +#   git pull
 +#   git switch bugfix/JIRA-1-new-bugfix
 +#   git rebase main  # --dry-run to test first
 +
 +# Consider whether you want to squash commits before pushing
 +#   git reset --soft HEAD~3  # Moves head back 3, and those 3 become staged
 +
 +git commit -m "fixed bug"
 +git push --set-upstream origin bugfix/JIRA-1-new-bugfix
 +# Do a MR/PR that deletes the original branch at the remote
 +git switch main
 +git branch -d bugfix/JIRA-1-new-bugfix
 +git pull
 +</code>
 +
 +====== Resolving a Merge Conflict ======
 +
 +<code>
 +git mergetool (possibly with filename)  # Bring up the vim 3-way diff
 +
 +# +----------+-----------+------------------+
 +# | (others) | (common)  | (my most recent) |
 +# | LOCAL    | BASE      | REMOTE           |
 +# +-----------------------------------------+
 +# |                                         |
 +# | temp file with <<< ||| >>> diffs        |
 +# +-----------------------------------------+
 +
 +git commit -a -m "Resolved merge conflict"
 +</code>
 +
 +Possibly keep rebasing.
 +<code>
 +git rebase --continue
 +git pull
 +</code>
 +
 +====== Alternative to Rebasing: Stash, Pull, Unstash ======
 +
 +**[[https://roku.slack.com/archives/C453HNABT/p1657224221440529|Problem]]**:
 +
 +> 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/main, do a pull, unstash the change, then commit and push -f upstream to the branch. Kind of like this:
 +
 +<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 <branchname>
 +</code>
 +
 +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  # --dry-run to test first
 +</code>
 +
 +====== Applying changes in a stash to a changed file =====
 +
 +When ''git stash apply'' doesn't work: Show the stash changes and pipe that to patch. Now you have a patch you can apply.
 +
 +<code>
 +git stash show -p | patch -p0
 +</code>
 +
 +====== When using Merge Commits instead of Rebasing ======
 +
 +Use ''--first-parent'' with ''git log''.
 +
 +====== Two Independent Remotes ======
 +
 +After you've already set up one remote, ''origin'', and you want to map your ''main'' to ''other-main'' at ''other-origin'', you can do so like:
 +
 +<code bash>
 +git remote add other-origin ssh://other.com/project.git
 +git fetch other-origin
 +git branch --set-upstream-to=other-origin/other-main main
 +git pull other-origin other-main --allow-unrelated-histories
 +git mergetool <file-with-conflicts>
 +... do your pushes and pulls, then to switch back to origin...
 +git branch --set-upstream-to=origin/main main
 </code> </code>
  
Line 173: Line 221:
 ... ...
 testcode$ git remote add origin ssh://USERNAME@dlma.com/~/git/testcode.git testcode$ git remote add origin ssh://USERNAME@dlma.com/~/git/testcode.git
-testcode$ git push origin master+testcode$ git push origin main
 </code> </code>
  
 I could've used gitweb but I used GitHub-like [[https://github.com/klaussilveira/gitlist|gitlist]] at http://git.dlma.com. I could've used gitweb but I used GitHub-like [[https://github.com/klaussilveira/gitlist|gitlist]] at http://git.dlma.com.
 +
 +====== Limit scope of huge repos ======
 +
 +Create a .gitconfig file at the base of your repo:
 +<file git .gitconfig>
 +[remote "origin"]
 +    fetch = +refs/heads/main:refs/remotes/origin/main
 +    fetch = +refs/heads/user/dblume/*:refs/remotes/origin/user/dblume/*
 +    tagopt = --no-tags
 +</file>
 +
 +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
 +</code>
 +
 +The submodule one is an optimization for the more general:
 +<code bash>
 +git submodule update --recursive  # Add --init before --recursive on first time
 +</code>
 +
 +====== git vim mergetool on macOS ======
 +
 +File /usr/local/Cellar/git/2.38.1/libexec/git-core/mergetools/vimdiff has this line:
 +
 +<file bash /usr/local/Cellar/git/2.38.1/libexec/git-core/mergetools/vimdiff>
 +FINAL_CMD="-c \"set hidden diffopt-=hiddenoff | $CMD | tabfirst\""
 +</file>
 +But vim has a problem with "diffopt-=hiddenoff"
  
git.1644015742.txt.gz · Last modified: 2023/04/12 20:44 (external edit)