Github: Difference between revisions
No edit summary |
|||
Line 1: | Line 1: | ||
= Git = | |||
* [http://kbroman.org/github_tutorial/ git/github guide a minimal tutorial] from Karl Broman | |||
* Some examples of projects hosted in github: RHadloop, shiny, Qt. | |||
* https://help.github.com/articles/fork-a-repo | |||
* http://www.atlassian.com/git/tutorial/undoing-changes | |||
* [http://gitref.org/creating/ Git reference] (5 pages of summary) and [http://git-scm.com/book/en/Git-Basics-Working-with-Remotes Pro Git book] | |||
* [http://www.vogella.com/articles/Git/article.html Distributed Version Control with Git] (nice tutorial including Github, BitBucket. ) | |||
* [http://byte.kde.org/~zrusin/git/git-cheat-sheet.svg Cheat sheet] | |||
* https://www.kernel.org/pub/software/scm/git/docs/everyday.html | |||
* http://haacked.com/archive/2012/05/21/introducing-github-for-windows.aspx | |||
* [https://help.github.com/articles/deleting-a-repository Delete a repository] | |||
* '''git push''' will ask username/password if we have not logged in (eg use linux machine). | |||
* Under Windows, we need to provide username/password when we install Git program provided by Github. Even that, when we use powershell program to commit codes to github, the username was not yet shown on the github website. We need to use git config --global user.name "Your Name" to fix it. | |||
* When I commit (git commit) a directory containing resource file, it shows warning: LF will be replaced by CRLF in xxxx.qrc. | |||
* [https://help.github.com/articles/set-up-git Password cache] | |||
* [http://kbroman.org/github_tutorial/ github tutorial] (for statistician) | |||
* [http://stackoverflow.com/questions/572549/difference-between-git-add-a-and-git-add git add -A] | |||
<pre> | |||
git config --global credential.helper cache | |||
</pre> | |||
My example of working on a new repository called 'network'. | |||
# Follow https://help.github.com/articles/create-a-repo to create a new repository. For convenience, I also check the button to create README file. | |||
# Click 'GitHub' icon on Windows Desktop. Look at the LHS and click on the word 'github'. Click 'clone' button (This can be accomplished by '''git clone https://github.com/arraytools/network.git'''). The new repository will appear under C:\Users\USERNAME\Documents\Github\ directory. Now Click 'Git Shell' icon on the Windows Desktop and go to C:\Users\USERNAME\Documents\Github\network directory where 'network' is my repository's name. | |||
<pre> | |||
git config --global color.ui auto # colorize the output of git | |||
git init | |||
git add client.c | |||
git add server.c | |||
git add server2.c | |||
git commit -m 'first commit' | |||
git status | |||
# On branch master | |||
# Your branch is ahead of 'origin/master' by 1 commit. | |||
# git remote add origin https://github.com/arraytools/network.git | |||
# git push <remote> <branch> | |||
git push origin master | |||
# If we modify any file, we need to go through git add, git commit and git push 3 commands. | |||
# get commit_id | |||
git log | |||
# get a specific version | |||
git checkout commit_id | |||
# after an examination, we want to get the latest version | |||
git pull origin master | |||
# If you do not want to merge the upstream changes wit your local repository, run git fetch to | |||
# fetch the changes and then git merge to merge the changes. | |||
# git pull is just a combination of fetch and merge. | |||
# To rename a file | |||
git mv originafile newfile | |||
git commit -m "rename orginalfile" | |||
git push | |||
# To remove a file | |||
rm myfile | |||
git add . -A | |||
git commit -m "remove a file" | |||
git push | |||
# Revert to origin's master branch's version of file | |||
# http://stackoverflow.com/questions/1817766/revert-to-origins-master-branchs-version-of-file | |||
# 1. Assuming you did not commit the file, or add it to the index, then: | |||
git checkout filename | |||
# 2. Assuming you added it to the index, but did not commit it, then: | |||
git reset HEAD filename | |||
git checkout filename | |||
# Compare difference in local and remote repository | |||
git fetch (the syntax is the same as git pull, but it doesn't automatically merge) | |||
git status | |||
git diff master origin/master --color --name-only (color, Show only names of changed files.) | |||
# To force to overwrite local files on pull | |||
git fetch --all | |||
git reset --hard origin/master | |||
</pre> | |||
Check the https://github.com/arraytools/network. The commit goes to the repository! | |||
In summary: '''add''' and '''commit''' are local operations, '''push''', '''pull''' and '''fetch''' are operations that interact with a remote. | |||
If we want to checkout a specific commit on a new computer, we can use (here we use Qt repository as an example) | |||
<pre> | |||
git clone https://xxxxxx/xxxxxx/Qt.git | |||
cd Qt | |||
git log --oneline | |||
git checkout SHA1 (7 digits obtained from git log --oneline commandis enough) | |||
</pre> | |||
After that we can run | |||
<pre> | |||
# move HEAD to origin | |||
git checkout origin/master | |||
# Visualize using text mode | |||
git log --graph --oneline --date-order --decorate --color --all | |||
</pre> | |||
Using GUI client: gitg | |||
<pre> | |||
sudo apt-get install gitg | |||
</pre> | |||
To switch among different branch (eg. github project page is located in '''gh-pages''' branch of a repository), | |||
<pre> | |||
git checkout gh-pages | |||
git checkout master | |||
</pre> | |||
To avoid being asking the username each time when you use 'git push', you can modify the .git/config file of your local repository. This file contains a section called 'remote' with an entry called 'url'. The 'url' entry should contains the https link of repository you're talking about. When you prefix the host url with your username, git shouldn't be asking for your username anymore. Here's an example: url = https://[email protected] | |||
== Set up a new local repository == | |||
<pre> | |||
mkdir /path/to/your/project | |||
cd /path/to/your/project | |||
git init | |||
git remote add origin https://[email protected]/arraytools/REPOSITORYNAME.git | |||
git config --global user.name "YOUR NAME" | |||
git config --global user.email "YOUR EMAIL ADDRESS" | |||
echo "arraytools" >> contributors.txt | |||
git add contributors.txt | |||
git commit -m 'Initial commit with contributors' | |||
git push -u origin master | |||
</pre> | |||
== Already has a git repository on my computer == | |||
<pre> | |||
cd /path/to/my/repo | |||
git remote add origin https://[email protected]/arraytools/REPOSITORYNAME.git | |||
git push -u origin --all # pushes up the repo and its refs for the first time | |||
git push -u origin --tags # pushes up any tags | |||
</pre> | |||
== Rename a repository == | |||
# Go to github.com, open the project and click Settings button on the left-bottom corner. Change the repository name on top. | |||
# On local machine, rename the directory. Go to the directory. Issue | |||
<pre> | |||
git remote -v | |||
</pre> | |||
to get the ULR for the current working copy. Suppose the url is ''[email protected]:someuser/someproject.git''. Now issue the following command to change to the new repository | |||
<pre> | |||
git remote set-url origin [email protected]:someuser/newprojectname.git | |||
</pre> | |||
== Git Tips == | |||
* '''git difftool FILENAME -y''' will launch 'meld' (if it has been installed before) to compare the file between revisions by using custom tools. It has to be run before we call '''git add'''. This is quite convenient since you can double check before running git commit. The '-y' argument is used to launch a diff tool without a prompt. See the documentation [http://git-scm.com/docs/git-difftool here]. | |||
* If we want to monitor/find files that have been changed since last pull, run | |||
<pre> | |||
git fetch origin/master | |||
git diff --name-only origin/master | |||
</pre> | |||
* If we want to run git pull and also overwrite possibly changed local file, we use (see [http://stackoverflow.com/questions/1125968/force-git-to-overwrite-local-files-on-pull stackoverflow]) | |||
<pre> | |||
git fetch --all | |||
git reset --hard origin/master | |||
</pre> | |||
* '''git status''' shows '''Your branch is Ahead by X commits''' after running '''git pull'''. See [http://stackoverflow.com/questions/2432579/git-your-branch-is-ahead-by-x-commits this post]. The solution is to run '''git fetch''' after '''git pull'''. | |||
== Create a git server (command line only) == | |||
Follow the instruction on [http://git-scm.com/book/en/Git-on-the-Server-Setting-Up-the-Server git-scm.com]. It works. I tested it by | |||
# create a git account (called 'git') on my host machine. | |||
# sudo to create a directory under /opt/git/project.git. Change the owner to 'git'. Cd to the directory and initialize it. | |||
# Create two virtual machines (vb1 and vb2). Creating a username 'david' on vb1 and a user name 'joseph' on vb2. | |||
# Create ssh key for both 'david' and 'joseph'. Ssh to copy their ssh keys to git account. | |||
# Create a new directory on vb1 and initialize it. Run git commands to commit & push files to the server (no password is needed). Note that when we use 'git commit', git will ask to create a username and email by first running 'git config' command. | |||
# Switch to vb2 and run git clone (no password is needed). The user can modify the code and commit & push files to the server. | |||
# Run 'git log' to check if each user's name/email are shown on the log. | |||
== Create a git server (github like w/ web interface) == | |||
If we like to create a github-like web interface, check out [https://about.gitlab.com/ GitLab]. | |||
Below is my note | |||
# https://about.gitlab.com/downloads/ contains steps of setting up Gitlab. | |||
# By default, the domain name you have entered in setting up gitlab will be the URL you will use to access gitlab. | |||
# Use the recommended method to install gitlab. Nginx will be installed as an http server. | |||
# The root username and password is root and 5iveL!fe. | |||
# When new users are created by root, we can put a faked email there (eg [email protected]). The root account can create password for the user. | |||
# User's password is used to access GitLab web interface only. It is not used for pushing commits. | |||
# After a new user is created, log out of root account and log in using the new user account. Click 'Profile setting' icon and then select SSH > Add SSH key. Copy your <id_rsa.pub> content there. To create your ssh key, use the command line "ssh-keygen -t rsa". The <id_rsa.pub> is located under ~/.ssh directory. The title should be auto populated. If ssh key is added successfully to gitLab, we won't get a pop-up asking password when we run 'git push'. | |||
# A new project should be created by users (not root). If I create a project by root, I keep getting a permission issue when I run 'git push'. | |||
# The username will affect path to all personal projects; e.g. [email protected]:newuser/test2.git. | |||
[[File:Gitlab2.png|100px]] [[File:Gitlab1.png|100px]] [[File:Gitlab3.png|100px]] | |||
== [http://git-scm.com/docs/gitk gitk] - graphical tool to display history == | |||
* http://lostechies.com/joshuaflanagan/2010/09/03/use-gitk-to-understand-git/ | |||
* Other GUI tools http://git-scm.com/download/gui/linux | |||
<pre> | |||
sudo apt-get update; sudo apt-get install gitk | |||
git clone [email protected]:joshuaflanagan/gitk-demo.git | |||
cd gitk-demo | |||
gitk --all | |||
</pre> | |||
== General resources == | |||
* https://www.atlassian.com/git/tutorial/remote-repositories | |||
* http://www.sbf5.com/~cduan/technical/git/git-4.shtml | |||
* http://www.acquia.com/blog/getting-started-collaborative-development-git | |||
* http://documentup.com/skwp/git-workflows-book | |||
* http://www.eqqon.com/index.php/Collaborative_Github_Workflow | |||
= Host web site = | = Host web site = | ||
* [http://blog.teamtreehouse.com/using-github-pages-to-host-your-website Using GitHub Pages To Host Your Website] using a Custom Domain. The IP address information for Github is changed to [https://help.github.com/articles/my-custom-domain-isn-t-working/ new ones]. | * [http://blog.teamtreehouse.com/using-github-pages-to-host-your-website Using GitHub Pages To Host Your Website] using a Custom Domain. The IP address information for Github is changed to [https://help.github.com/articles/my-custom-domain-isn-t-working/ new ones]. |
Revision as of 08:15, 13 July 2015
Git
- git/github guide a minimal tutorial from Karl Broman
- Some examples of projects hosted in github: RHadloop, shiny, Qt.
- https://help.github.com/articles/fork-a-repo
- http://www.atlassian.com/git/tutorial/undoing-changes
- Git reference (5 pages of summary) and Pro Git book
- Distributed Version Control with Git (nice tutorial including Github, BitBucket. )
- Cheat sheet
- https://www.kernel.org/pub/software/scm/git/docs/everyday.html
- http://haacked.com/archive/2012/05/21/introducing-github-for-windows.aspx
- Delete a repository
- git push will ask username/password if we have not logged in (eg use linux machine).
- Under Windows, we need to provide username/password when we install Git program provided by Github. Even that, when we use powershell program to commit codes to github, the username was not yet shown on the github website. We need to use git config --global user.name "Your Name" to fix it.
- When I commit (git commit) a directory containing resource file, it shows warning: LF will be replaced by CRLF in xxxx.qrc.
- Password cache
- github tutorial (for statistician)
- git add -A
git config --global credential.helper cache
My example of working on a new repository called 'network'.
- Follow https://help.github.com/articles/create-a-repo to create a new repository. For convenience, I also check the button to create README file.
- Click 'GitHub' icon on Windows Desktop. Look at the LHS and click on the word 'github'. Click 'clone' button (This can be accomplished by git clone https://github.com/arraytools/network.git). The new repository will appear under C:\Users\USERNAME\Documents\Github\ directory. Now Click 'Git Shell' icon on the Windows Desktop and go to C:\Users\USERNAME\Documents\Github\network directory where 'network' is my repository's name.
git config --global color.ui auto # colorize the output of git git init git add client.c git add server.c git add server2.c git commit -m 'first commit' git status # On branch master # Your branch is ahead of 'origin/master' by 1 commit. # git remote add origin https://github.com/arraytools/network.git # git push <remote> <branch> git push origin master # If we modify any file, we need to go through git add, git commit and git push 3 commands. # get commit_id git log # get a specific version git checkout commit_id # after an examination, we want to get the latest version git pull origin master # If you do not want to merge the upstream changes wit your local repository, run git fetch to # fetch the changes and then git merge to merge the changes. # git pull is just a combination of fetch and merge. # To rename a file git mv originafile newfile git commit -m "rename orginalfile" git push # To remove a file rm myfile git add . -A git commit -m "remove a file" git push # Revert to origin's master branch's version of file # http://stackoverflow.com/questions/1817766/revert-to-origins-master-branchs-version-of-file # 1. Assuming you did not commit the file, or add it to the index, then: git checkout filename # 2. Assuming you added it to the index, but did not commit it, then: git reset HEAD filename git checkout filename # Compare difference in local and remote repository git fetch (the syntax is the same as git pull, but it doesn't automatically merge) git status git diff master origin/master --color --name-only (color, Show only names of changed files.) # To force to overwrite local files on pull git fetch --all git reset --hard origin/master
Check the https://github.com/arraytools/network. The commit goes to the repository!
In summary: add and commit are local operations, push, pull and fetch are operations that interact with a remote.
If we want to checkout a specific commit on a new computer, we can use (here we use Qt repository as an example)
git clone https://xxxxxx/xxxxxx/Qt.git cd Qt git log --oneline git checkout SHA1 (7 digits obtained from git log --oneline commandis enough)
After that we can run
# move HEAD to origin git checkout origin/master # Visualize using text mode git log --graph --oneline --date-order --decorate --color --all
Using GUI client: gitg
sudo apt-get install gitg
To switch among different branch (eg. github project page is located in gh-pages branch of a repository),
git checkout gh-pages git checkout master
To avoid being asking the username each time when you use 'git push', you can modify the .git/config file of your local repository. This file contains a section called 'remote' with an entry called 'url'. The 'url' entry should contains the https link of repository you're talking about. When you prefix the host url with your username, git shouldn't be asking for your username anymore. Here's an example: url = https://[email protected]
Set up a new local repository
mkdir /path/to/your/project cd /path/to/your/project git init git remote add origin https://[email protected]/arraytools/REPOSITORYNAME.git git config --global user.name "YOUR NAME" git config --global user.email "YOUR EMAIL ADDRESS" echo "arraytools" >> contributors.txt git add contributors.txt git commit -m 'Initial commit with contributors' git push -u origin master
Already has a git repository on my computer
cd /path/to/my/repo git remote add origin https://[email protected]/arraytools/REPOSITORYNAME.git git push -u origin --all # pushes up the repo and its refs for the first time git push -u origin --tags # pushes up any tags
Rename a repository
- Go to github.com, open the project and click Settings button on the left-bottom corner. Change the repository name on top.
- On local machine, rename the directory. Go to the directory. Issue
git remote -v
to get the ULR for the current working copy. Suppose the url is [email protected]:someuser/someproject.git. Now issue the following command to change to the new repository
git remote set-url origin [email protected]:someuser/newprojectname.git
Git Tips
- git difftool FILENAME -y will launch 'meld' (if it has been installed before) to compare the file between revisions by using custom tools. It has to be run before we call git add. This is quite convenient since you can double check before running git commit. The '-y' argument is used to launch a diff tool without a prompt. See the documentation here.
- If we want to monitor/find files that have been changed since last pull, run
git fetch origin/master git diff --name-only origin/master
- If we want to run git pull and also overwrite possibly changed local file, we use (see stackoverflow)
git fetch --all git reset --hard origin/master
- git status shows Your branch is Ahead by X commits after running git pull. See this post. The solution is to run git fetch after git pull.
Create a git server (command line only)
Follow the instruction on git-scm.com. It works. I tested it by
- create a git account (called 'git') on my host machine.
- sudo to create a directory under /opt/git/project.git. Change the owner to 'git'. Cd to the directory and initialize it.
- Create two virtual machines (vb1 and vb2). Creating a username 'david' on vb1 and a user name 'joseph' on vb2.
- Create ssh key for both 'david' and 'joseph'. Ssh to copy their ssh keys to git account.
- Create a new directory on vb1 and initialize it. Run git commands to commit & push files to the server (no password is needed). Note that when we use 'git commit', git will ask to create a username and email by first running 'git config' command.
- Switch to vb2 and run git clone (no password is needed). The user can modify the code and commit & push files to the server.
- Run 'git log' to check if each user's name/email are shown on the log.
Create a git server (github like w/ web interface)
If we like to create a github-like web interface, check out GitLab.
Below is my note
- https://about.gitlab.com/downloads/ contains steps of setting up Gitlab.
- By default, the domain name you have entered in setting up gitlab will be the URL you will use to access gitlab.
- Use the recommended method to install gitlab. Nginx will be installed as an http server.
- The root username and password is root and 5iveL!fe.
- When new users are created by root, we can put a faked email there (eg [email protected]). The root account can create password for the user.
- User's password is used to access GitLab web interface only. It is not used for pushing commits.
- After a new user is created, log out of root account and log in using the new user account. Click 'Profile setting' icon and then select SSH > Add SSH key. Copy your <id_rsa.pub> content there. To create your ssh key, use the command line "ssh-keygen -t rsa". The <id_rsa.pub> is located under ~/.ssh directory. The title should be auto populated. If ssh key is added successfully to gitLab, we won't get a pop-up asking password when we run 'git push'.
- A new project should be created by users (not root). If I create a project by root, I keep getting a permission issue when I run 'git push'.
- The username will affect path to all personal projects; e.g. [email protected]:newuser/test2.git.
gitk - graphical tool to display history
- http://lostechies.com/joshuaflanagan/2010/09/03/use-gitk-to-understand-git/
- Other GUI tools http://git-scm.com/download/gui/linux
sudo apt-get update; sudo apt-get install gitk git clone [email protected]:joshuaflanagan/gitk-demo.git cd gitk-demo gitk --all
General resources
- https://www.atlassian.com/git/tutorial/remote-repositories
- http://www.sbf5.com/~cduan/technical/git/git-4.shtml
- http://www.acquia.com/blog/getting-started-collaborative-development-git
- http://documentup.com/skwp/git-workflows-book
- http://www.eqqon.com/index.php/Collaborative_Github_Workflow
Host web site
- Using GitHub Pages To Host Your Website using a Custom Domain. The IP address information for Github is changed to new ones.
Some Examples
Jekyll
Install Jekyll
on Ubuntu 14.04 I use (the second command will take a while to start),
sudo apt-get install ruby1.9.1-dev sudo gem install jekyll
Example 1
cd /tmp jekyll new MyNewSite # It will create a new folder 'MyNewSite' with about.md, _config.yml, css (folder), # _includes (folder), index.html, _layouts (folder), _posts (folder) and _sass (folder). cd MyNewSite jekyll serve --watch # It'll say the Server Address: http://127.0.0.1:4000/ # We can open a browser to see a template of html page created by '''jekyll new''' command.
hexo
Hexo is a fast, simple and powerful blog framework. You write posts in Markdown (or other languages) and Hexo generates static files with a beautiful theme in seconds.