From:http://rogerdudler.github.io/git-guide/index.zh.html
Git-A Concise guide
A concise guide to getting Started with git, with advanced content in the wood;)
Roger Doudler
Thanks: @tfnico, @fhd and Namics
Other languages 中文版, Deutsch, Español, Français, Indonesian, Italiano, Nederlands, Polski, Português,русский, Türkçe,
??????, Japanese,??? Vietnamese
If there is a mistake, please quote a question on GitHub
Installation
Download git OSX version
Download git Windows edition
Download the git Linux version
Create a new warehouse
Create a new folder, open it, and then execute
git init
To create a new git repository.
Check out the Warehouse
Execute the following command to create a cloned version of the local repository:
git clone /path/to/repository
If this is the repository on the remote server, your command will look like this:
git clone [email protected]:/path/to/repository
Work flow
Your local repository consists of three "trees" maintained by git. The first one is yours 工作目录
, it holds the actual file; the second is 暂存区(Index)
that it's like a cache area that temporarily saves your changes, HEAD
and finally, it points to the result of your last commit.
Add and submit
You can make changes (add them to staging area) by using the following command:
git add <filename>
git add *
This is the first step in the Git basic workflow, using the following command to actually commit the changes:
git commit -m "代码提交信息"
Now, your changes have been submitted to HEAD, but not to your remote repository.
Push changes
Your changes are now in the HEAD of the local repository. Execute the following command to commit the changes to the remote repository:
git push origin master
You can replace master with any branch you want to push.
If you have not cloned an existing repository and want to connect your warehouse to a remote server, you can add it using the following command:
git remote add origin <server>
So you can push your changes to the server you added.
Branch
Branches are used to insulate the development of features. When you create a warehouse,master is the "default" branch. Develop on other branches, and then merge them onto the main branch when you are finished.
Create a branch called "feature_x" and Switch to the past:
git checkout -b feature_x
Switch back to the main branch:
git checkout master
Then delete the new branch:
git branch -d feature_x
Unless you push the branch to the remote repository, the branch is not visible to others :
git push origin <branch>
Update and merge
To update your local repository to the latest changes, do the following:
git pull
To get (fetch) in your working directory and Merge (merge) remote changes.
To merge other branches into your current branch (for example, Master), execute:
git merge <branch>
In both cases, Git will try to automatically merge the changes. Unfortunately, this may not be successful every time, and there may be conflicts (conflicts). You will need to modify these files to manually merge these conflicts (conflicts). After you've changed, you'll need to execute the following commands to mark them as merged successfully:
git add <filename>
Before merging the changes, you can preview the differences using the following command:
git diff <source_branch> <target_branch>
Label
Creating labels for software releases is recommended. This concept is already in existence and is also available in SVN. You can create a label called 1.0.0 by executing the following command:
git tag 1.0.0 1b2e1d63ff
1b2e1d63ff is the first 10 characters of the commit ID you want to tag. You can get the commit ID using the following command:
git log
You can also use a little less commit ID before several, as long as it's pointing is unique.
Replace local changes
If you make a mistake (of course, this should never happen), you can replace the local change with the following command:
git checkout -- <filename>
This command replaces the files in your working directory with the latest content in the HEAD. Changes that have been added to staging area and new files are not affected.
If you want to discard all your local changes and commits, you can get the latest version history on the server and point your landlord branch to it:
git fetch origin
git reset --hard origin/master
Practical Tips
Built-in graphical git:
gitk
Color git output:
git config color.ui true
When the history is displayed, only one row is displayed for each submitted message:
git config format.pretty oneline
Interactively add files to staging area:
git add -i
Linking and resource graphical clients
- GITX (L) (OSX, open source software)
- Tower (OSX)
- Source Tree (OSX, free)
- GitHub for Mac (OSX, free)
- Gitbox (OSX, App Store)
Guides and Manuals
- Git Community Reference Book
- Professional Git
- Think like Git.
- GitHub Help
- Diagram Git
Comments
Common git operations