Skip to content

Instantly share code, notes, and snippets.

@joe-oli
Last active April 8, 2025 03:51
Show Gist options
  • Save joe-oli/aaaeec4b9b9b8338bb133726dd2e81dc to your computer and use it in GitHub Desktop.
Save joe-oli/aaaeec4b9b9b8338bb133726dd2e81dc to your computer and use it in GitHub Desktop.
Git notes
List ALL branches
>git branch
LIST branches by wildcard
>git branch --list "122160*"
======================
CLONE A SINGLE BRANCH
======================
cd to your desired location, e.g. >cd \temp
temp>git clone --single-branch --branch user-story/1069_my_branch_to_clone C:\Projects\DevOps\MyRepo MyNewRepo_with_Cloned_Branch_only
C:\Projects\DevOps\MyRepo is the folder for your original (local) repo, i.e. where .gitignore is;
C:\temp\MyNewRepo_with_Cloned_Branch_only, will be the folder where the new single-branch repo is created;
i.e. C:\temp\MyNewRepo_with_Cloned_Branch_only\.gitignore will be inside the folder;
>git fetch --all
Fetch all remote repos, don't download them yet.
Then to view all the branches:
>git branch
Then checkout specific branch:
>git checkout -t -origin/name_of_branch
creates a local branch, name derived from remote branch, and -t for tracking it.
-t or --track
+++++++++
RENAME BRANCH
OLD_NAME: 115311_MilestoneReport_cosmeticChanges
to
NEW_NAME: user-story/115311_MilestoneReport_cosmeticChanges
How To Rename a Local and Remote Git Branch
#1. switch to local branch which you want to rename:
> git checkout <old_name>
#1.ALT: Or check that it is already current (checked-out)
>git branch
(in green with asterisk)
* 115311_MilestoneReport_cosmeticChanges
96331_populate_grantee_authorised_person
...etc
#2. rename local branch (i.e. MOVE to new name)
>git branch -m <new_name>
git branch -m user-story/115311_MilestoneReport_cosmeticChanges
#2b. Check that new local branch is currently checked out(,and old is gone - it was renamed)
user-story/114449_HistoryTab_forAgent
* user-story/115311_MilestoneReport_cosmeticChanges
user-story/83223
#3. Push the <new_name> local branch and reset the upstream branch:
> git push origin -u <new_name>
git push origin -u user-story/115311_MilestoneReport_cosmeticChanges
#3b. Verify that remote branch exists.
>git branch -r
...
origin/user-story/115289_someOtherBranch
origin/user-story/115311_MilestoneReport_cosmeticChanges
origin/user-story/61770
...etc
#4. Delete the <old_name> remote branch:
git push origin --delete <old_name>
>git push origin --delete 115311_MilestoneReport_cosmeticChanges
>git push origin --delete 115311_MilestoneReport_cosmeticChanges
info: detecting host provider for 'http://proxy:8080/'...
warning: auto-detection of host provider took too long (>2000ms)
warning: see https://aka.ms/gcmcore-autodetect for more information.
To https://dev.azure.com/MyOrgName/Proj/_git/Projname
- [deleted] 115311_MilestoneReport_cosmeticChanges

When dealing with new folders, git status only shows the top-level directory as untracked, not the individual files within it. This is why you're seeing server/GemsConnect.EFLayer/ listed, but not the files inside it.

To see the untracked files within the directory, you can use the following command:

git status -uall

This will show all untracked files, including those nested within directories.

Alternatively, you can use:

git add server/GemsConnect.EFLayer/

This will stage all the new files within the GemsConnect.EFLayer directory. After running this command, git status should show the files under "Changes to be committed."

MORE INFO

The -uall option in the git status command is shorthand for --untracked-files=all. This option tells Git to show all untracked files, including those nested within directories.

Here's a breakdown of the --untracked-files options:

  • no: Do not show untracked files.
  • normal: Show untracked files and directories (this is the default behavior).
  • all: Show all untracked files, including those nested within directories.

So, when you use git status -uall, Git will display every untracked file in your working directory, regardless of how deeply nested they are. This is useful for ensuring you don't miss any new files that need to be staged.

To see most recent commit (across branches), on local repo:
It lists the most recent commit on each branch, and the branches are sorted by the date of the most recent commit:
```bash
git for-each-ref --sort=-committerdate refs/heads/ --format="%(committerdate:iso8601) %(refname:short)"
```
This command will give you a list of branches that you've worked on, sorted by the date of the most recent commit on each branch.
The most recently modified branch will be at the top.
Example output:
2024-08-20 04:41:45 +0000 bugs/12345_blueScreenOfDeath
2024-08-20 12:45:31 +1000 user-story/123444_Tab1_ShowHideQuestions
2024-08-13 23:59:36 +1000 task/setup_NewDevEnv
2024-08-09 08:23:57 +1000 user-story/123555_implementValidations
...etc etc...
+++++++++++++
You can use the `git log` command to list the most recent changes (commits) on THE CURRENT BRANCH;
The command shows the commit history in reverse chronological order (most recent commits first).
Here is a command that will show you the commit hash, author, date, and commit message:
```bash
git log --pretty=format:"%h - %an, %ar : %s"
```
In this command:
- `%h` is the abbreviated commit hash
- `%an` is the name of the author
- `%ar` is the author date, relative
- `%s` is the subject (commit message)
If you want to see the branch name for each commit, you can use the `--source` option with `git log`. This will prepend the name of the ref (branch) that points to each commit:
```bash
git log --source --all --pretty=format:"%h - %an, %ar : %s"
```
In this command, `--all` is used to show all branches, not just the current one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment