Skip to content

Instantly share code, notes, and snippets.

@jonchurch
Last active April 15, 2024 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 often want to look at commit history, individual commits, the changes a commit brought, etc.

This can be challenging when you don't know the exact commit or range you want.

If you only want to see X number of commits, you can pass a numerical flag: git log -3 will output the last 3 commits.

You can further filter by author: git log --author jonchurch -3 shows the last 3 commits by author jonchurch.

I also often want to be reminded of what changes a range of commits introduced. That's easy with the --patch flag

git log --author jonchurch --patch -3

Often I want to remember "What have I done on this branch so far?". Especially useful for context switching, picking up work I haven't touched in a weekk or so.

The following will show commits that ... uhh, aren't in the parent branch I'm comparing against?:

git log main..

commit 4296282d367e43c1e20b1c3eec72ad1995a0ca8c (HEAD -> jonchurch/demo-site-schemaname)
Author: Jon Church <jonchurch@microsoft.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@microsoft.com>
Date:   Fri Aug 26 15:20:57 2022 -0400

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

Even more commonly I want to be reminded what the patch was for each commit:

git log master.. --patch

commit 4296282d367e43c1e20b1c3eec72ad1995a0ca8c (HEAD -> jonchurch/demo-site-schemaname)
Author: Jon Church <jonchurch@microsoft.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-microsoft-com/src/pages/management/channel/components/deploy/BotPageUrl.tsx b/apps/powerva-microsoft-com/src/pages/management/channel/components/deploy/BotPageUrl.tsx
index ea41561ea0d..58e2f7d5bee 100644
--- a/apps/powerva-microsoft-com/src/pages/management/channel/components/deploy/BotPageUrl.tsx
+++ b/apps/powerva-microsoft-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

To check what commits from a given author are landed to master, but not landed to another branch (like a release), you can 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": This displays a list of commits by the specified author (alias) that are present in origin/master but not in the specified release branch origin/releases/pva/2402.2.

The fetch all updates your local's remote refs, without actually merging anything. Need to do that so you have the latest state of remote master or release branch.

Checking what branches a commit is on

to check what branches a given commit are in, I use: git fetch --all && git branch --contains -r <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 always 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. There's lots of ways to do this, but I have started using git recent

Switch to Previous Branch

git switch -

Git Alias for previous branch traversal

Helpful for if im on a branch. need to switch to master to branch off. this sets me up to come back to my previous branch without having to git reflog to try and remember the exact name of my previous branch

[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