Skip to content

Lecture 03 About git

Tomáš Hrabal edited this page Oct 7, 2019 · 5 revisions

Git & GitHub

READ HELP FOR EVERY COMMAND WITH -h

Theory

Internet

Git is:

Advantages

  • command line
  • local repository

Disadvantages

  • command line
  • local repository

Why do i use git ?

  • because of the company where I've been working :)
  • local repository
  • simple merging
  • rebasing
  • command line and quality graphic tools at the same time
  • be strong and don't use graphic tools!! (with the exception of commits history and merge tools)

Creating projets

$ mkdir git_test

$ cd git_test

$ git status

$ git init / git clone

Adds files

$ nano test.rb

$ git status - we can see untracked and tracked files

$ git add test.rb

$ git add . # dot '.' means everything

Commit and configuration

http://git-scm.com/book/en/Customizing-Git-Git-Configuration

$ cat ~/.gitconfig

$ git config

$ git config --global user.name 'Your Name'

$ git config --global user.email you@somedomain.com

$ cat ~/.gitconfig

$ git commit -a


[ADD] Initial commit

added file app.rb


$ git log --oneline

$ git blame app.rb

$ git diff

Git does't numbered each commit as a Subversion, otherwise it creates unique hash. Only six signs from it should be enough to clearly recognize commit in the frame of repository. Each commit, except the first one, has references to his parents ‘^’.

Graphic tool for Unix https://wiki.gnome.org/Gitg

Modification repository

One of the biggest Git advantage is the possibility to take changes back in the case of mistake.

$ git status

$ git log --oneline

$ git reset HEAD^ # mean reset to parent of newest commit

$ git status

$ git log --oneline

  • mixed (i don't use)
  • soft (default)
  • hard (pottencialy dangerous)

READ HELP FOR EVERY COMMAND WITH -h $ git reset -h

We add new file into and then take it out again

$ git add test.rb $ git reset test.rb $ git rm test.rb

There is a lot of posibilities how we can take the commit back. Use Internet!

Now the question for you. What is the difference between:

$ git reset HEAD^

$ git reset HEAD^^

$ git reset ...some_commit_hash...

$ git reset ...some_commit_hash...^

$ git checkout test.rb # discard changes on some file

Git pocket

$ git stash

$ git stash apply

Branching and merging

Git help us with creation of our own development branches and merging them. It’s all on us. (merge, rebase, cherry-pick)

Special remote: origin Special branch: master

$ git branch

$ git checkout -b devel

$ git branch

Make some changes on app.rb.

$ git add README

$ git commit -m ‘[ADD] Readme’

$ ls -la

$ git checkout master

$ ls -la

During merging can this situation show up: Conflicts on the same line of code, which git cannot deal with.

$ git checkout devel

$ echo 'a' > README

$ git add .

$ git commit -m '[MOD] Readme'

$ git checkout master

$ echo 'b' > README

$ git add .

$ git commit -m '[MOD] Readme'

$ git merge devel # ooups!!

I would like to recommend you to use graphic tools for conflicts resolving. http://meldmerge.org/ http://git-scm.com/book/en/Customizing-Git-Git-Configuration

Sharing

$ git remote

$ git remote add origin ……….

$ git remote

$ git push -u origin master

Cloning

$ git clone …... $ git remote set-url origin ……….

Conflicts on remote

Download and merge from origin master

$ git pull origin master

Push changes to origin master

$ git push -u origin master

Our hero git reflog

Allows to show every chagnes, even reset. Default expiration time for saving changes is 90 days.

$ git reflog show $ git reset commit

Interactive rebasing -

$ git checkout -b feature

$ echo 'a' > README

$ git commit -a ...

$ echo 'a' > README

$ git commit -a ...

$ echo 'a' > README

$ git commit -a ...

$ git rebase -i HEAD^^^

Then choose e, s, s and continue with git hints

Commands to known

$ git init

$ git status

$ git log

$ git log --oneline

$ git add .

$ git commit

$ git stash

$ git stash clear

$ git stash apply

$ git reset HEAD^

$ git log --oneline # and then

$ git reset ..some_commit_hash..

$ git config -h

$ git clone ....

$ git pull origin master

$ git push origin master

$ git checkout -b some_brach

$ git checkout some_branch

$ git merge some_branch

Another git features, but not necessary to know them

  • rebasing - potentially dangerous
  • modification of older commits
  • squashing commits with rebase
  • force push
  • cherry-pick (applications of commits among branches)
  • gitignore ** Cool templates: https://github.com/github/gitignore

cheat sheet: http://www.git-tower.com/blog/git-cheat-sheet-detail/ documentation: http://git-scm.com/doc basics: http://gitref.org/basic/ mergetool: http://meldmerge.org/ commits history: https://wiki.gnome.org/Gitg