My workflow without version control GUI tools (like SourceTree, for example) is like this. I type it in command line. I have a local repo on my local machine. And I have a server (AWS EC2 instance) with a copy of my local repo. And I use Bitbucket for my remote repo. The first part (local, see code below) I do in my local machine. Then I connect my EC2 server using SSH and a command line and type the the second part, git fetch
, get merge
:
# On local repo:
$ git add .
$ git commit -m "Commit message"
$ get push origin foo-branch
# On remote repo
$ git fetch origin
$ git merge origin/foo-branch
For the first part I have GUI tools like SourceTree. But for the second part I still have to use command line to fetch and apply changes on my server.
My question is, is there some tools that allow to automate the process. So that it would be unnecessary to connect the server with SSH in command line and type git fetch
and git merge
.
For example, I just push a commit on my local machine (using SourceTree, for example) and my server automatically detects it and fetches and merges changes.
You can Bitbucket webhooks to trigger your server commands based on several different repo events (Push for example). Just create a web page that runs a script that performs the Git steps on your server when the page URL is hit.
Add a webhook to the Bitbucket repo to hit your web page when the desired event occurs.
See Bitbucket Cloud docs on webhooks here https://confluence.atlassian.com/bitbucket/manage-webhooks-735643732.html. Bitbucket server is similar.
No, remote git repo can’t execute merge operation since remote repo is a bare repo which has no working directory. So the commands like git fetch
, git merge
can’t be used for remote repo.
The work around you can use a pre-push hook in your local repo: before you push foo-branch
to remote, the pre-push hook will be triggerd, then you can update your master
branch and merge foo-branch
into master
. The script for pre-push hook as below:
#!/bin/sh
git checkout master
git pull origin master
git merge foo-branch
git checkout foo-branch
Note:
- If merging
foo-branch
into master
branch is not fast-forward merge, or there may has conflicts when merging, you should use -X
option to resolve merge conflicts automatically. Such as you can use git merge foo-branch -X theirs
to instead.
- If you also want to push the merge commit from local
master
branch to remote master
branch, you should use git push --all
to instead git push origin foo-branch
.
BTW: Even you use webhook to trigger a script when changes pushed to remote repo, you still can't execute git fetch and git merge from remote repo.