github 配置

An Illustrated Guide to Git on Windows

About

Note: This guide was written in early 2009. Some parts of itmay be out of date. Good luck!

This document is designed to show that using git on Windowsis not a difficult process. In this guide, I will create arepository, make several commits, create a branch, merge abranch, search the commit history, push to a remote server, andpull from a remote server. The majority of this will be doneusing GUI tools.

Although this guide is targeted for use on Windows, the git gui toolworks the same on all platforms. Because of this, git users on otherplatforms may find useful information here as well.

Downloading PuTTY

Although you can use the SSH program that comes with git, Iprefer to use the PuTTY Agent to keep track of my SSH keys. Ifyou don't already have them, downloadputty.exe,plink.exe, pageant.exe, andputtygen.exe from thePuTTYweb site.

Later in this guide, we will use these programs for securelypushing our changes to a remote server.

Installing Git

First, download msysgit.This download is a single executable which installs the entiregit system. While going through the installer, you will want tocheck the options to add Windows Explorer integration when youright click on a folder.

Because we will be using PuTTY as our SSH client, chooseUse PLink and fill in the path to the downloadedplink.exe executable.

Continue clicking Next until the installation iscomplete.

Creating a Repository

To create a repository, first create the folder you want theproject to live under. Next, right click on the folder andchooseGit GUI Here. Because there is no git repositoryin this folder yet, you will be presented with the git guistartup dialog.

Choosing Create New Repository brings us to the nextdialog.

Fill in the path to your new directory and click Create. Youwill then be presented with the main interface of git gui, whichis what will be shown from now on when you right click on yourfolder and clickGit GUI Here.

Now that the repository has been set up, you will need to tellgit who you are so that commit messages will have the correctauthor. To do this, chooseEdit → Options.

In the options dialog, there are two versions of eachpreference. On the left side of the dialog are options that youwant for this repository only, while the right side contains theglobal options which apply to all repositories. The defaults forthese options are sensible so just fill in the user name andemail for now. If you have a favorite font, you may want to setit now as well.

Committing

Now that the repository has been created, it is time tocreate something to commit. For this example, I created a filecalledmain.c with the following content:

#include <stdio.h>

int main(int argc, char **argv)
{
	printf("Hello world!\n");
	return 0;
}

Clicking the Rescan button in the git gui will causeit to search out new, modified, and deleted files in thedirectory. In the next screenshot, git gui has foundour new file (amazing, I know).

To add the file for committing, click the icon to the left ofthe filename. The file will be moved from theUnstagedChanges pane to theStaged Changes pane. Nowwe can add a commit message and commit the change with theCommit button.

Saying hello to the world is all well and good, but I wouldlike my program to be more personal. Let's have it say hello tothe user. Here's my code for that:

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
	char name[255];

	printf("Enter your name: ");
	fgets(name, 255, stdin);
	printf("length = %d\n", strlen(name)); /* debug line */
	name[strlen(name)-1] = '\0'; /* remove the newline at the end */

	printf("Hello %s!\n", name);
	return 0;
}

I had some trouble figuring out why a newline was printedafter the user's name, so I added a debugging line to help metrack it down. I would like to commit this patch without thedebug line, but I want to keep the line in my working copy tocontinue debugging. With git gui, this is no problem. First,click Rescan to scan for the modified file. Next,click the icon to the left of the filename to stage allmodifications for commit. Then, right click on the debugline and choseUnstage Line From Commit.

Now, the debug line has been unstaged, while the rest of thechanges have been staged. From here, it is just a matter offilling in the commit message and clickingCommit.

Branching

Now let's say that we want to start adding new features forour next big version of the program. But, we also want to keep astable, maintenance version of the program to fix bugs on. To dothis, we will create a branch for our new development. Tocreate a new branch in git gui, choose Branch →Create. The big feature that I would like to add is toask the user for their last name, so I am calling this branchlastname. The default options in theCreate Branch dialog are all fine, so just enter the name andclick Create.

Now that I am on the lastname branch,I can make my new modifications:

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
	char first[255], last[255];

	printf("Enter your first name: ");
	fgets(first, 255, stdin);
	first[strlen(first)-1] = '\0'; /* remove the newline at the end */

	printf("Now enter your last name: ");
	gets(last); /* buffer overflow? what's that? */

	printf("Hello %s %s!\n", first, last);
 	return 0;
}

