Dziś będą git podstawy, dodaje parę przydatnych komend w zasadzie takie małe wprowadzenie. Sam korzystam z github.com i używam podstawowych poleceń aby go w miarę ogarniać. Nie jest to jakiś oszałamiający zestaw, ale na pewno początkującemu użytkownikowi przyda się aby łatwo zarządzać wersjami swojego projektu.

Poniżej komendy w takiej kolejności tak jak mniej więcej się ich używa w praktyce.

Git podstawy

git init – inicjalizacja repozytorium

sosprz@sosprz:~$ mkdir test
sosprz@sosprz:~$ cd test/
sosprz@sosprz:~/test$ ls
sosprz@sosprz:~/test$ touch index.php
sosprz@sosprz:~/test$ echo "test123" >> index.php

sosprz@sosprz:~/test$ git init 
Initialized empty Git repository in /home/sosprz/test/.git/

git config – konfiguracja

sosprz@sosprz:~/test$ git config --global user.email "test@gmail.com"
sosprz@sosprz:~/test$ git config --global user.name "User"

git status – status projektu, przydatna sprawa widać co się dzieje i informuje o nowych plikach w repo.

sosprz@sosprz:~/test$ git status
 # On branch master
 #
 # Initial commit
 #
 # Untracked files:
 # (use "git add ..." to include in what will be committed)
 #
 # index.php
 nothing added to commit but untracked files present (use "git add" to track)

git add – dodaje pliki do repozytorium

sosprz@sosprz:~/test$ git add index.php

git status

sosprz@sosprz:~/test$ git status
 # On branch master
 #
 # Initial commit
 #
 # Changes to be committed:
 # (use "git rm --cached ..." to unstage)
 #
 # new file: index.php
 #

git commit -am „pierwszy” – zatwierdzanie zmian, trzeba dodać komentarz.

sosprz@sosprz:~/test$ git commit -am "pierwszy" 
 [master (root-commit) f301ae3] start 1 file changed, 1 insertion(+) 
 create mode 100644 index.php

git commit -am „nowe zmiany”

sosprz@sosprz:~/test$ echo "test1234" >> index.php

sosprz@sosprz:~/test$ git commit -am "nowe zmiany"
 [master 0701f4a] nowe zmiany
 1 file changed, 1 insertion(+)

git log – wyświetla listuje zatwierdzone commity wraz z komentarzami

sosprz@sosprz:~/test$ git log
 commit 0701f4a1bd44c8edee30a606ca7ca2c2e9e65361
 Author: User <test@gmail.com>
 Date: Wed Jun 5 23:51:54 2013 +0200

nowe zmiany

commit f301ae30211c419ef90a4f8e87a1cad81a9598cd

 Author: User <test@gmail.com>

 Date: Wed Jun 5 23:49:51 2013 +0200

start

Git wgrywanie danych na zdalny serwer

git remote add – dodanie zdalnego punktu

git remote add sosprz https://github.com/sosprz/nettemp

git push – wgrywanie zmian na zdalny serwer

git push sosprz

Zarządzanie gałęziami

git branch – wyświetlanie istniejących gałężi i ich tworzenie.

sosprz@sosprz:~/test$ git branch
* master
sosprz@sosprz:~/test$ git branch fix
sosprz@sosprz:~/test$ git branch
 fix
* master

git checkout – przełączanie się pomiędzy gałęziami

sosprz@sosprz:~/test$ git checkout fix
Switched to branch 'fix'

git status

sosprz@sosprz:~/test$ echo lol >> index.php 

sosprz@sosprz:~/test$ git status
# On branch fix
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: index.php
#
no changes added to commit (use "git add" and/or "git commit -a")

git commit

sosprz@sosprz:~/test$ git commit -am "fix"
[fix b69efd0] fix
 1 file changed, 1 insertion(+)

git checkout

sosprz@sosprz:~/test$ git checkout master
Switched to branch 'master'

git merge – łączenie gałężi z głównym repozytorium

sosprz@sosprz:~/test$ git merge fix
Updating 0701f4a..b69efd0
Fast-forward
 index.php | 1 +
 1 file changed, 1 insertion(+)

git push -u – wgrywanie gałężi na zdalny serwer

sosprz@sosprz:~/test$ git push -u origin fix

Username for 'https://github.com': sosprz
Password for 'https://sosprz@github.com': 
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/sosprz/nettemp
 * [new branch] fix -> fix
Branch test set up to track remote branch test from origin.

git push –delete – kasowanie zdalnej gałężi

sosprz@sosprz:/test$ git push --delete origin fix

 Username for 'https://github.com': sosprz
 Password for 'https://sosprz@github.com':
 To https://github.com/sosprz/nettemp
 - [deleted] fix

git push –delete – kasowanie lokalnej gałężi

sosprz@sosprz:test$ git branch --delete test
Deleted branch test (was 7f6d216).

git remote show origin – pokazuje informacje o zdalnym repozytroium

root@raspberrypi:/var/www/nettemp# git remote show origin
* remote origin
 Fetch URL: https://github.com/sosprz/nettemp
 Push URL: https://github.com/sosprz/nettemp
 HEAD branch (remote HEAD is ambiguous, may be one of the following):
  master
  v6
 Remote branches:
  master tracked
  v6 tracked
 Local branches configured for 'git pull':
  master merges with remote master
  v6 merges with remote v6
 Local refs configured for 'git push':
  master pushes to master (up to date)
  v6 pushes to v6 (up to date

git pull – pobiera tylko zmiany

git pull https://github.com/sosprz/nettemp

git clone – pobiera całość

git clone https://github.com/sosprz/nettemp

Dodawanie zdalnej gałęźi

git checkout -b beta origin/beta

Dodatkowe:

git update-index

Przydatną komendą w git, jest opcja git update-index –assume-unchanged która to powoduje że plik który wskażemy zostaje cały czas w takim samym stanie w bazie git, chyba że wydamy komendę git update-index –no-assume-unchanged.**** Przydaje się to np gdy pracujemy z bazą i nie chcemy żeby po każdym komicie w git były tam dane np. testowe lub prywatne.

git update-index --assume-unchanged baza.sql
git update-index --no-assume-unchanged baza.sql

git clone -b – pobieranie gałęzi

Aby ściągnąć gałąź bo np. jest tam nasza wersja beta trzeba wydać polecenie: git clone -b gałąź repo

git clone -b v7 https://github.com/sosprz/nettemp

git reset

Aby zrestartować zmiany/cofnąć się do ostatniego commita

git reset --hard HEAD