Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

creating/deleting branches #9

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions create_challenge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ Flag: ShowMeMore
# For reference in commit.md later
BRANCH_COMMIT="$(git rev-parse --short @)"

echo 'A slightly older alternative to `switch` is `checkout`, which also works, but it can do destructive things if you don'\''t pay attention, so that is why `switch` is generally prefered.' >> branch.md
echo 'A slightly older alternative to `switch` is `checkout`, which also works, but it can do destructive things if you don'\''t pay attention, so that is why `switch` is generally preferred.' >> branch.md
git add branch.md
commit -m "WIP branch: add explenation on checkout"
commit -m "WIP branch: add explanation on checkout"

cat "$DOCDIR/04_branch/branch_create_delete.md" >> branch.md
git add branch.md
commit -m "WIP branch: add explanation on how to create/delete"

git switch main

Expand All @@ -58,7 +62,7 @@ echo "$STAGING_FLAG" >> README.md
echo >> README.md # newline for readability
echo "$COMMIT_DESCRIPTION" >> README.md
git add README.md
commit -m 'README: add explenation on status and diff'
commit -m 'README: add explanation on status and diff'

# tmp file, because gnused and MacOS/FreeBSD sed handle "-i" differently
# `{N;N;d:}` for deleting the following (empty) line as well
Expand All @@ -69,7 +73,7 @@ sed "/$UNSTAGED_FLAG/{N;N;d;}" README.md > tmp
sed "/$STAGING_DIFF_DESCRIPTION/{N;N;d;}" tmp > README.md
rm tmp

# hooks (should be installed last, since they are selfmutating and would be called e.g. by `git commit`)
# hooks (should be installed last, since they are self-mutating and would be called e.g. by `git commit`)
# Prepend the LocalCodeExecution flag with the deletion script to all hooks to avoid code duplication
rm .git/hooks/*

Expand Down
11 changes: 11 additions & 0 deletions src/04_branch/branch_create_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Toggling between two branches

Sometimes you find yourself switching back and forth between two branches and it becomes tedious to always type the branch names, so there is a shortcut for it: instead of a branch name, you can simply write a `-` like: `git switch -`

## How to create a branch

To create a new branch from the commit you are currently on you can simply run `git switch -c my-new-branch` (short for `--create`). You can also create one based on a different commit (not the one you currently have checked out) by adding it (in this example the last commit on the main branch): `git switch main -c my-new-branch`.

## How to delete a branch

To delete a branch you can run `git branch -d my-new-branch` (short for `--delete`). That will not work if the branch has changes that are not merged yet (not in the main branch). In that case you need to add a `--force` (or `-f` for short) as well. Alternatively there is the shortcut `-D` that is the same as `--delete --force`.
8 changes: 8 additions & 0 deletions src/hooks/post-checkout
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ PREV_HEAD="$1"
NEXT_REF="$2"
IS_SWITCH="$3"

# switched to my-new-branch
if [ "$(git rev-parse --abbrev-ref HEAD)" = "my-new-branch" ] && [ "$IS_SWITCH" = 1 ]; then
echo "Flag: MyFirstBranch"
exit
fi

# switched to branches-explained
if [ "$NEXT_REF" = "$(git rev-parse branches-explained)" ] && [ "$IS_SWITCH" = 1 ]; then
echo "Flag: Switcheridoo"
exit
fi
3 changes: 3 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ exec $(sed -n '/^```sh$/,/^```$/{n;p;}' commit.md) | grep --quiet "Flag: ShowMeM
echo 'Switcheridoo when switching to "branches-explained"'
git switch branches-explained 2>&1 | grep --quiet "Flag: Switcheridoo"

echo 'MyFirstBranch when creating'
git switch -c my-new-branch 2>&1 | grep --quiet "Flag: MyFirstBranch"

echo success!