Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git alias to pull --rebase current branch

Tags:

git

git-alias

I am working on a branch called foo and I want to rebase my branch against origin/foo. In modern versions of git I could run

$ git pull --rebase origin foo

That's a lot of typing for a common operation. Additionally, because I have an old version of git (see this), I also need to run a fetch first so that my local graph doesn't seem confusing:

$ git fetch && git pull --rebase origin foo

How can I write that as a git alias?

like image 997
Barry Avatar asked Mar 23 '15 17:03

Barry


People also ask

How do I rebase a branch with a current branch?

Pull into Current Using Rebase (for remote branches) to fetch changes from the selected branch and rebase the current branch on top of these changes. Checkout and Rebase onto Current (for both remote and local branches) to check out the selected branch and rebase it on top of the branch that is currently checked out.

How do I rebase my current branch with a master?

To rebase, make sure you have all the commits you want in the rebase in your master branch. Check out the branch you want to rebase and type git rebase master (where master is the branch you want to rebase on).

What does rebase current branch do?

The Rebase Option This moves the entire feature branch to begin on the tip of the main branch, effectively incorporating all of the new commits in main . But, instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch.


1 Answers

The command

git symbolic-ref --short HEAD

outputs the short name of the current branch, if any (or HEAD, if the HEAD is detached). Therefore, you can use it inside a command substitution in your alias definition:

git config --global alias.<name> '!git fetch && git pull --rebase origin $(git symbolic-ref --short HEAD)'

where <name> stands for your alias's name.

like image 54
jub0bs Avatar answered Sep 20 '22 14:09

jub0bs