Using Git and Remote code libraries

Source: Internet
Author: User

From: http://blog.jobbole.com/53573/

Work steps I will show you the following steps, usually helping me to do projects on one or more machines on my own.
    1. Create a remote, empty code base (on BitBucket)
    2. Add a project to the local code base
    3. Develop new features on the branch
    4. A) keep new features or b) discard them
    5. Perhaps, back to an earlier point in time
    6. To push the local code base to the remote code base
    7. Get the remote code base on another machine

Installing Git on most *nix systems (Linux, OS X), Git is already installed. You can update it to the latest development version (not recommended) via GIT itself by sending the following command. git clone https: //github .com /git/gitOn Windows, you can download the Git installer here. If you really need to install programs for other systems, MAC OS x installation files here, Linux operation instructions are here.

Create a remote code base many people like to use GitHub. I personally prefer BitBucket because it provides an unrestricted private code base, which is what I need most. You can convert the following instructions to GitHub, which are the same process. So, go to www.bitbucket.org and sign up for an account. Once done, log in and click on the "Create" button at the top. Check the private code base by filling in the form. You don't want anyone else to spy on your Facebook killer app's source code, right? You can leave BitBucket now, we've got everything we need there.

Setting up Git we need to make a one-time configuration before we can work with Git. In order for Git to track who made the changes, we need to set up your username. I strongly recommend that you use the same username and email address as your registered BitBucket account. To send these commands, replace the "your_username" and "[email protected]" (note the quotation marks) accordingly: git config --global user.name "your_username" git config --global user.email [email protected]We will also set the default value for push to be ' simple '. To understand what this means, quickly read the default values (not required) that I posted earlier about push. Send this command: git config --global push.default simpleWe're all set up. You don't need to repeat these configurations on your machine, but if you work on another machine, don't forget the configuration. If you forget to do the initial configuration, Git will not allow you to submit anything, which will bother you.

Create a local code base as an example, we will pretend that we have a website (no technology) that exists in the ' My_site ' folder of the ' workspace ' folder on our machine. On the command line, go to the root folder of your site. On OS X and Linux: cd ~ /workspace/my_site/On Windows: cd c:\workspace\my_siteWe first need to tell Git that this folder is the item we need to track. So we send this command to initialize a new local git code base git initGit creates a hidden folder in the My_site folder called. Git, which is your local code base.

Loading (stage) files We now need to command git and we need to load all the project files on (stage). Send: git add .The last "." The symbol means "All files, folders, and subfolders". If we just want to add specific files to the source control, we can specify them: git add my_file, my_other_file

Submit file Now, we want to commit the loaded (staged) file. Read "Add a point in time where your files are in a restorable state". When we submit our documents, we always attach meaningful annotations that describe their present state. I always use "initial commit" as the first comment to commit. git commit -m "initial commit"That's it. You can now roll back to this commit state at any time. If you have a need to check the status of your current loaded (staged) and non-loaded (unstaged) files, submit, etc., you can ask Git for status: git status

Creating branch branching is the act of creating a standalone version of your code, independent of your trunk branch. By default, every file you commit to Git will be stored in the "Master (Trunk)" Branch. Now let's say you want to add a feature to the project, but you want to be able to roll back to the current version in case something goes wrong, or you decide to give up the feature. This is the time for you to create a branch. Create and switch to your new branch at the same time, send: git checkout -b new_featureAlternatively, you can create a branch and then switch manually, just like this: git branch new_featuregit checkout new_featureTo see all the branches under your current project, send this: git branchNow you can do whatever you want to do on your project: At any time, you can go back to the state you were in before you created the branch. Note that you can have multiple branches at the same time, and you can even create a branch from a branch.

Merge branch When you are satisfied with your new function, you want to add it to the trunk branch. When you are on your new feature branch, you first need to load (stage) and submit your file: git add .git commit -m "adds my new feature"Then you move to your trunk branch: git checkout masterMerge like this: git merge new_featureAt this point, your trunk branch will become the same as your new feature branch.

Discard branches Instead, if you are going to discard the changes you made in the branch, you first need to load (stage) your file and submit it in the branch: git add .git commit -m "feature to be discarded"Then, you move to the trunk branch: git checkout masterNow your code is in the state before you created the branch.

Delete a branch If you want to merge your branch into the trunk branch, send it from the trunk (master) Branch: git branch -d new_featureIf the changes have been merged, it will only delete the branch. If the branch is not merged, you will get an error message. Delete an unincorporated branch (usually you don't want to keep the modifications), you need to send the same command with an uppercase d. It means "force Delete branch, anyway I don't want it anymore." ”: git branch -D new_feature

Rollback to the previous commit state at some point, you may want to go back to the previous code version. First, you need to find out which version you want to go back to. To see all the completed commits, send: git logThis will output the history of your commits, like this: commit ca82a6dff817ec66f44342007202690a93763949Author: your_username [email protected]:   Mon Nov 4 12:52:11 2013 -0700    changes the frontpage layout commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7Author: your_username [email protected]:   Mon Nov 4 11:40:33 2013 -0700    adds my new feature commit a11bef06a3f659402fe7563abf99ad00de2209e6Author: your_username [email protected]:   Mon Nov 4 10:37:28 2013 -0700    initial commitIf you want to go back to "adds my new feature" This commit, simply check out with the submitted ID (checkout) (I usually only use 9 characters from the beginning of the ID) git checkout 085bb3bcbYou can also check out a new branch, like this: git checkout -b my_previous_version 085bb3bcbJust don't be so crazy! The more complex your branch, the more difficult it is to determine what you are really doing.

Push to remote code base the first time you want to push a local code base to a remote code base, you need to add it to your project configuration. Do it like this: git remote add origin https: //your_username @bitbucket.org /your_username/name_of_remote_repository .gitNote that the "origin" here is just a habit. It's an alias for your remote code base, but you can use any other word you like. You can even have multiple remote code libraries, you just need to give them different aliases. After that, you want to push the trunk branch of your local code base to your remote code base: git push origin masterIf you use BitBucket, at this point you will be asked to enter your password. As you do, your local code base will be pushed to your remote code base.

Get a local copy of the remote code base if you don't have a local version of the remote code base (for example, if you start working on another machine and you haven't used the project on this machine), you need to copy it first. Go to the folder where you want to copy the code base and send it: git clone https: //your_username @bitbucket.org /your_username/name_of_remote_repository .gitOn the other hand, if you have already worked on a local project, just want to get its latest version from the remote code base, move it to the root of the project and send it: git pull origin master

Alias git allows you to create shortcuts (aliases) for the commands you use most often. For example, if you don't want to enter Git commit-m "some comment" every time, but instead enter git C "some comment", you can add an alias to your git global configuration, like this: git config --global alias .c ‘commit -m‘Here is the list of aliases I used: git config --global alias .c ‘commit -m‘ git config --global alias .co ‘checkout‘ git config --global alias .cob ‘checkout -b‘ git config --global alias .br ‘branch‘ git config --global alias .m ‘merge‘ git config --global alias .a ‘add .‘ git config --global alias .s ‘status‘ git config --global alias .dbr ‘branch -d‘

Further of course, there are more git content than these. If you want to learn more about git, I recommend official documentation and tutorials that you can find in http://git-scm.com/documentation.

Using Git and Remote code libraries

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.