Skip to content

Instantly share code, notes, and snippets.

@innyso
Last active April 26, 2020 11:10
Show Gist options
  • Save innyso/562ab42aac783100b9545185948b9c08 to your computer and use it in GitHub Desktop.
Save innyso/562ab42aac783100b9545185948b9c08 to your computer and use it in GitHub Desktop.
#git #gitconfig #alias

It had been a habit of mine where I always do git pull/push origin [branchname] for years because I push to master by accident once years. This week, I decided its time to up my git game and did some research on how to make the current checkout branch as the default upstream to push or pull.

To push to current branch

This one is simple, just set global config push.default to current

git config --global push.default current 

# after that I can simply push to my current checkout branch without type origin branchname
git push

To pull from current branch

This one is not as simple but would be great if I can find another solution.

# creating a new branch, make sure we track it too
git checkout -t -b my_branch

# switching to an existing branch, make sure we set the checkout branch to our upstream too
git checkout my_branch && git branch -u origin/my_branch

# and now we can pull without origin my_branch
git pull

Adding the above to my default gitconfig

[push]
	default = current
[alias]
  co = "!f() { git checkout $1 && git branch -u origin/$1; }; f"

References

https://stackoverflow.com/questions/14031970/git-push-current-branch-shortcut

https://gist.github.com/johnpolacek/69604a1f6861129ef088

https://jondavidjohn.com/git-aliases-parameters/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment