Saturday 26 September 2015

Part 5 - git skim : Syncing (remote, fetch, pull, push)

git remote:

git gives every developer their own copy of the repository, complete with its own local history and branch structure. You can make connections to these remote repositories using their URLs. However, URLs are not so convenient and easy to use. You can keep some easy to remember alias for these connections using git-remote command.
git remote lets you create, view and delete connections to other repositories.

List the connections you have to other repositories
git remote
List the connections along with their URLs
git remote -v
Create a new remote connection. You can refer to this connection using his name later.
git remote add <name> <URL>
Rename a connection
git remote rename <old-name> <new-name>
Remove a connection
git remote rm <name>

NOTE: When you clone a repository, a remote connection named "origin" is created automatically to point back to that remote repository.

git fetch:

git fetch command downloads the commits from remote repository into your local repo. Fetched contents are represented as remote branches. So, it absolutely does not have any impact on your local work.

Fetch all of the branches from remote
git fetch <remote>
Fetch specific branch from remote
git fetch <remote> <branch>

Following is example for synchronizing your local repository with the central repository's master branch.git checkout master
git log --oneline -n 10
git fetch origin
git log --oneline master..origin/master # To see new commits came in origin/master
git merge origin/master   # Merge origin/master changes to master

git pull:

Syncing your local repository with remote repository is quite a frequent task. As seen earlier, you can do this with "fetch" followed by "merge" command. 
However, "git pull" does the same thing in single command.
pull=fetch+merge.

Suppose you are on master branch and you want to sync with origin's master.
git pull origin
# git fetch origin master
# git merge origin/master 

git pull --rebase:

git pull uses "merge" for merging. If you want to maintain linear history of project, you should use rebase rather than merge. "git pull --rebase" uses the "git rebase" for merging the changes.
pull-rebase=fetch+rebase
git pull --rebase origin
# git fetch origin master
# git rebase origin/master

No comments:

Post a Comment