Skip to content

Commit

Permalink
Tidy up my git notes
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisguest75 committed Jan 3, 2024
1 parent 44ee265 commit 44d451a
Show file tree
Hide file tree
Showing 8 changed files with 356 additions and 282 deletions.
34 changes: 30 additions & 4 deletions 36_git/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,31 @@

Demonstrates some examples of using `git` queries and tools

## Contents

- [README](#readme)
- [Contents](#contents)
- [Sections](#sections)
- [Installing a new machine](#installing-a-new-machine)
- [Upgrade](#upgrade)
- [Git Extras](#git-extras)
- [Open repo in code](#open-repo-in-code)
- [Examining repos](#examining-repos)
- [Clean](#clean)
- [Resources](#resources)

TODO:

* Gitlab cli [glab](https://glab.readthedocs.io/en/latest/)
* [glab source](https://github.com/profclems/glab)
* Deleting branches
* git filter-repo
* git filter-repo - https://github.com/newren/git-filter-repo
* git reset --hard origin/feature reset against a new pushed version of a branch.
* .gitattributes https://git-scm.com/docs/gitattributes
* git update-index --assume-unchanged sites/default/settings.php
* git check-ignore vscode/list_extensions.sh
* git bisect example
* https://git-scm.com/docs/git-notes

## Files
## Sections

* FAQS - [FAQS.md](./sections/FAQS.md)
* Rebasing fun - [REBASING_FUN.md](./sections/REBASING_FUN.md)
Expand Down Expand Up @@ -94,9 +106,23 @@ git effort -- --since='yesterday'
git effort -- --since='1 month ago'
```

## Clean

To restore a repo to a fresh clone.

```sh
# -x Don’t use the standard ignore rules
# -d Specify -d to have it recurse into such directories
# -f Force
git clean -xfd
```

## Resources

* Git --fast-version-control [here](https://git-scm.com/)
* [git-extras](https://github.com/tj/git-extras/blob/master/Commands.md) are a really good set of useful supplementary commands.
* [github cli](https://github.com/cli/cli) tool that supports creating PRs directly from the shell
* Git Release Notes [here](https://github.com/git/git/tree/master/Documentation/RelNotes)
* Git blog from github [here](https://github.blog/tag/git/)
* Git Rev News [here](https://git.github.io/rev_news/)
* Learn Git Branching [here](https://learngitbranching.js.org/)
125 changes: 125 additions & 0 deletions 36_git/sections/BRANCHES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# BRANCHES

NOTES:

* Merging in Git creates a special commit that has two unique parents. A commit with two parents essentially means "I want to include all the work from this parent over here and this one over here, and the set of all their parents."

## How do I look at the latest commit on each branch?

```sh
# latest commit on each branch.
git branch -vvv -a

# latest commit on each branch 'refs/heads/'.
git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'

# latest commit on remote branches 'refs/remotes/origin'.
git for-each-ref --sort=committerdate refs/remotes/origin --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'
```

## How do I clean up my local branches?

```sh
git fetch --prune

# using git-extras
git show-unmerged-branches
git show-merged-branches
git delete-merged-branches

# show merged branches (gets confused if squash commits have been done on web).
git branch -r -vvv --merged origin/master
# and unmerged
git branch -r -vvv --no-merged origin/master

# delete a local and a remote branch (git-extras)
git delete-branch <branch>
```

## Keeping branches up-to-date

```sh
# have a look at the differences
git log -n 10 --graph --oneline master
git log -n 10 --graph --oneline <branch>
git diff master..<branch> --name-only

# rebase the branch
git rebase master
```

## Find common ancestor between branches

```sh
# get a common ancestor commitid
git merge-base mybranch master
```

## Show changes in staged

```sh
git diff --cached
```

## Changes to a branch since creation

```sh
# files changed from master in the current branch
git diff $(git merge-base $(git branch --show-current) master)..head --name-only
```

## View a file from another branch

```sh
# show a file in a branch
git show origin/branchname:path/README.md
```

## Checkout a file from another branch

Use to undo single file changes in a branch.

```sh
# Get the version of the file from origin/master
git checkout origin/master filename

# revert directory back to state of common ancestor
git checkout $(git merge-base $(git branch --show-current) master) -- <directory>

# get a file from another branch (cherry-pick a single file)
git restore --source brew -- FAQS.md
# or
git checkout -m <branchname> ./package-lock.json
```

### Cherry pick a commit into staged "Reverting a revert"

This might need to be done if you made a change that was reverted and you want to recall and work with it again.

```sh
# create new branch
git checkout -b retry-broken-code

# take the commit of the original changes.
git cherry-pick --no-commit abcde29877733c861aad625f567b7455838 -m 1
```

## Origins

### Git switch origin

This is useful after syncing a repo with `https` rather than `ssh`

```sh
git remote show origin
# add a new origin
git remote add originssh git@github.com:chrisguest75/default_dotfiles.git
# push local changes up to it to test it is configured correctly
git push originssh
git remote
# remove old origin
git remote remove origin
git remote rename originssh origin
git remote
git remote show origin
```
53 changes: 53 additions & 0 deletions 36_git/sections/CLIS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# GIT CLIS

Github and Gitlab both have cli tooling for working with PR/MR and issues, etc.

## Github CLI

### How do I work with PRs on a repo from cli?

Using the github cli command.

```sh
# turn off pager
PAGER=

# list PRs using github cli
gh pr list

# create a draft pr
gh pr create --draft

# look at changes in PR.
gh pr diff <id>

# you can edit the title of the pr
gh pr edit <id>

# promote a draft pr to ready
gh pr ready <id>

# merge to master
gh pr commit <id>
gh pr merge <id>
```

## Gitlab CLI

Use the gitlab cli command.

```sh
# list mr
glab mr list

# create an mr
glab mr create

# view gitlab in browser
glab repo view --web
```

## Resources

* Gitlab cli [glab](https://glab.readthedocs.io/en/latest/)
* [glab source](https://github.com/profclems/glab)
111 changes: 111 additions & 0 deletions 36_git/sections/COMMITS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# COMMITS

## Contents

- [COMMITS](#commits)
- [Contents](#contents)
- [Commits](#commits-1)
- [How do I know what changes were in a commit?](#how-do-i-know-what-changes-were-in-a-commit)
- [Rollback the last commit](#rollback-the-last-commit)
- [Diff between two commits](#diff-between-two-commits)
- [Get the top commitid](#get-the-top-commitid)
- [Show the staged diff](#show-the-staged-diff)
- [Show missing commits between branches](#show-missing-commits-between-branches)
- [Show commits for current directory](#show-commits-for-current-directory)

## Commits

```sh
git show head --name-only
git show master~4 --name-only
git show master^1 --name-only
```

## How do I know what changes were in a commit?

```sh
# show last 4 commits on a branch
git log -n 4 --oneline <branch>

# choose a commit and list files
git show <commitid> --name-only --oneline
```

## Rollback the last commit

Rollback in the current local branch

```sh
# pop the last commit off and move the changed files into staging
git reset head~1
```

Rollback on default branch.

```sh
# create a new branch
git checkout -b reverting

# revert commit
git revert head|commitid

# push and create an MR
git push

# or switch to main and merge the commit in
git switch main
git merge reverting
```

## Diff between two commits

```sh
# diff changes on current branch
git diff head..head~1

# diff across a branch for a specific file
git diff master brew -- ./FAQS.md

# get the diff of a branch from the base where it was created
function branch-diff() {
local ORIGIN=$1
local BRANCH=$2
local BRANCH_COMMIT=$(git log --pretty=format:"%H" -n 1 $ORIGIN$BRANCH)
local BRANCH_BASE_COMMIT=$(git merge-base $ORIGIN$BRANCH $ORIGIN$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5))
#git diff $BRANCH_BASE_COMMIT..$BRANCH_COMMIT
git difftool --tool=vscode $BRANCH_BASE_COMMIT..$BRANCH_COMMIT
}
branch-diff origin/ awsvpcs
branch-diff "" gitactivity
```

## Get the top commitid

```sh
# head on current branch
git rev-parse HEAD

# head on current branch
git rev-parse $(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)
```

## Show the staged diff

```sh
# diff head against staged
git diff --staged
```

### Show missing commits between branches

```sh
# show commits missing between branches
git-missing master feat/testing
```

## Show commits for current directory

```sh
# show commits containing entries for current directory
git log .
```
Loading

0 comments on commit 44d451a

Please sign in to comment.