And then I can commit the change. Note here that I amcommitting using a different name. This is to show off somethinglater. Normally you would always use the same name whencommitting.

Meanwhile, a user informs us that not displaying a comma whendirectly addressing someone is a serious bug. In order to makethis bug fix on our stable branch, we must first switch back toit. This is done usingBranch → Checkout.

Now we can fix our major bug.

If we choose Repository → Visualize All BranchHistory, we can see how our history is shaping up.

Merging

After days of work, we decide that our lastnamebranch is stable enough to be merged into themasterbranch. To perform the merge, useMerge → LocalMerge.

Because the two different commits made two differentmodifications to the same line, a conflict occurs.

This conflict can be resolved using any text editor. Afterresolving the conflict, stage the changes by clicking the fileicon and then commit the merge by clicking theCommitbutton.

Viewing History

The main.c file is starting to get abit big, so I decided to move the user prompting portion of thecode into its own function. While I was at it, I decided tomove the function into a separate file. Therepository now contains the filesmain.c,askname.c,and askname.h.

/* main.c */
#include <stdio.h>

#include "askname.h"

int main(int argc, char **argv)
{
	char first[255], last[255];

	askname(first, last);

	printf("Hello, %s %s!\n", first, last);
 	return 0;
}
/* askname.c */
#include <stdio.h>
#include <string.h>

void askname(char *first, char *last)
{
	printf("Enter your first name: ");
	fgets(first, 255, stdin);
	first[strlen(first)-1] = '\0'; /* remove the newline at the end */

	printf("Now enter your last name: ");
	gets(last); /* buffer overflow? what's that? */
}
/* askname.h */
void askname(char *first, char *last);

The history of the repository can be viewed and searched bychoosing Repository → Visualize All Branch History.In the next screenshot, I am trying to find which commit addedthelast variable by searching for allcommits which added or removed the wordlast. Commitswhich match the search are bolded, making it quick and easy tospot the desired commit.

A few days later, someone looks through our code and seesthat the gets function could cause abuffer overflow. Being the type to point fingers, this persondecides to run a git blame to see who last modified this line ofcode. The problem is that Bob is the one who committed the line,but I was the one who last touched it when I moved the line intoa different file. Obviously, I am not to blame (of course). Isgit smart enough to figure this out? Yes, it is.

To run a blame, select Repository → Browse master'sFiles. From the tree that pops up, double click on thefile with the line in question which in this case isaskname.c.Hovering the mouse over the line in question shows a tooltipmessage that tells us all we need to know.

Here we can see that the line was last modified by Bob incommit f6c0, and then I moved it to itsnew location in commit b312.

Pushing to a Remote Server

Before pushing to a remote server, you must first create aSSH public and private key pair. By using SSH, you will be ableto securely authenticate to the server that you are who you sayyou are. Creating the key pair is a simple process. Begin byrunning theputtygen.exe programdownloaded earlier. Next, click the Generate button togenerate the keys. After processing for a few seconds, clicktheSave private key button to save your new privatekey. Copy the public key to the clipboard in preparation for thenext step. I would recommend not clicking theSave publickey button because the saved file is in a non-standardformat; trying to use it with other software might beproblematic.

Now that the keys are generated, the remote servers need toknow about it. If you would like to usegithub to host your code, justgo to your account page and paste in the public key.

Now github has our public key, but we do not yet havegithub's. To remedy this, launchputty.exe, connect togithub.com, and clickYes toaccept github's public key. You can safely close the loginwindow that opens up after accepting the key.

We need our private key to be loaded up to use with ourpublic key, so launch pageant.exe. Thisprogram will create an icon in your system tray. Double clickingon the icon will open up a window into which the private key canbe added. Once the private key is added, the agent will sit inthe background and provide authentication when needed.

Now that our client and server can authenticate each other,it is time to push!Remote → Push will open upthe push dialog. Typing in the commit address for the projectand clickingPush will send the changes on their way.

Of course, typing in the remote url would become quiteannoying if we had to do it with every push. Instead, git allowsus to alias the long urls using remotes. Git gui currently doesnot have a way to add a remote, so the command line must beused. Right click on the repository folder and choose Git Bash Here.In the prompt, enter the following command:

git remote add github git@github.com:nathanj/example.git

Note: After adding a remote, close and reopen git gui for it to recognizethe new remote.

Now the remote github is aliased tothe url git@github.com:nathanj/example.git. Whenviewing the push dialog in git gui, a convenient drop down listof remotes is shown.

Pulling from a Remote Server

Because our code is so useful, dozens of people havedownloaded and now use our program. One person in particular,Fred, has decided to fork our project and add his own commits.Now that he's added his code, he would like us to pull thosecommits from him into our repository. To do this, first createanother remote.

git remote add fred ssh://fred@192.168.2.67/home/fred/example

Now we can fetch Fred's changes using Remote → Fetchfrom → fred.

After the fetch, Fred's commits have now been added to ourlocal repository under theremotes/fred/master branch. We can usegitk to visualize the changes that Fred has made.

If we like all of Fred's changes, we could do a normal mergeas before. In this case though, I like one of his commits butnot the other. To only merge one of his commits, right click onthe commit and chooseCherry-pick this commit. Thecommit will then be merged into the current branch.

We can now push a final time to send Fred's patch to ourgithub tree for everyone to see and use.

Conclusion

In this guide, I have shown how to do many common tasks ingit using GUI tools. I hope that this guide has shown that it isnot only possible but easy to use git on Windows without havingto use the Windows shell for most operations.

If you have any comments, you can contact me. I don't alwaysrespond to emails though, sorry.



http://matthew-brett.github.io/pydagogue/git_gui_windows.html

http://nathanj.github.io/gitguide/tour.html

http://xiaozhen1900.blog.163.com/blog/static/1741732572012718105259/

http://www.cnblogs.com/cheche/archive/2011/01/07/1918825.html

https://help.github.com/articles/generating-ssh-keys#platform-windows



Getting started with git gui on Windows

Install git for Windows

  • Get a copy of the msysgit .exe installer
  • Double click to install git on your machine
  • Accept all the defaults

A pretend project

For simplicity, let us make a folder for our project in the c:\ drive.

  • Right click on the C:\ drive in Windows Explorer, select “New folder”,name my_idea
  • Right click in my_idea, folder, select New file, of type “Text document”.Name it good_idea.txt.
  • Open good_idea.txt and add some text like “This is my good idea”, andsave.

Putting the project into version control

Now we have a very small project, just a folder with a single file in it. We’llnow put this folder into version control.

Initializing the repository

  • Right click in the my_idea folder, choose “Git Init Here” to initializethe git database:

    _images/git_init_rc.png
  • You may notice that there is now a hidden folder called .git in the my_idea folder.

    _images/dot_git_folder.png

    If you can’t see this hidden folder, you may need to change the folder viewingoptions. From Windows XP explorer this is the “Tools” menu, then “Options”.You should enable viewing hidden folders in the “View” tab:

    _images/folder_options.png

Adding stuff to the repository

Now we have a git database in our folder, we need to add our file.

  • Right click in the my_idea again, choose “Git Commit Tool”

    _images/git_gui_rc.png
  • You will get a dialog like this:

    _images/first_commit_unstaged.png

    Notice that our file is at the top left, and is “Unstaged”. That means that,at the moment, it is not going to be part of any “Commit” we do. A commit iswhere we record changes into the database. We do want to record our file, so

  • Either click on the little icon to the left of the good_idea.txt filenamein the dialog, or go to the “Commit” menu, “Stage to commit” option. Noticethe good_idea.txt file then goes to the “Staged changes (Will Commit)”section of the dialog.

    _images/first_commit_staged.png
  • Add a message in the lower right box, like The first signs of my idea. Thecommit message is a reminder what set of changes the commit has. It’s veryuseful to put meaningful messages here for quick reminders of what youintended with the changes. Now click “Commit”. Congratulations!

Speak with your own voice

Each commit has someone who wrote the contents of the commit - the “author”. Soyou can get all the blame, and all the credit, you can identify yourself to git.That way git can see who’s doing the work (or doing the damage).

  • Right click in “my idea”, choose “Git Gui”.

  • Click on the “Repository” menu and select “Visualize master’s history”:

    _images/gg_repository_menu.png
  • Notice that you are ‘Author: unknown’:

    _images/first_history.png
  • Close the history window, right click and open ‘Git Gui’ again

  • Go to menu Edit - Options. Set your username and and email address in the Right hand panel (global options).

    _images/setting_self.png

    Save. Close.

  • Let’s show ourselves that I have become myself. Make a new fileanother_idea.txt. Start “Git Commit Tool” with a right click, stage thefile by clicking on the icon next to the filename, add a message and the clickcommit.

  • Open Git history from the Git Gui, choose the “Repository” menu, “Visualize master’s history”.Notice that you have two commits now, and the second one has your name on it.

Making a backup

Now you have your changes backed up into your repository, but you will probablywant to back up the repository somewhere.

Let’s pretend to back the repository up to a shared drive (but in fact we’llback up to the C:\ drive).

  • Right click in the my_idea folder, chose “Git Gui”

  • Choose menu “Remote”, “Add”:

    _images/initial_remote_add.png
  • Give a name for your backup (no spaces) and a path to write the backup to.I like to add .git to the folder name for the backup, because this will bea backup with only the repository (the .git subfolder, but not the.txt files):

  • Select “Initialize repository and push”, then click “Add”.

    _images/remote_add_dialog.png
  • If you get “Success” then your files have been backed up to this other folder.

Working from another machine

Now you can work from any computer and send changes back to the backup copy.

We can pretend to do this by making a new copy as if it was on (say) yourlaptop. But in fact we’ll create the folder in the C:\ drive again.

  • Right click on the C:\ drive, choose ‘Git Gui’:

    _images/git_gui_main.png
  • Select “Clone existing repository”. For the ‘Source location’, browse to thebackup that you just made. Type C:\laptop_working for the target directory. Choose“full copy” from the options (for safety). Click on “Clone”.

    _images/clone_dialog.png
  • You now have a c:\laptop_working folder that is a clone of the “my_backup”repository, and that also has the same data as the c:\my_idea folder.

  • Let’s make some changes on the laptop. Go to the c:\laptop_workingfolder. Make a new file idea_on_laptop.txt and type some text in it.Save.

  • Right click, “Git Commit Tool”, stage the idea_on_laptop.txt file, add acommit message and click “Commit”.

    Now you have this contents in your laptop_working folder.

    _images/laptop_directory.png

You put the changes back into the backup by doing a “Push”

  • Right click in laptop_working. “Git Gui”, “Remote” menu, “Push ...”

    _images/laptop_remote_menu.png

    On the next dialog, click “Push”.

    _images/laptop_push.png

    The changes go back to the my_backup.git repository.

Getting changes from a common backup

Let’s pretend that we’ve gone back to our original computer and we want thechanges that we pushed from the laptop.

  • Go back to the my_idea folder. Notice you don’t have theidea_on_laptop file yet.

  • Right click, “Git Gui”, “Remote” menu, “Fetch from” from the my_backupremote.

    _images/gg_fetch_my_idea.png

    Now you have the changes in the repository (the .git subdirectory)- but not in the working tree. Specifically, we don’t have theidea_on_laptop.txt file in the folder yet:

    _images/my_idea_before_merge.png

    To get the file, we need to do a merge. This brings the changes from therepository into the working tree. Open the “Git Gui” if it is not open.Choose the “Merge” menu, “Local Merge” option.

    _images/local_merge.png

    In the dialog, accept thedefaults and click “Merge”:

    _images/merge_dialog.png

    Now you have your file - and you are synchronized with the laptop.

    _images/my_idea_after_merge.png

Review

  • Code states stored so you can
    • See what you’ve done
    • Go back to an earlier state if you make a mistake
    • Send files to and from different computers
  • If you are working with someone else they can add their changes
    • You can see what they did, they can see what you did
    • It’s easy to undo if one of you makes a mistake

This system is powerful, very useful and sometimes confusing. Consult anexpert if you get confused. It is fairly difficult to lose data if you ask anexpert after you have made a mistake or got confused.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值