about using the GIT version system in Visual Studio [go]

Source: Internet
Author: User
Tags git commands

This tutorial is expected to hope that no version of the use of the basic novice can also grasp, so more details, improper, welcome correction.

First, install the Git development tool

If you want to use Git for versioning, it's really enough to use git command-line tools, and graphical tools (whether Git extentions or tortoisegit) are just a command-line package. As far as functionality is concerned, all they can do, command line can do, but the command line can do, they do not necessarily can do. The command line is more primitive and natural, spanning platform, inordinately. It is recommended that you familiarize yourself with the git command line tool.

But graphical tools also have their own advantages, is intuitive. The following recommended combination scenarios are:

git command line (cygwin) + git Extensions + git Source Control Provider

-This combination can completely solve the problem of Chinese files (including Chinese file name, directory name, and normal display in GitHub).

1) Installing the Cygwin command-line tool

Installation tutorial See http://gotgit.github.com/gotgithub/10-appendix/030-install-on-windows-cygwin.html

Software that needs to be installed during the installation:

    • Git-completion: Provides automatic completion of GIT commands. Installing the package will automatically install the dependent bash-completion software package.
    • OPENSSH:SSH client that provides Git access to the SSH protocol repository.
    • Vim: is the Git default editor.

2) Install Git extensions

Role

Git extensions must depend on the command-line tool, so the second one installs.

It can be used not only after installation, but also automatically add plugins in VS, which can be very intuitive and easy to operate in vs.

"Installation Method"

Download URL: http://code.google.com/p/gitextensions/downloads/list Choose the version with the complete typeface in Gitextensions226setupcomplete.msi, which integrates the KDIFF3 tool.

After downloading, double click Install, all the way next, remember to tick msysgit and KDIFF3.

Git extensions comes with a comprehensive tutorial after installation.

Configuration

We know that there are currently two git command-line tools, one is Cygwin command line, one is the msysgit command line, git extensions can be configured to use which command line tool, for example, we choose to use Cygwin instead of msysgit

The first one is: "The user runs Git commands"

The second is: Global profile, please click the "Change HOME" button, the directory to Cygwin directory.

In addition, GIT extensions will change the Cygwin default editor VI to its own, and you can change it back in "Global Settings".

Supplemental 1:git extensions configuration files are placed in the registry, if uninstalled, these configurations remain, you can install the problem again, you can check the registry for related configuration items, whether there is a problem.

Add 2: In the process of installing Git extensions, we checked the Msysgit, now installed, you can actually uninstall Msysgit, but you can also keep, because msysgit installation directory with git all the commands of the help document.

3) Install Git Source Control Provider

Role

Git Source Control Provider is an extension of VS, but it has to rely on git extensions, so the third installation, which can show file status, display file history, and so on, makes the file operation more convenient in vs.

Project website: http://gitscc.codeplex.com/

Source: Https://github.com/yysun/Git-Source-Control-Provider

Video Tutorial: www.youtube.com/watch?v=efS0kKvfi6k

"Installation Method"

In the VS menu, click on "Tools" extension Manager

Then search for git online, the first result is git Source Control Provider, and then click "Download"

Then click "Options" in the "tools" of the VS menu, expand "Source control" on the left, select "Git source Control Provider" in "Current source control plug-in" on the right, then click "OK"

After you enable Git Source Control Provider, all files display a status icon. You can right-click on the file to go to the Git menu.

The following is a demonstration of how to visualize the Git repository in VS by creating a new test project. If you do the basic work, you can participate in the actual GitHub project, such as "I record the development framework" and "I record the website Integrated system (integrated with" sns/Portal cms/Forum/blog/album/Weibo/wiki "applications such as Internet products)" has been hosted on GitHub, URL is Https://github.com/wojilu/wojilu.

Second, create the version library

Create a new project with VS, then click "Initialize New Repository" in the "Git" menu

Pop-up window lets you choose the directory you want to include in GIT version management--

