Skip to content

Commit

Permalink
creating/deleting branches
Browse files Browse the repository at this point in the history
  • Loading branch information
miallo committed Jan 2, 2024
1 parent 484b60a commit ed8eee7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions create_challenge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ echo 'A slightly older alternative to `switch` is `checkout`, which also works,
git add branch.md
commit -m "WIP branch: add explenation on checkout"

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

git switch main

# commit
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!

0 comments on commit ed8eee7

Please sign in to comment.