OVERVIEW
When you create a REMOTE server, an 'ORIGIN' is created, along with the REMOTE, on your local machine that always tries to stay in sync with the REMOTE.
So, when you PUSH to the REMOTE, the origin will see the changes and sync up with it.
If there are changes made on the REMOTE server, say from a collaborator, you will need to FETCH those changes down to the ORIGIN.
Then you need to PULL those changes from the ORIGIN to your MASTER branch
LIST ALL REMOTES
>git remote (or >git remote -v //to see the url, or >cat .git/config)
To remove a remote: >git remote rm origin
SHOW REMOTE BRANCHES
>git branch -r (>git branch -a //shows local and remote branches)
CREATE NEW REPO ON GITHUB
Then paste the 2 commands from the github page into your terminal to run commands to create the remote connection and then PUSH up the local repo to github.com
1. >git remote add origin https://github.com/markberning/resume.git
2. >git push -u origin master
-u sets the branch to track with the remote.
If you forget the -u you can reset it to start tracking by:
>git branch --set-upstream branchname origin/branchname
PUSHING CHANGES TO REMOTE
>git push //since it is a tracking branch we do not need >git push origin master
PUSH BRANCH TO REMOTE
>git push origin nameofbranch //short for >git push origin nameofbranch:nameofbranch (localbranchname:remotebranchname)
COMPARE LOCAL BRANCH WITH REMOTE BRANCH
>git diff origin/master master
FETCH CHANGES FROM REMOTE DOWN TO LOCAL
Doing a >git branch -r will not show new remote branches until after you do a FETCH first!! That is because it is just looking at the origin which is stored locally and the origin doesn't know about new stuff on the remote until you do a FETCH first. FETCH synchronizes ORIGIN with the REMOTE.
>git fetch //>git branch -r will now know about any new branches from the remote
MERGING IN FETCHED CHANGES
First look at diff b/w origin/master and master
>git diff origin/master..master
Then merge
>git merge origin/master
PULL: FETCH PLUS MERGE
>git pull = >git fetch then >git merge
CHECKOUT REMOTE BRANCHES
Normally you create a new local branch by:
>git branch nameofbranch OR
>git checkout -b nameofbranch to create a new branch AND switch to it
To checkout a remote branch:
>git branch nameofbranch origin/nameofbranch OR
>git branch -b nameofbranch origin/nameofbranch
PUSHING TO REMOTE THAT HAS BENN UPDATED MY OTHER PEOPLE
So you try to PUSH to your remote in which you are collaborating on with others and git tells you that it cannot because the remote has indeed been updated since you last tried to PUSH. Git will NEVER merge during a PUSH if the remote has been updated by others. It will tell you to FETCH first so you can see the new changes and then you work out the MERGE.
So,
>git fetch
then
>git merge origin/master //this will merge my changes into the origin master, which is still local like always. Then you can PUSH again. The remote will now accept this merge. If there were conflicts, you would resolve as you do a normal merge. You have to do it this way even if there are no conflicts which is usually the case, assuming you and your collaborators work on different lines of code.
DELETE A REMOTE BRANCH - use : or use --delete
>git push origin :nameofbranch //puts blank:nameofbranch in place of nameoflocalbranch:nameofremotebranch, and the blank means it gets replaced with an empty branch = deleted)
Easier way:
>git push origin --delete nameofbranch