After initialization, in the project directory, a. Git hidden folder appears, and this folder is the Git repository.

All files in the same directory as. Git are now included in the scope of this repository.

Now that you are in this directory, you can execute the git command.

For example, the following is the git command in the command line window directly manipulate GIT commands, using the command line, you can complete all GIT version management operations.

However, our section is mainly about how to visualize git in VS, which is not involved in command-line management for the time being.

Iii. submitting files to the repository

Only by incorporating the files in the project into the repository can we enjoy the benefits of a version management system, such as browsing historical versions, switching branches, etc...

1) First, turn off vs and then turn it back on. If you do not reopen, the plugin Git Source control cannot monitor the file status.

After reopening, the file displays the status symbol: the "+" plus sign appears before, which indicates that the status of these files is "not yet included in the repository" and needs to be added.

You can submit by clicking "Commit" in the "Git" menu.

The following submission window appears

We see that there are a lot of files that need to be submitted here, but in fact, a lot of files are auto-generated temporary files, and the project content is not related, should not be included in version management, such as the ***.suo file, we need to set these files as "need to ignore files", let git ignore them.

2) Set Ignore file

Please close the Commit window above and click "Edit. Gitignore" in the "Git" command in the VS menu.

An edit window appears ignoring the file, and the content area on the left side of the window is currently blank and has no content.

However, the right side lists some common files that need to be ignored (the default), click the "Add Default Ignore" button on the lower right, add the content you want to ignore to the left content area, then click "Save".

Then open the folder and you'll see a file called. Gitignore in the project, and the content is what you just saved.

3) Submit step A (join staging area)

Click "Commit" in the menu "Git" to submit the command, now the submission window shows that the file needs to be submitted greatly reduced, such as the ***.suo file has been ignored by Git.

At this point, if you click the "Submit" command, the following window appears, meaning "There are no files in staging area." Need to be staged and submit all files right away? "

What does that mean? Here's a staging area concept of git that has three files in git:

1) working directory, which is the directory and files you operate on the hard disk;

2) Git repository, that's what you said earlier. Git hides the folder. The repository is also divided into "staging area" and the real repository.

A) the staging area (stage) is actually equivalent to a buffer before submission;

b) Repository content area, which contains the history of the file, various branches, etc...

In summary, submitting a file to the repository is actually the process--

1. Working Directory--(2. Staging Area--3. Repository)

Why does Git need a staging area? This is to facilitate the commit process of the revocation of the undo and other fine operations. Once you have joined staging area (stage), you have joined. Git, but have not yet committed to the repository, with this cache, the commit process becomes more flexible.

In summary, your submission process is divided into two steps: Add to Staging Area (stage), then commit (commit). If you use the command line, it is

First step: Git add fileName

Step two: Git commit-m "submit descriptive text"

The two steps correspond to the left side of the two windows, the upper left is the working directory, the lower left is staging area, two windows separated by the "staging" command, such as.

You can select a file, click the arrow on the left side of the way, add the file to staging area, or click the double arrow on the right to add all the files to the staging area.

This double-arrow command, in fact, is the same as the following add Files command (right-click into the Git menu in the project), all of which are added to staging area. Special Note: If the file name or directory name is in Chinese, use the ADD Files command instead of the stage operation here.

4) Submit step B (Real commit commit)

After all the files have been added to staging area, as shown, please enter the submission instructions in the lower right window, please fill in carefully, do not leave blank. Each submission should have a clear explanation, which is a key clue for future version browsing and management.

After completing the "Submit information (M)", you can click the "Submit" button, then the following window will appear, indicating the success of the submission.

The above operation is very intuitive, but if you use the git command line, you can simply have two simple lines of command:

git Add.

Git commit-m "My first Commit"

Both ways have advantages and disadvantages.

At this time we look at the status of the files in the project, the "+ Plus" in front of the file becomes "lock", indicating that the file has been successfully incorporated into the repository.

Iv. Browsing the Repository

1) To prepare the work, we add 2 files in the version and then Submit 2 times.

