GIT TUTORIAL
ref: http://rogerdudler.github.io/git-guide/
1. Create a new repository
create a new directory, open it and perform a
git init
to create a new git repository.
2. checkout a repository
create a working copy of a local repository by running the command
git clone /path/to/repository
git clone https://github.com/balanced/balanced-php.git
when using a remote server, your command will be
git clone username@host:/path/to/repository
git clone advcha@yahoo.com:https://github.com/balanced/balanced-php.git
3. workflow
your local repository consists of three “trees” maintained by git. the first one is your “Working Directory” which holds the actual files. the second one is the “Index” which acts as a staging area and finally the “HEAD” which points to the last commit you’ve made.
4. add & commit
You can propose changes (add it to the Index) using
git add <filename>
git add *
This is the first step in the basic git workflow. To actually commit these changes use
git commit -m “Commit message”
Now the file is committed to the HEAD, but not in your remote repository yet.
5. pushing changes
Your changes are now in the “HEAD” of your local working copy. To send those changes to your remote repository, execute
git push origin master
Change master to whatever branch you want to push your changes to.
If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it with
git remote add origin <server>
Now you are able to push your changes to the selected remote server
6. branching
Branches are used to develop features isolated from each other. The master branch is the “default” branch when you create a repository. Use other branches for development and merge them back to the master branch upon completion.
create a new branch named “feature_x” and switch to it using
git checkout -b feature_x
switch back to master
git checkout master
and delete the branch again
git branch -d feature_x
a branch is not available to others unless you push the branch to your remote repository
git push origin <branch>
7. update & merge
to update your local repository to the newest commit, execute
git pull
in your working directory to fetch and merge remote changes.
to merge another branch into your active branch (e.g. master), use
git merge <branch>
in both cases git tries to auto-merge changes. Unfortunately, this is not always possible and results in conflicts. You are responsible to merge those conflicts manually by editing the files shown by git. After changing, you need to mark them as merged with
git add <filename>
before merging changes, you can also preview them by using
git diff <source_branch> <target_branch>
8. tagging
it’s recommended to create tags for software releases. this is a known concept, which also exists in SVN. You can create a new tag named 1.0.0 by executing
git tag 1.0.0 1b2e1d63ff
the 1b2e1d63ff stands for the first 10 characters of the commit id you want to reference with your tag. You can get the commit id by looking at the log
9. log
in its simplest form, you can study repository history using..
git log
You can add a lot of parameters to make the log look like what you want. To see only the commits of a certain author:
git log –author=bob
To see a very compressed log where each commit is one line:
git log –pretty=oneline
Or maybe you want to see an ASCII art tree of all the branches, decorated with the names of tags and branches:
git log –graph –oneline –decorate –all
See only which files have changed:
git log –name-status
These are just a few of the possible parameters you can use. For more, see
git log –help
10. replace local changes
In case you did something wrong, which for sure never happens ;), you can replace local changes using the command
git checkout — <filename>
this replaces the changes in your working tree with the last content in HEAD. Changes already added to the index, as well as new files, will be kept.
If you instead want to drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it like this
git fetch origin
git reset –hard origin/master
11. useful hints
built-in git GUI
gitk
use colorful git output
git config color.ui true
show log on just one line per commit
git config format.pretty oneline
use interactive adding
git add -i
GIT COLA
tutorial: http://cerebrux.net/2013/02/07/github-git-cola-gui-step-by-step-management-of-your-code/
1) Install Git Cola GUI through Software Center or through terminal:
sudo apt-get install git-cola
2) Copy the repository link from the website then Launch Git Cola and paste the link. In terminal, it’s equivalent of
git clone /github/repo/link.git
Once you launch the Git Cola click «Clone» and paste the link you have copied from Github
Once you provided the link, select where to download the repository and click «Open»
This is the main window. It’s empty because we haven’t changed any code
3) You can edit your preferences by clicking Edit menu were you can customize your settings like email, username, prepared editor etc. In terminal it’s equivalent of:
git config –global user.name ‘John Doe’
git config –global user.email ‘johndoe@mail.com’
To edit your preferences, click on «Edit –> Preferences»
4) Now edit some files inside your local repository clone and the hit the refresh Rescan button on Git Cola. In terminal, it’s equivalent of:
git status
and
git diff
Open the file that you need to edit and start editing. Save and close the file
5) Now once you can hit Stage, Comment to add a message for the changes and the Commit these changes:
In terminal, it’s equivalent of
git add filename
git add README.md
and then
git commit -m ‘Commit message’
git commit -m ‘Update the README file for better description’
OR COMBINE ADD AND COMMIT LIKE THIS:
git commit -a -m ‘Update the README file for better description’
Click Stage, add some comment and then commit
6) You can also Tag your changes to create versions of your repo by clicking Actions menu –> Create Tag
In terminal, it’s equivalent of
git tag -a v4.5.6 -m ‘my version 4.5.6’
Create tag by clicking «Actions –> Add Tag»
7) Now you can upload (push) your changes and versions (tags) to your GitHub account.
In terminal, it’s equivalent of
git push origin master
and your tag with
git push origin v1.0.4
Click on «Push» on the main window, check that «Include tags» is activated and then click Push
Provide your Username and Password of your Github account
Check the commands tab to see the background messages
8) Now you can go to your Github page to see your changes and your tags
Refresh the GitHub page to see your comment and the commit history
I WANT TO UPDATE MY LOCAL REPOSITORY:
ref:http://stackoverflow.com/questions/1443210/updating-a-local-repository-with-changes-from-a-github-repository
teddy@teddy-K43SJ:~/Documents/java/processing-java-client$ git pull origin master
Username for ‘https://github.com’: advcha
Password for ‘https://advcha@github.com’:
From https://github.com/finix-payments/processing-java-client
* branch master -> FETCH_HEAD
Updating 79ff8fe..c52614a
error: Your local changes to the following files would be overwritten by merge:
src/main/java/com/finix/payments/processing/client/Configuration.java
src/main/java/com/finix/payments/processing/client/model/User.java
Please, commit your changes or stash them before you can merge.
Aborting
ref:http://stackoverflow.com/questions/15745045/how-do-i-resolve-git-saying-commit-your-changes-or-stash-them-before-you-can-me
https://git-scm.com/book/en/v1/Git-Tools-Stashing
so pls do:
git stash
then repeat:
git pull origin master
I ADDED A .gitignore FILE. I ALREADY ADD AND COMMIT THE FILE BUT THEN I GOT ERROR HERE WHEN TRYING TO PUSH A CHANGES:
! [rejected] master -> master (fetch first)
error: failed to push some refs to ‘https://github.com/advcha/persuratan.git’
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details.
SO ACTUALLY I DELETED A FEW FILES (WITH ~) ON THE REMOTE REPO. I NEED TO UPDATE MY LOCAL REPO FIRST
git pull origin master
THEN PUSH AGAIN
git push origin master
IF WE DONT WANT TO UPLOAD/PUSH A FILE LIKE database.php
1. Create a new file .gitignore
here is the content:
app/config/database.php
2. type: git commit -a -m ‘add .gitignore file’
3. type: git push OR git push origin master
REMOTE, BRANCH, CHECKOUT
1. check our remote repo:
type: git remote OR MORE DETAIL git remote -v
teddy@teddy-K43SJ:~/Documents/works/persuratan$ git remote -v
origin https://github.com/advcha/persuratan.git (fetch)
origin https://github.com/advcha/persuratan.git (push)
2. We can add a branch in the remote repo.
on the remote repo website, just click the down arrow at Branch:master then type a branch we want like: addfeature
3. in local repo, check our branch
git branch
teddy@teddy-K43SJ:~/Documents/works/persuratan$ git branch
* master
WE ONLY HAVE ONE BRANCH ‘master’ YET
4. create a local repo:
git branch addfeature
teddy@teddy-K43SJ:~/Documents/works/persuratan$ git branch addfeature
THEN check again:
teddy@teddy-K43SJ:~/Documents/works/persuratan$ git branch
addfeature
* master
5. to switch to the new local repo:
git checkout addfeature
teddy@teddy-K43SJ:~/Documents/works/persuratan$ git checkout addfeature
Switched to branch ‘addfeature’
6. make a change in index.html file (AYNTHING) then check status:
teddy@teddy-K43SJ:~/Documents/works/persuratan$ git status
On branch addfeature
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.html
no changes added to commit (use “git add” and/or “git commit -a”)
teddy@teddy-K43SJ:~/Documents/works/persuratan$ git commit -a -m ‘change index.php test’
[addfeature fc28a68] change index.php test
1 file changed, 103 insertions(+), 102 deletions(-)
rewrite index.html (97%)
7. commit the changes
teddy@teddy-K43SJ:~/Documents/works/persuratan$ git commit -a -m ‘change index.php test’
[addfeature fc28a68] change index.php test
1 file changed, 103 insertions(+), 102 deletions(-)
rewrite index.html (97%)
8. then push to the remote repo (branch ‘addfeature’)
teddy@teddy-K43SJ:~/Documents/works/persuratan$ git push addfeature
9. Check the remote repo
index.html in master branch not affected ONLY in addfeature branch
10. merge the branch to master(NOT WORKING!!!)
git merge master
git checkout master
git push
OK THIS HAS TO BE DONE IN github PAGE. FIRST CLICK ‘New Pull Request’
11. Click ‘Create Pull Request’ … THEN WE CAN MERGE IT
AT LAST WE CAN DELETE THE ‘addfeature’ BRANCH BECAUSE ITS ALREADY MERGED TO ‘master’ BRANCH!
WE CAN ALSO DELETE THE REMOTE BRANCH FROM TERMINAL WITH git push origin :addfeature (BE CAREFULL!!!)
teddy@teddy-K43SJ:~/Documents/works/persuratan$ git push origin :addfeature
Username for ‘https://github.com’: advcha
Password for ‘https://advcha@github.com’:
error: unable to delete ‘addfeature’: remote ref does not exist
error: failed to push some refs to ‘https://github.com/advcha/persuratan.git’
ref:http://makandracards.com/makandra/621-git-delete-a-branch-local-or-remote
http://stackoverflow.com/questions/2003505/delete-a-git-branch-both-locally-and-remotely
12. NOW WE NEED TO MERGE IT ALSO IN OUR LOCAL REPO
type: git checkout master
then: git pull
teddy@teddy-K43SJ:~/Documents/works/persuratan$ git checkout master
Already on ‘master’
Your branch is up-to-date with ‘origin/master’.
teddy@teddy-K43SJ:~/Documents/works/persuratan$ git pull
remote: Counting objects: 1, done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (1/1), done.
From https://github.com/advcha/persuratan
1d338b5..7cc859e master -> origin/master
Updating 1d338b5..7cc859e
Fast-forward
index.html | 187 +++++++++++++++++++++++++++++++——————————
1 file changed, 94 insertions(+), 93 deletions(-)
13. DELETE LOCAL BRANCH ‘addfeature’ (ref:http://makandracards.com/makandra/621-git-delete-a-branch-local-or-remote)
type: git branch -d addfeature
teddy@teddy-K43SJ:~/Documents/works/persuratan$ git branch -d addfeature
Deleted branch addfeature (was fc28a68).
14. Check the branch again
teddy@teddy-K43SJ:~/Documents/works/persuratan$ git branch
* master
15. SYNCHRONIZED THE BRANCHES (REMOTE & LOCAL)
teddy@teddy-K43SJ:~/Documents/works/persuratan$ git fetch -p
From https://github.com/advcha/persuratan
x [deleted] (none) -> origin/addfeature
MERGE TWO REPOSITORIES
re:http://saintgimp.org/2013/01/22/merging-two-git-repositories-into-one-repository-without-losing-file-history/
http://stackoverflow.com/questions/1425892/how-do-you-merge-two-git-repositories
REAL WORLD SAMPLE DEVELOPMENT WITH GITHUB:http://nvie.com/posts/a-successful-git-branching-model/
WORKING SAMPLE FROM SCRATCH
1. Create a new repository in my github account: OpenCV-python (https://github.com/advcha/OpenCV-Python.git)
2. in my terminal, clone it into /media/data/MASTER/opencv/
teddy@teddy-K43SJ:/media/data/MASTER/opencv$ git clone https://github.com/advcha/OpenCV-Python.git
Cloning into ‘OpenCV-Python’…
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
Checking connectivity… done.
3. Add some files and directories in it like images/ and opencv_read_write.py (I copied them from /home/teddy/opencv/samples/python/) OR SEARCH THE FILENAME IN /media/data/MASTER/opencv/opencv_python_tutorial.txt
4. check the un-uploaded file and dir with ‘git status’:
teddy@teddy-K43SJ:/media/data/MASTER/opencv/OpenCV-Python$ git status
On branch master
Your branch is up-to-date with ‘origin/master’.
Untracked files:
(use “git add <file>…” to include in what will be committed)
images/
opencv_read_write.py
nothing added to commit but untracked files present (use “git add” to track)
5. Add the file and the dir to prepare for uploading with ‘git add *’:
teddy@teddy-K43SJ:/media/data/MASTER/opencv/OpenCV-Python$ git add *
6. Commit the file and dir and give it a tag ‘upload some python scripts’:
teddy@teddy-K43SJ:/media/data/MASTER/opencv/OpenCV-Python$ git commit -a -m ‘upload some python scripts’
[master 46abb81] upload some python scripts
28 files changed, 17 insertions(+)
create mode 100644 images/Temperate 09 – Small.png
create mode 100644 images/Temperate 09 – Small_clean.png
create mode 100644 images/approx.jpg
create mode 100644 images/bolt.png
create mode 100644 images/clahe.jpg
create mode 100644 images/dave.jpg
create mode 100644 images/dave.png
create mode 100644 images/fruits.jpg
create mode 100644 images/fruits_gray.jpg
create mode 100644 images/grass.png
create mode 100644 images/hand.jpg
create mode 100644 images/john_dory-zeus_faber.jpg
create mode 100644 images/mario.jpg
create mode 100644 images/mario_coin.png
create mode 100644 images/mario_coin1.png
create mode 100644 images/messi5.jpg
create mode 100644 images/messi_face.jpg
create mode 100644 images/more_coins.png
create mode 100644 images/my_family.jpg
create mode 100644 images/noisy2.png
create mode 100644 images/opencv_logo.png
create mode 100644 images/rect.jpg
create mode 100644 images/shapes.png
create mode 100644 images/star.jpg
create mode 100644 images/star2.jpg
create mode 100644 images/uss_arizona.jpg
create mode 100644 images/water_coins.jpg
create mode 100644 opencv_read_write.py
7. Push them to github account with ‘git push origin master’:
teddy@teddy-K43SJ:/media/data/MASTER/opencv/OpenCV-Python$ git push origin master
Username for ‘https://github.com’: advcha
Password for ‘https://advcha@github.com’:
Counting objects: 32, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (31/31), done.
Writing objects: 100% (31/31), 5.93 MiB | 1.10 MiB/s, done.
Total 31 (delta 0), reused 0 (delta 0)
To https://github.com/advcha/OpenCV-Python.git
330ef49..46abb81 master -> master
8. Check again my local repo with ‘git status’:
teddy@teddy-K43SJ:/media/data/MASTER/opencv/OpenCV-Python$ git status
On branch master
Your branch is up-to-date with ‘origin/master’.
nothing to commit, working directory clean
OK DONE
9. Add some python scripts from /home/teddy/opencv/samples/python/ (copy and paste them into /media/data/MASTER/opencv/OpenCV-Python)
10. check with ‘git status’:
teddy@teddy-K43SJ:/media/data/MASTER/opencv/OpenCV-Python$ git status
On branch master
Your branch is up-to-date with ‘origin/master’.
Untracked files:
(use “git add <file>…” to include in what will be committed)
drawing.py
mouse_draw.py
mouse_paint.py
mouse_paint_img.py
nothing added to commit but untracked files present (use “git add” to track)
11. add them with ‘git add *’:
teddy@teddy-K43SJ:/media/data/MASTER/opencv/OpenCV-Python$ git add *
12. commit them:
teddy@teddy-K43SJ:/media/data/MASTER/opencv/OpenCV-Python$ git commit -a -m ‘Upload some python scripts’
[master 567e4fd] Upload some python scripts
4 files changed, 165 insertions(+)
create mode 100644 drawing.py
create mode 100644 mouse_draw.py
create mode 100644 mouse_paint.py
create mode 100644 mouse_paint_img.py
13. The last, push them into my github account:
teddy@teddy-K43SJ:/media/data/MASTER/opencv/OpenCV-Python$ git push origin master
Username for ‘https://github.com’: advcha
Password for ‘https://advcha@github.com’:
Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 2.66 KiB | 0 bytes/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To https://github.com/advcha/OpenCV-Python.git
46abb81..567e4fd master -> master
OK. DO IT OVER AND OVER AGAIN FOR THE OTHERS FILES AND DIRECTORIES!!!
HOW TO UPDATE FILE AND UPDATE THE CHANGES TO REMOTE REPO?
1. Modify README.md to
Some python scripts for practising OpenCV. Use Python 2.7 (some scripts works for Python 3.4) and OpenCV 3.1.0.
SAVE THE MODIFICATION (IT BETTER TO REMOVE THE TEMPORARY FILE LIKE README.md~)
2. check the status:
teddy@teddy-K43SJ:/media/data/MASTER/opencv/OpenCV-Python$ git status
On branch master
Your branch is up-to-date with ‘origin/master’.
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: README.md
Untracked files:
(use “git add <file>…” to include in what will be committed)
README.md~
no changes added to commit (use “git add” and/or “git commit -a”)
3. use ‘git add *’
teddy@teddy-K43SJ:/media/data/MASTER/opencv/OpenCV-Python$ git add *
4. commit:
teddy@teddy-K43SJ:/media/data/MASTER/opencv/OpenCV-Python$ git commit -a -m ‘Update README’
[master c7ea471] Update README
2 files changed, 3 insertions(+), 1 deletion(-)
create mode 100644 README.md~
5. use ‘git push’:
teddy@teddy-K43SJ:/media/data/MASTER/opencv/OpenCV-Python$ git push origin master
Username for ‘https://github.com’: advcha
Password for ‘https://advcha@github.com’:
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 367 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://github.com/advcha/OpenCV-Python.git
1f66fba..c7ea471 master -> master
LOCAL REPO DIRECTORY HAS DIFFERENT NAME WITH REMOTE REPO:
ref:http://stackoverflow.com/questions/8570636/change-name-of-folder-when-cloning-from-github
git clone https://github.com/sferik/sign-in-with-twitter.git signin
REMOTE REPO: sign-in-with-twitter
LOCAL REPO: signin