Tag Archives: git

How To Upload Your Project to Github

Warning
Using free Github account will cause your project to be visible publicly

Often you’re solving a very tricky programming problem and it’s hard to describe it on forum. One very useful thing to do is to isolate the problem by creating a small project and share it. This can be done easily using git and .

  1. Install git if your PC/Mac doesn’t come with one. Use git --version on a terminal shell / command prompt to check.
  2. if you haven’t got one
  3. To avoid having to type in password each time you upload / sync code to github, let’s setup public/private key authentication. This only need to be setup once per PC. First check if you already have a ssh keypair. On windows: open explorer > right click > git bash (or just open a terminal shell on OSX / Linux) > check if you have the file ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub. If you don’t, generate a new one using ssh-keygen (accept all the default setting)
  4. On github . Paste the content of ~/.ssh/id_rsa.pub.
  5. Create new github repository. Give it your project name (eg: foobar).
    github
  6. Back in your computer, point your git shell to the root of your project and run these commands:
    $ git --init
    # You may want to create a .gitignore file before proceeding to
    # next command, see below
    $ git add -A
    $ git commit -m 'initial commit'
    # Change mygitusername and foobar
    $ git remote add origin :mygitusername/foobar.git
    $ git push -u origin master
    

    It’s a good practice to add a .gitignore to avoid tracking binaries and project metadata. Do this before git add -A. This is my typical .gitignore file for a maven java project:

    # All files paths matching these prefix will not be tracked by git
    target
    .settings
    .project
    .classpath
    .springBeans
    
  7. Congratz, you project is now up in github. It’s also a good practice to add a README.md file to include instruction on how to run your project. Run following commands for subsequent changes you want to push to github:
    $ git add -A
    $ git commit -m 'fixed bugs abcd'
    $ git push
    

Git Cheat Sheet

Deleting Remote Commits

Assuming the remote repository has been cloned locally and the remote repository name is origin

  1. Delete all local modification using git reset -- ...
  2. Delete the latest commit locally using git reset HEAD^ --hard
  3. Synchronize it to remote using git push origin -f

Thanks To

  • http://christoph.ruegg.name/blog/git-howto-revert-a-commit-already-pushed-to-a-remote-reposit.html

Merging Git Conflicts On Github

This article applies to following environments (but conceptually it can be applied to any environment):

  • Github
  • Windows 7

If you’ve been relying on the github GUI, you may get (very) annoyed when the conflict screen appear (Failed to sync this branch due to unmerged files)

gitmerge

This is few (often not so) simple steps to resolve the conflicts:

  1. Click OPEN SHELL, a windows powershell will come up
  2. Start by running git status, this command will help you as you progress resolving the conflict. It also contain pretty clear instruction on how to mark a file resolved, and how to finish conflict resolution
  3. In this examply (luckily) I only have 1 file in conflict hello.txt. Open it using a text editor, and edit the conflict. Conflicts are presented in a unified diff format:
    hello world
    <<<<<<< HEAD
    mary
    =======
    bobby
    >>>>>>> bobby
    the many
    testing
    file
    

    Above example is saying you changed the 2nd line to “mary”, but your colleague changed it to “bobby”. You now have to decide either (or merge both).

  4. For every conflict you resolved, you can tell git so by git add command, eg: git add hello.txt
  5. Once all conflicts are resolved do git rebase –continue command (if you’re in rebase mode — sometime github will use other conflict resolution and will tell you what command to run when completed)