0%

Git basic

How to start

  • Learn how to clone others repository
  • Learn how to create your own repository
  • Learn how to commit your code to git server
  • Learn how to development
  • Learn how to code review
  • Learn more things

Clone others repository

To download from a HTTPS url

1
$ git clone https://github.com/pcjustin/pcjustin.github.io.git

To download from a SSH url

1
2
$ git clone
git@github.com:pcjustin/pcjustin.github.io.git

Create your own repository

To create a empty repository

1
$ git init new-repository

To add your code for commiting

1
$ git add main.c

To commit your code to local

1
$ git commit -m "initial commit"

Commit your code to git server

To set up remote server

1
$ git remote add origin https://github.com/pcjustin/pcjustin.github.io.git

To push your code

1
$ git push -u origin master

Using Git to development

Please following the Golden rules:

  • Don’t develop in master branch.
  • Don’t develop in develepment branch.
  • Create a sandbox or something branch for developemnt.
  • Combine multipe commits when merging back to development branch.
  • Rebase is good
  • That’s hack it.

Create a branch

1
2
3
$ git checkout dev
$ git checkout -b sandbox/pcjustin/add_hello_world
$ git push -u origin sandbox/pcjustin/add_hello_world

Rebase your multiple commits

HEAD~3 means how many commits do you want to combine

1
$ git rebase -i HEAD~3

Merge your code into developement branch

1
2
3
$ git checkout dev
$ git merge sandbox/pcjustin/add_hello_world
$ git push -u origin dev

Update your code from development branch

Sometimes you need to solve some conflicts.

1
2
$ git checkout sandbox/pcjustin/add_hello_world
$ git rebase dev

Code review

How to code review

Many services support Git for code review. I prefer Gerrit to code reivew that is stable version.

1
2
3
$ git checkout dev
$ git merge sandbox/pcjustin/add_hello_world
$ git push origin HEAD:refs/for/dev

How to modify my review when modify the code

1
2
3
$ git add main.c
$ git commit --amend
$ git push origin HEAD:refs/for/dev

How to update my review when merge conflicts

You need to solve local conflicts.

1
2
3
4
$ git checkout dev
$ git pull origin dev
$ git rebase origin/dev
$ git push origin HEAD:refs/for/dev

Download code review from Gerrit

1
$ git-review -d <change id>

One more thing

How to clean the remote repositories

1
$ git remote prune origin

To be continued