2) How can I view the previous submissions? Open the "Browse" command in the "Git" menu

The Submit History window appears--

The window shows a total of three commits, in the Commit history right-click, you can see related actions, such as branch, tag, etc...

If you right-click in the project and then hit "Git-history" in the context menu

You can also view the commit history graphically

V. Synchronizing to a remote server

Because Git is a distributed version of the system, everyone has a copy of the Git repository on their own natively, and synchronization is required to synchronize with the remote other repository.

There are two kinds of synchronization operations, one is pull, and the other is push push.

Below, we push the project to GitHub on our registered project.

1) We click on the "Push" command

A push window appears, then click "Manage Remote" to add the GitHub project--

The Remote administration window appears, please fill in

"Name",

"URL address" (usually [email protected]:yourname/yourproject.git format),

Browse for "Private key File" (the. ppk file you generated with PuTTY),

"Load SSH key",

Then test the connection and the last "save"

The system starts to crawl the remote information, after successful, switch to "default pull behavior (get and merge)", in the right "remote archive" drop-down box select "origin", "default merge" fill in "Master", and finally click "Save"

2) Close the "Remote Archive" window, close the "push" window and reopen (that is, restart the "push" window), and you will find that the Gihub remote library that you just set up has been automatically populated

Click on the "Push" command, start the push, depending on the speed of the different, it may take a few seconds or a few minutes, please wait patiently, the end will prompt push success.

Vi. Document Management

"Delete File"

In VS, because there are plug-in support, so can be deleted directly, after deletion, you need to submit. Git extensions will synchronize the delete operation to the repository at the time of submission.

For example, after the file "XxxFile.cs" is deleted, the delete tag "-" is displayed in the commit window:

Note: If you are on the command line, do not use the RM operation directly, but instead use the git RM command.

"How can I see the files that have been deleted?"

Click the "Browse" command on the "Git" menu to open the Commit History window, click on a commit that has not been deleted, such as "add Class1" below, and then switch the following window to the "File tree"

In the number of files, you can view all the files in the current history.

Alternatively, you can click the "git-history" command in the right-click menu of the project to view the number of files in the corresponding "Show files" window.

"How do I recover a deleted file?"

In the, locate the file that you want to recover, and then right-click Save As to the project.

"Move Files"

1) move directly in vs.

2) If the file being moved is a Chinese name, the GIT extensions dialog will have a bug and need to use the command line tool. Please open the command line, enter the directory where the. git file is located, enter the command--

Git add-u.

This command, more than the normal "git Add." More than a parameter-U, means to add a variety of changes in the

"How do I recover the changes I just made?"

In the Submit window, select the file, click "Reset Selected Files", or "reset changes" in the lower right corner

Or, in the right-click "Git", "Undo File changes"

"Modify last Commit information"

Sorry about the submission? Want to change the last commit?

Add a commit and click on "Change Last commit" above, and the new submission will overwrite the last commit.

Warning: This method can only be used when the last commit has not been push to the remote repository, otherwise the modification of the last commit will cause confusion.

VIII. Branch Management

In git, branching is very easy to use and it is recommended to create branches frequently.

For example, you have an experimental idea that you can create a branch to test without affecting the formal code at all.

For example, to develop a new feature, create a branch that specifically submits this feature-related code, and if it is completed, merge the branch into the main branch. This new branch is called "feature branch (Feature Branch)". One important advantage of this is that if the new work can be completed on schedule, it will not affect the release of the main branch.

In addition to bug fixes, Git's branching function is also very easy to use. It can be said that branching is one of the best features of Git.

1) Create a branch

Fill in the "Branch name" and click "Create Branch"

After creation, the solution Manager (solution Explore shows the current branch, such as display branch1)

2) Switch Branches

Note: Before switching, remember to commit to ensure that files that have not yet been submitted have been saved in the repository.

Click on the "Checkout Branch" command in the Git menu,

Then select the branch you want to check out:

3) Merging branches

Click on the "Merge" command

Then select the branch you want to merge:

After merging, the contents of the target branch appear in your current branch.

4) Delete Branch

Because it is easy and frequent to create branches in git, it is often necessary to delete a branch when it is not useful. Note that if the branch has not been merged, deleting the branch causes all commits under this branch to be lost, so merge the branches first before deleting them.

Method: Click "Browse" in the "Git" menu, go to the Git log list, right click on the "Delete branch" command on the top of the branch tag:

5) Branch "Yan" (rebase, also known as "variable base")

To rebase a branch on master, the following steps are performed:

    • All commits in this branch will stash the temporary cache;
    • then delete this branch;
    • Re-create this branch on top of the master branch;
    • Commit all the commits that were just cached on the newly created branch;

During the rebase process, it is possible to merge conflicts, which often requires you to solve them manually.

Rebase method: First enter the branch that needs to be rebase, then click on "The" command, select the branch after rebase on in the pop-up window, for example, select the master branch in the branch branch1, which means the branch1 is added to master (rebase on Master).

Eight, Tag management

In git, tag (tag) is often used to mark a version, such as a "v1.0" tag for a commit, to indicate that this is version 1.0.

1) Create tag

Through the "Browse" command on the "Git" menu, go to the Commit List window and you can right-click on the "Create new Tag" command on any commit (commit), e.g. enter "v1.0"

When creating, it is advisable to tick "create annotated Tag", as shown in, and fill in the "message". Because you play the tag, others may not understand its meaning and purpose, annotated tag more convenient to work together. In addition, spaces are not allowed in tag names.

2) Delete tag

Tag can not be modified and moved, so if you want to modify or move, please delete first, then add a new tag.

Delete method: Right click on tag to "delete tag".

3) Tag push

When pushing data to a remote server push, by default, the tag will not be pushed, you need to go to the "Push Tag" (as shown) and then tick "force push" to push the tag up.

Note: Once the tag is pushed to the remote server, do not change it because it is possible to cause the same tag to be different in different users, causing confusion.

4) Download available on GitHub

Github is very thoughtful, as long as the tag, are automatically packaged, to provide the download. For example, I put the "v1.8.5" tag on the first commit after hosting Wojilu, and on GitHub, the other person can download the version directly.

After downloading, it can be debugged and run with vs2010 open.

Ix. merger of conflicts

In the case of branch merging, content conflicts often occur, and in most cases git will help you merge automatically, eliminating a lot of hassle.

But sometimes, git can't handle it, and you need to solve it manually.

There are generally three types of conflicts that you need to handle manually:

One is deleted, one is modified to prompt you: Select the modified file, or delete the file?

One is deleted, a new creation will prompt you: Select the created file or delete the file?

A different change in two branches will prompt you: Manually modify the conflict using the Merge tool

Here's a brief look at how to use the merge tool KDIFF3.

For example, in a branch merge, if there is a conflict, there will be pop-up window reminders

Click "Yes" and the following Merge tool window appears, please select "Open in KDIFF3" on the upper right corner.

KDIFF3 a pop-up window will appear when you start.

Please click "OK" and the main window of KDIFF3 appears.

There are four windows on the screen, the top three, then the Base/local/remote window, the following is the Output window

A window base needs to merge the most recent common ancestor version of the two files

B window Local Current branch file

C window remote need to merge other branches of the file

Files after the output is merged

Merge method: In the conflict area in the output file, right-select the corresponding content of a/b/c.

After selection, if you are not satisfied, you can also edit the input directly in the conflict area

After you modify, save and exit.

Note: It should be noted that some conflicts are logical conflicts, are implicit, tools cannot be detected automatically. For example, if you modify the return value of a function to make his meaning change, and others use it, the other person's program may not work properly after you modify it, but the tool does not find this logical conflict. This, of course, is not the problem of merging tools, it requires the careful and thoughtful thinking of the programmer.

about using the GIT version system in Visual Studio [go]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.