1

I generally use these commands on branch 'work' with git

git commit -a -m "blah! blah!"
git checkout master
git merge work
git checkout work

I have heard about git aliases. Is it possible to combine all these commands into one via aliases or something else.

1

3 Answers 3

0

As well as the answers to the duplicate question posted in the comments, you can also put an executable shell script called git-foo (for example) somewhere in your PATH and access it with git foo <args> as though it were a native Git command.

This could be useful if the logic for your alias is too lengthy for a single line in your config file.

For example:

$ cat > ~/bin/git-foo
#!/bin/bash
echo "Foo: $1"
^C
$ chmod +x ~/bin/git-foo
$ git foo test
Foo: test
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I am a windows user and new to git. So could you please tell me what your example is doing
0

As @Will-Vousden suggests, it would be possible to write a shell script to do this. For example:

#!/bin/bash
git commit -a -m "$1"
git checkout master
git merge work
git checkout work

which you would call like:

$ git-commit.sh "Commit message in quotes"

However, this approach (or your own alias) could easily fail if there is, for example, a merge conflict:

$ git-commit.sh "Commit message in quotes"
[work 1a6e17f] Commit message in quotes
 1 file changed, 1 insertion(+), 1 deletion(-)
Switched to branch 'master'
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.
file.txt: needs merge
error: you need to resolve your current index first

leaving you in the master branch with an unresolved merge conflict.

You could probably add some logic into the script to detect -- and potentially work round -- such issues, but that sounds like much more work and effort than just running the four separate commands!

Comments

0

You could also add the following to your shell.rc file:

gmm merges all given branches with the one you are currently on

invoke: gmm branch1-to-be-merged branch2-to-be-merged ...

gmm () {
        src=`git branch --no-color | grep "*" | cut -f 2`
        for i in $@
        do
                git checkout $i
                git merge $src
        done
        git checkout $src
}

Comments