Skip to content

Instantly share code, notes, and snippets.

@jonchurch
Last active July 29, 2024 15:43
Show Gist options
  • Save jonchurch/0c972842f18c91f176b963dee30d9d92 to your computer and use it in GitHub Desktop.
Save jonchurch/0c972842f18c91f176b963dee30d9d92 to your computer and use it in GitHub Desktop.
My Most Used Git Commands

Viewing Commits

I want to look at commit history, individual commits, the changes a commit brought, etc.

I don't know the exact commit or range I want.

To see X number of commits, pass a numerical flag: git log -3 will output the last 3 commits.

Further filter by author: git log --author jonchurch -3 shows the last 3 commits by author jonchurch.

To be reminded of what changes a range of commits introduced, use the --patch flag

git log --author jonchurch --patch -3

"What have I done on this branch so far?". I'm context switching, picking up work I haven't touched in a week or so.

Show commits that aren't in the current branch:

git log main..

commit 4296282d367e43c1e20b1c3eec72ad1995a0ca8c (HEAD -> jonchurch/demo-site-schemaname)
Author: Jon Church <jonchurch@example.com>
Date:   Mon Aug 29 17:14:06 2022 -0400

    stub out FCB use for schemaname in Demo Site URL

commit 469ee13bdb117375123a93dae643eca94ce02ca6
Merge: 8e9df3ec77f 289e396ea37
Author: Jon Church <jonchurch@example.com>
Date:   Fri Aug 26 15:20:57 2022 -0400

    Merge branch 'master' into jonchurch/demo-site-schemaname

What was the patch was for each commit:

git log master.. --patch

commit 4296282d367e43c1e20b1c3eec72ad1995a0ca8c (HEAD -> jonchurch/demo-site-schemaname)
Author: Jon Church <jonchurch@example.com>
Date:   Mon Aug 29 17:14:06 2022 -0400

    stub out FCB use for schemaname in Demo Site URL

diff --git a/apps/powerva-example-com/src/pages/management/channel/components/deploy/BotPageUrl.tsx b/apps/powerva-example-com/src/pages/management/channel/components/deploy/BotPageUrl.tsx
index ea41561ea0d..58e2f7d5bee 100644
--- a/apps/powerva-example-com/src/pages/management/channel/components/deploy/BotPageUrl.tsx
+++ b/apps/powerva-example-com/src/pages/management/channel/components/deploy/BotPageUrl.tsx
@@ -67,12 +67,20 @@ export class BotPageUrlInternal extends React.Component<Props, BotPageUrlStatePr
     const styles = getClassNames(this.props.styles, { theme: this.props.theme ?? getTheme() });
     const isV2Bot = this.props.cdsBot?.isV2Bot;
     const isUacProvisioned = isV2Bot && isFeatureSettingEnabled(FeatureSettings.EnableUacProvisioning);
+    let botIdentifier: string | undefined;
+    if (isFeatureSettingEnabled(FeatureSettings.UseSchemaNameInDemoSiteUrl)) {
+      botIdentifier = this.props.cdsBot?.schemaName;
+    } else if (isUacProvisioned) {
+      botIdentifier = this.context.currentBot?.cdsBotId;
+    } else {
+      botIdentifier = this.context.currentBot?.id;
+    }
     const url = new URL(
       formatString(
         ShareCodeTemplates.demoWebsiteUrl,
         window?.location?.origin,
         this.context.currentEnvironment?.id,
-        this.props.cdsBot?.schemaName ?? ''
+        botIdentifier ?? ''
       )
     );

Checking for missing commits on release branhes

What commits from author are landed to master, but not landed to another branch (like a release)? Do a git log, while ignoring all commits already on the target branch.

git fetch --all && git log origin/master ^origin/releases/pva/2402.2 --author="alias": Shows all commits by the specified author (alias) that are present in origin/master but not in the specified release branch origin/releases/pva/2402.2.

git fetch --all updates your local's remote refs, without actually merging anything. You can fetch just the specific remote and branch you're interested in, but this works too.

Checking what branches a commit is on

Show all branches a commit is in: git fetch --all && git branch -r --contains <sha> The -r checks against remotes, instead of your local branch's state (so no need to pull)

Add a | grep release to only see release branches. Or in PPUX MCS specifically: | grep releases/pva

Navigating Branches

I forget what branch I was working on for that thing I had to put down two weeks ago but didn't open a PR for yet. I have started using git recent to list my recently checked out branches.

Switch to Previous Branch

Go back to the previous branch I checked out. git switch -

Git Alias for previous branch traversal

This is the same thing as git switch - but skips over main|master.

[alias]
 # prev
 # an alias that will go thru previous branches using git reflog until it encounters one that is not master and is not main, and is not the current branch. it will then checkout that branch

 prev = "!f() { \
   branch=$(git rev-parse --abbrev-ref HEAD); \
   branch=${branch:-master}; \
   branch=${branch:-main}; \
   branch=${branch:-HEAD}; \
   git reflog show --all | grep -oP 'checkout: moving from \\K[^ ]+' | grep -v $branch | grep -v master | grep -v main | grep -v HEAD | head -n 1 | xargs git checkout; \
 }; f"

Worktree aware navigation

I used to have hardcoded aliases to navigate to different paths in my project. That broke once I started using worktrees more.

So here's a function which can do worktree aware navigation (knowing to navigate to a dir inside your current worktree, vs harcoded to a location on the filesystem):

function cdgit() {
  local subpath="$1"
  local base_dir="$2"  # Take the base directory as a second argument.

  # Attempt to determine the Git repository root
  local git_root="$(git rev-parse --show-toplevel 2>/dev/null)"

  # Navigate based on whether the Git root was successfully found
  local target_dir
  if [[ -n "$git_root" ]]; then
    target_dir="$git_root/$subpath"
  else
    target_dir="$base_dir/$subpath"  # Use the provided base directory if not in a Git worktree
  fi

  # Check if the directory exists and navigate, otherwise fallback to base_dir and subpath
  if [[ -d "$target_dir" ]]; then
    cd "$target_dir"
  else
    cd "$base_dir/$subpath"
  fi
}

Then, you can use aliases together with a fallback location. Lets say I want to navigate to my /packages dir in my git repo, with a fallback to the main git repo location if Im either not in a git repo or that path doesn't exist in my current worktree:

alias cdpk="cdgit 'packages' '~/work/my-repo'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment