diff --git a/git.md b/git.md index 810ab9ef7420c2ac04f35b493dd81e5a920e06b3..ef36876f8e77e7113610f224a7cd23169b814595 100644 --- a/git.md +++ b/git.md @@ -323,6 +323,26 @@ git checkout -b <new branch name> git checkout -b <new branch name> <some other exisiting branch> ``` +#### Tracking Branches + +Lets say you are working on a branch named `dev` which you fetched from the remote named `origin`. Now you want to work on this branch for a quite a long time and want the `git push` and `git pull` using the `dev` branch of `origin` by default. So that you won't have to type `git push origin dev` or `git pull origin dev` every time you want to update or pull the updates to and from the remote branch. + +First, you need to tell Git which branch to track: + +```c +# suppose you are on the dev branch on you local machine, and there is already a dev branch in the remote repo +git branch --set-upstream-to origin/remote +``` + +This only works when there is already a `dev` branch in the remote repo. If you want to create a new branch in the remote repo, and track it automatically, then you can use the following command: + +``` +# this will create a dev branch in the remote and track it using the current branch locally +git push --set-upstream origin dev +# or, same as above +git push -u origin dev +``` + #### Merging Branches Merging in Git is the way to putting forked history back together again. The `git merge` command lets you integrate branches created by `git branch` into a single branch.