Skip to content

Instantly share code, notes, and snippets.

@jonchurch
Last active July 3, 2024 00:32
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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment