0

https://wiki.postgresql.org/wiki/Working_with_Git#Using_git_hooks
I want to quickly test my idea (fail fast).
So whatever I changed, committed to the local branch. then I switch to a new branch using

git checkout -b test1

I can quickly get local as latest as remote (master branch) then I do new changes.

How to automate this using git hooks.


https://postgresql.life/post/andrey_borodin/

I think what I do is an antipattern. I have at $HOME directories postgres0, postgres1, postgres2…postgresE, postgresF, postgres10… Whenever I do not understand what I was changing in postgresX - I do git clone https://github.com/postgres/postgres postgresX+1.

Yes, I know git was invented for a reason. But to give a name to the branch I need at least a subtle understanding of what was the purpose of changes.

Basically, when I create a new branch, I can get the latest remote automatically. But before I switch if the old branch doesn't commit, the old branch will be lost? (so I need to prevent this).


I want to make sure every time I use "git checkout -b", the new branch's local content is as fresh as the latest remote. (That means later when I make some change or break something, it's all my fault) also, the previous branch's changes are still there. (I want to automate this).

7
  • Sorry, I don't understand the question. Are you asking "How can I be sure that when I change to a particular branch I can be sure that it is synced with the upstream master?" with the hint that you want to use git hooks to do it? If so I suggest you look into git aliases. In general this is not a good idea as you may not be able to reach the machine holding the upstream copy. git hooks are more designed for when you are making changes to the repo, not the working directory.
    – icarus
    Jul 7 at 20:25
  • @icarus I updated my question.
    – jian
    Jul 7 at 23:59
  • Thank you for the update. However I have read it and I still don't understand, so let me try asking again. I am getting a little more confident that "git hooks" are not what you are looking for. Are you doing as the post from Andrey is doing where he has a lot of different directories, each with a git clone in them or can I ignore that? I think the important thing for you to know is that git checkout -b **will not delete anything you have written unless you give it a -f or --force ** option. Also branches are not deleted automatically.
    – icarus
    Jul 8 at 1:15
  • but git checkout -b is not the latest remote content?
    – jian
    Jul 8 at 1:18
  • @icarus I updated my question......
    – jian
    Jul 8 at 1:28

1 Answer 1

1

Following a discussion in chat we clarified what the OP wanted.

Suppose the upstream had commits R1, R2, and R3 on it. The local branch1 had these (R1, R2, R3) and some local commits L1 and L2.

Another commit is made to the remote (R4).

Then a new branch is created locally (branch2) and selected. The question is how to use git hooks so that branch2 is up to date with respect to the remote, in effect to do a git fetch automatically.

This is not something that is suitable for git hooks. One can argue that it is not suitable for git aliases either, as it is not known if the remote is accessible at the time the branch is being created.

Anyway the desired sequence of commands is (assuming the remote is called the traditional origin

# get the changes from origin/master
git fetch origin
# If you want a linear history, with the local changes, create the
# new branch
git switch -c branch2
git rebase origin/master

If there are edit conflicts between L1+L2 and R4 they will be reported in the rebase and need to be fixed.

This will leave branch1 as R1-R2-R3-L1-L2 and branch2 as R1-R2-R3-R4-L1'-L2'.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .