DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Work on Remote Git Repository


Create New Branch

$ git clone https://github.com/hackrole/pyevernote.git
$ cd pyevernote
$ git branch    // list all existing branches
$ git checkout -b leetschau     // create new branch "leetschau" and checkout it

Push to remote repository

$ git push origin leetschau, where "origin" is repository name, "leetschau" is branch name.

When you push your branch the first time, add "-u" to specify that the current branch is associated with the remote branch.

Ref: http://stackoverflow.com/questions/6089294/why-do-i-need-to-do-set-upstream-all-the-time.

Push all branches to remote repository

$ git push origin --all.

Sync with remote repository

If there's no local changes, the first command is unnecessary.

git checkout -- .
git pull --all

Discard local changes

Discard all local changes and keep the same with last commit in repo.

git reset --hard HEAD

We need discard all local changes and keep the same with remote repository. Provided that you want sync with "master" branch in remote repository "origin":

$ git fetch orgin
$ git reset --hard origin/master

Note that files not in stage area (not been "add") will remains, while files in stage area or committed will be removed. If your remote repo is developed with git flow, your synchronized repo also need install git flow. On CentOS, install it with yum install gitflow. See installation doc on gitflow for details.

Remove Existing Branch

$ git push origin :leetchao

Create, query and remove remote repository alias

$ git remote add pyevernote-repo https://github.com/hackrole/pyevernote.git
$ git remote -v
$ git remote rm pyevernote-repo

Checkout from a existing repository and branch

$ git clone https://github.com/hackrole/pyevernote.git
$ cd pyevernote
$ git branch -r               // list all local and remote branches
  origin/leetschau
  ...
$ git checkout leetschau      // or "git checkout -b leetschau origin/leetschau" for old version git

For verification, you can find 'remote=origin ... under [branch "leetschau"]' in file .git/config.

Update from remote repository

$ git fetch origin
$ git status
  Your branch is **behind** 'origin/develop' by 3 commits... (which means your local branch need to be updated with remote one)
$ git merge origin/develop

Or simply git pull origin develop, where develop is the branch name we want to sync.

Note: Be careful to use "git pull" for "pull" is too "smart". git: fetch and merge, don’t pull gives the reason and provides an excellent explanation of what is "branch".

Move a git repository from Bitbucket to Github

Based on Moving Repository from Bitbucket to GitHub.

cd $DSNOTE_HOME
git remote rename origin bitbucket
// create a new repository "dsnote" on Github
git remote add origin https://github.com/leetschau/dsnote.git
git push -u origin master
git remote -v
git branch -r

Other

Always use git status to get current status, such as the difference between CWD and stage area, difference between stage area and local repository, and difference between local repository and remote repository. Use git log to show commit history.



Published

Nov 19, 2013

Last Updated

Nov 19, 2013

Category

Tech

Tags

  • git 36
  • github 6
  • remote 5

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor