Using Git Hub to work from home and office

I often work both from home and office on certain proto-type projects and always have to constantly upload and download my code to and from Google Drive. So now I started to use Git Hub for these types of projects and find it a mush better solution.

So I thought I will note down the basic steps involved in doing things.


Creating a new repository

I think there are a number of ways to do this. But this is the way I do it and its more easier to get your head around it.

a) Create a new repo on github.com. I give it the same name as my solution name
    This creates a repo with a default branch name of Master
b) Open git bash and cd to the local solutions folder
c) Create the local repo:
    git init
    This creates a local repo with a default branch name of Master
d) Locally setup the user name and email of your github.com account. This need to be done only once for a git install:
    git config --global user.email "useremail@server.com"
    git config --global user.name "useremail"
    git config --global user.email or name (without parameter to check already setup email or user name)
e) Create a .gitignore file and put in it folders and files you don't want to be tracked
     e.g to ignore the bin folder, just put bin/. It will ignore this no matter where its located in the solutions folder
f) Stage all the changes that needs to be committed. The first time this will be everything bar the files in the .gitignore
   git add --all
g) Commit the staged changes
    git commit -m "change history comments"
h) Connect local repo to remote repo. This needs to be done only once for a new repo:
    git remote add origin <https clone url>
    The url can be copied from github.com under the newly created repo
i) Push committed changes to remote repo
    git push origin master

Cloning a repo

Once a new repo is created on github.com and locally, it can be cloned at another site like home or office.
a) cd to the local development folder where you want to put the clone
b) Create a local repo if not already created
     git init
c) Clone the remote repo:
    git clone <https clone url>
    The url can be copied from github.com under the newly created repo
d) Connect local repo to remote repo if not already done:
    git remote add origin <https clone url>
e) Fetch files from remote master branch  to the local clone master branch:
    git fetch origin master
f) Merge fetched files from remote master branch with local active branch
    git merge origin/master
    The local active branch by default is Master

Merging the repo at office with the one cloned at home via github.com

After creating a repo and cloning it at another site i.e home or office, you can work at one site (say office),  push changes to github.com remote repo, then fetch latest changes down to the second site (when you go home)
a) At office after doing some work, check what has been changed:
    git status
    This will list all the files from active local branch (Master by default) that has been changed
b) stage the changes and commit:
    git add -all
    git commit -m "change history comments"
c) Push changes from local active branch to github.com remote repo master branch:
    git push origin master
d) At home fetch files from github.com remote master branch:
    git fetch origin master
e) At this point you can check what are the differences between the local and remote repos:
    git diff master origin/master
    This will list the line by line differences between the two repos
f) Finally merge the local active branch with the remote repo master branch:
    git merge origin/master

Other useful command

* To undo local changes before staging:
    git checkout -- .
* To undo all local changes including any untracked files:
    git stash -u
    you can redo them back with 
    git stash pop
* To undo local changes after staging
    git reset --hard
    If you also want to undo any untracked files then run 
    git clean -f
* To force push and overwrite github with local changes
    git push origin master --force
* To force fetch from github and overwrite local with remote changes
    git fetch origin master
    git reset --hard fetch_head
    (git clean -df) only if you want to remove untracked files from remote fetch
* To copy file from one branch to another. From the branch you want the file to land, run:
    git checkout otherbranch myfile.txt
* To stash and apply stash
    git stash
    git stash apply
* To list stashes and clear all stashes
   git stash list
   git stash clear


0 comments:

Post a Comment