Home > database >  How to git pull with custom commit message?
How to git pull with custom commit message?

Time:07-28

I'm in a big problem. I was in a new working environment and working with a team. They are pushing straight to the master branch for new features while I used to work in a separate branch myself, on my private repositories. (I thought that this development method was best practice, never actually worked with a team).

I once developed a feature and pulled the master branch to my feature-x then push it to my my branch but it triggers the the deployment and replaced the production app with one from my latest git pull commit.

I reported it to my seniors and they say it's fine because it's my first day joining them. But they say I should be careful when merging stuffs.

After I check the CI script, it turns out that the deployment script will be executed if there is a word Merge and --deploy in commit message on ANY branch. So I need to change my merge commit message produced by git pull to another message.. Is it possible?

And yes, I know that git pull is a shorthand for git fetch and git mergeI read from another similar question on SO but I just can't grasp that fast by reading the docs because I'm not a native English speaker. The deadline is near* and I'm panicked.

I'm afraid if I do git fetch and git merge manually, I'll messed up merging my unfinished branch to the master branch instead of merging master to my branch.

Please help! What is the safest command to fetch and merge with custom message?

*I have committed all my work and just need to pull from master before pushing to my branch and make a pull request.

CodePudding user response:

So I need to change my merge commit message produced by git pull to another message.. Is it possible?

Yes. After the pull, if a merge commit was created, say

git commit --amend

You will be asked for a replacement commit message.

However, you should have done this together with the pull, by saying

git pull --edit

And yes, I know that git pull is a shorthand for git fetch and git mergeI read from another similar question on SO but I just can't grasp that

Grasp it. You never need to say pull; it is confusing and dangerous. To merge master into the branch you are on, say

git fetch
git merge origin/master 

Again, to avoid having to rewrite the commit message later, say

git fetch
git merge --edit origin/master 

You asked: "What is the safest command to fetch and merge with custom message?" That.

(It is also possible to configure your Git so that a merge commit always offers you the chance to edit its message. I prefer it that way. Recent Git versions are set up this way by default; you might be using a somewhat outdated Git, else the problem would never have arisen.)

CodePudding user response:

The easiest would be to do git pull as usual, then run :

git commit --amend -m"New commit message"

This will rewrite the last commit message that was commited.

Be aware that you should not use this if you already pushed that particular commit, because this will then need you to do git push --force which is usually bad practice.

Note that you can create a new branch for your current changes before doing the merge with master.

  • Related