@jpmens not aware offhand. one trick I've seen some folks do is to have a "dev" branch hung off master, and by habit they keep that branch in their foreground. so if they commit/push without thinking it goes in dev, keeping master clean
@jpmens You could alias vim to run a script and if the branch is called "dev" or "main" it could ask if that's what you want to do?
@jpmens with great power comes great responsibility …
@jpmens Before committing, you can still do "git checkout-b dev".
I'm not sure you can do it before *editing*, but you can do it before checking in,
$ ed file.c
⋮
$ git checkout -b new_branch
$ git add file.c
$ git commit -m "Edit file.c"
or if you've already committed to a branch you didn't intend to:
$ ed file.c
$ git commit -am "Edit file.c a bit"
$ ed file.c
$ git commit -am "Edit file.c some more" # whoops, we've been committing on wrong branch
you can drop the new branch at the current progress:
$ git branch new_branch
then (as long as you haven't pushed those changes anywhere) reset the wrong branch back to its upstream:
$ git reset --hard @{u}
and then checkout the new branch to keep working there:
$ git checkout new_branch
@jpmens probably easiest¹ with a pre-commit hook², but idk if you can configure them globally without a tool like "pre-commit" or similar.
¹: to verify if the current branch is not the default branch of the repo
²: not what you asked for, but git doesn't know where and how you edit files, only how you commit them
@jpmens if you created the branch afterwards, it will also have the change, or am I wrong?
@jpmens @bentsukun yes I think that's true
@jpmens Been there 😅 I have not tried it but you could add a global hook that prevents you from committing to all master/main branches.
$ git config --global init.templatedir '~/.git_templates'
$ mkdir ~/.git_templates/hooks
Then in ~/.git_templates/hooks create file 'pre-commit' (0755) with something like (this is a ruby example I found): https://gist.github.com/Fedalto/5257702
Hook present and assuming you edited a file in the main/master branch:
$ git switch -c mybranch
$ git commit -a -m"my change"
@jpmens
PS1=$(git reset —hard upstream/master)
I tend to just configure my git remotes to not accept pushes to the main branch, only merges. After that it's just a matter of simple git history editing.
- replies
- 0
- announces
- 0
- likes
- 0