Git Guide
A guide of git / github
I. Create a new project from scratch
- Create a directory of the project.
- Go to the directory and type “git init”
- Start coding
- Add the files to git by using “git add” command
- Commit the update by using “git commit” command
The first file to create (and add and commit) is probably a ReadMe file, either as plain text or with Markdown, describing the project.
Markdown allows you to add a bit of text markup, like hyperlinks, bold/italics, or to indicate code with a monospace font
. Markdown is easily converted to html for viewing in a web browser, and GitHub will do this for you automatically.
II. Add an existing project to git
- Create a directory of the project.
- Go to the directory and type “git init”
- Add the files to git by using “git add” command
- Create a .gitignore file to indicate all of the files you don’t want to track. Type “git add .gitignore” too.
- Commit the update by using “git commit” command
III. Connect local git repository to github
- Go to github.
- Log in to your account.
- Click the new repository button in the top-right. You’ll have an option there to initialise the repository with a README file, but I don’t.
- Click the “Create repository” button.
- Now, follow the second set of instructions, “Push an existing repository…”
$ git remote add origin [email protected]:username/new_repo
$ git push -u origin master
Actually, the first line of the instructions will say
$ git remote add origin https://github.com/username/new_repo
But it is better to use [email protected]:username/new_repo
rather than https://github.com/username/new_repo
, as the former is for use with ssh (if you set up ssh, then you won’t have to type your password every time you push things to github). If you use the latter construction, you’ll have to type your github password every time you push to github.
IV. Contribute to other developer’s project in github
If you want to contribute changes to someone else’s repository
- Go to the repository on github. (Say it’s by
myfriend
, and is calledthe_repo
, then you’ll find it athttps://github.com/myfriend/the_repo
.) - Click the “Fork” button at the top right. You’ll now have your own copy of that repository in your github account.
- Open a terminal/shell.
- Type the command, where
username
is your username.
$ git clone [email protected]:username/the_repo
- You’ll now have a local copy of your version of that repository.
- Change into that project directory (
the_repo
):
$ cd the_repo
- Add a connection to the original owner’s repository.
$ git remote add myfriend git://github.com/myfriend/the_repo
- Note the distinction between
[email protected]
andgit://github.com/
.[email protected]
is for the case that you have write access to the repository
git://github.com/ is for the case that whyou only want read access. - Also note the first myfriend does not need to be the same as the username of
myfriend
. You could very well choose:$ git remote add repo_nickname git://github.com/myfriend/the_repo
- To check this
remote add
set up
$ git remote -v
- Make changes to files.
git add
andgit commit
those changesgit push
them back to github. These will go to your version of the repository.
- Note: if you get an error like:
error: src refspec master does not match any. error: failed to push some refs to '[email protected]:username/the_repo'
Then try git push origin HEAD:gh-pages
(see stackoverflow.). Typing git show-ref
can show what reference to put after HEAD.
- Go to your version of the repository on github.
- Click the “Pull Request” button at the top.
- Note that your friend’s repository will be on the left and your repository will be on the right.
- Click the green button “Create pull request”. Give a succinct and informative title, in the comment field give a short explanation of the changes and click the green button “Create pull request” again.
V. Handling Pull Request
If your friend has suggested some changes to your code, ask them to get a github account and follow the instructions above: fork your repository, make the changes, and submit a pull request.
Once they do that, you’ll get an email about it. How to handle it?
(a) Using the github website:
- Go to your version of the repository.
- Click on “Pull Requests” at the top.
- Click on the particular request.
- You’ll see their comments on the pull request, and can click to see the exact changes.
- If you want them to make further changes before you merge the changes into your repository, add a comment.
- If you hate the whole idea, just click the “Close” button.
- If you want to merge the changes into your repository, click the “Merge pull request” button.
- Your github repository will now be fixed, but you’ll want to get them into your local repository, too.
- Open a terminal/shell, and type
$ git pull
Using the command line:
- Open a terminal/shell.
- Go into the directory for your project.
- Add a connection to your friend’s version of the github repository, if you haven’t already.
$ git remote add myfriend git://github.com/myfriend/the_repo
Pull his/her changes.
$ git pull myfriend master
Push them back to your github repository.
$ git push
The pull request on github will be automatically closed.