Git
13 January, 2023 - Tags: git
I created this guide based on my experience with git. This is the first thing I look into for anything related to git followed by git documentation.
Checking out a PR
I prefer to use gh
command line tool but since it always requires a token, working with it on a remote session is hard. You've to manually paste your access token to a remote instance. Here's how you achieve the same using git.
$ git fetch origin pull/<id>/head:<branch-name>
$ git checkout branch-name
Worktree
I've been working with worktree for a while now and here's my snippets for working on this.
- For me, all the branch leaves under the
wtree
directory under the repo.
# clone the repo
$ git clone --bare <repo-url> <project-name>
# add a branch
$ git worktree add -b <branch-name> wtree/<branch-name>
# delete a branch
$ git worktree remove <branch>
# pulling changes from a remote branch
$ git worktree add -b <branch-name> wtree/<branch-name>
# pull the changes on that branch
$ git pull origin <branch-name>
# How to know what are the available branches in your remote repo?
$ git ls-remote --heads origin
Branches
# deleting a branch locally
$ git branch -d <branch-name> # only delete if it's merged into current branch
$ git branch -D <branch-name> # force delete even if it's not merged
# deleting a remote branch
$ git push origin --delete <branch-name>
Managing Remotes
# adding a remote
$ git remote add <repo-url>
# removing a remote
$ git remtoe remove <repo-url>
# changing the URL of the remote
$ git remote set-url <remote-name> <repo-url> # remote-name (origin,upstream etc.)
git diffs
- git diff is something that you'll check almost everytime before creating a PR. Here are some commands that I use with git diff on a very frequent basis.
You can list the names of all those files where you've made any changes.
$ git diff --name-only
- Now you can do multiple things with this, you can open all the files where you've made any changes using the following command.
$ git diff --name-only | xargs hx
Note: Helix is my primary editor and I love it.
- You can also search within your diff for patterns using the following command.
$ rg gcs_credentials (git diff --name-only)
setting upstream
Sometimes, I want to leave my origin and do everything from upstream. Check this article: https://developers.redhat.com/articles/2023/09/07/drop-git-pull-fetch-and-rebase#
git branch --set-upstream-to=upstream/main main
In lazygit, press u
followed by s
and then choose the branch you want to select as upstream.