git - Simple Guide

A simple guide to help you get started with git, no advanced content ;)

Author:Roger Dudler
Acknowledgments:@tfnico, @fhdandNamics
If there are any errors or omissions, pleasegithubReport issue

Installation

Download git OSX version

Download Git for Windows

Download the Linux version of git

Create a new repository

Create a new folder, open it, and then execute
git init
to create a new git repository.

Check out a repository

Run the following command to create a cloned copy of a local repository:
git clone /path/to/repository
If the repository is on a remote server, your command will look like this:
git clone username@host:/path/to/repository

Workflow

Your local repository consists of three “trees” maintained by git. The first is yourWorking directory, which holds the actual files; the second is暂存区(Index), which is like a cache area, temporarily saving your changes; and the last isHEAD, which points to the result of your last commit.

Add and commit

You can propose changes (add them to the staging area) using the following command:
git add <filename>
git add *
This is the first step in git’s basic workflow; use the following command to actually commit the changes:
git commit -m "代码提交信息"
Now, your changes have been committed toHEAD, but not yet to your remote repository.

Push changes

Your changes are now in the local repository'sHEADThere you go. Run the following command to push these changes to the remote repository:
git push origin master
Can putmasterto whatever branch you want to push.

If you haven’t cloned an existing repository and want to connect your repository to a remote server, you can use the following command to add:
git remote add origin <server>
In this way you can push your changes to the added server.

Branches

Branches are used to isolate feature development. When you create a repository,masteris the “default” branch. Develop on other branches, and when finished, merge them back into the main branch.

Create a branch called "feature_x" and switch to it:
git checkout -b feature_x
Switch back to the main branch:
git checkout master
Then delete the newly created branch:
git branch -d feature_x
Unless you push the branch to a remote repository, that branch isNot visible to others:
git push origin <branch>

Update and merge

To update your local repository to the latest changes, execute:
git pull
to ... in your working directoryFetchandMergethe remote changes.
To merge another branch into your current branch (e.g., master), run:
git merge <branch>
In both cases, git will try to automatically merge changes. Unfortunately, this may not always succeed, andConflict (conflicts). At this point you need to modify these files to manually merge theseConflict (conflicts). After you’ve finished modifying, you need to run the following command to mark them as successfully merged:
git add <filename>
Before merging the changes, you can use the following command to preview the differences:
git diff <source_branch> <target_branch>

Tags

It is recommended to create tags for software releases. This concept has existed for a long time, and it is also in SVN. You can run the following command to create a1.0.0the label:
git tag 1.0.0 1b2e1d63ff
1b2e1d63ffis the first 10 characters of the commit ID you want to tag. You can use the following command to obtain the commit ID:
git log
You can also use fewer leading characters of the commit ID, as long as they uniquely identify it.

Replace local changes

If you make a mistake (which, of course, should better never happen), you can use the following command to replace local changes:
git checkout -- <filename>
This command will replace the files in your working directory with the latest content from HEAD. Changes already added to the staging area and new files will not be affected.

If you want to discard all your local changes and commits, you can fetch the latest version history from the server and point your local main branch directly to it:
git fetch origin
git reset --hard origin/master

Useful tips

Built-in graphical git:
gitk
Colored git output:
git config color.ui true
When displaying history, each commit’s information is shown on a single line:
git config format.pretty oneline
Interactively add files to the staging area:
git add -i

Links and resources

Graphical clients

Guides and manuals