Git - prepare for Merge
Yes, I use git.
And, I enjoy working with branches and pull requests.
But every now and then I encounter this:
When this happens, you need to merge master into your branch.
If the merge is simple, with minimal conflicts, it can be done in github’s UI. But if not, you better do it on your dev environment.
I found myself doing the same things:
- update feature branch
- switch to master
- update master
- switch back to feature branch
Only after I’m sure that my local feature and master branch are updated, I can
Lazy is good!
I’m a lazy developer, and it’s a good thing.
I want to add a git command to do all the sh** for me.
Add your personal Git command
For that you’ll need:
- Write you command.
- Place it in PATH
Let’s create a bash file named
#!/bin/bash current_branch=$(git branch | grep \* | cut -d ' ' -f2) git pull git checkout master git pull git checkout $current_branch
git- prefix in filename is important.
2. Place in path
Place the file in a directory that is already in your
I put it in
/usr/local/bin on my Mac.
An alternate option will be to place it in a dedicated folder (i.e.
~/my-git), and add this folder to bash path.
To so, add
To enable execution of that file.
Now, before each merge, I just run:
After I merged a lot, I have to many useless local branches..
#!/bin/bash echo "purging merged branches..." git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d
This script lists al local branches that were merged already,
Then it gives them as a param to
git branch -d, which stands for
Now, on updated master, run: