A concise guide to git

Create a new repository

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

Check out the warehouse

Execute the following command to create a clone of the local repository:
git clone /path/to/repository
if it was a repository on a remote server, your command would look like this:
git clone username@host:/path/to/repository

Workflow

Your local repository consists of three "trees" maintained by git. The first is yours 工作目录, which holds the actual file; the second is 暂存区(Index)like a cache area that temporarily holds your changes; and the last is HEAD, it points to the result of your last commit.

add and submit

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 the basic git workflow; use the following command to actually commit the changes:
git commit -m "代码提交信息"
Now, your changes have been committed to HEAD , but not yet to your remote repository.

push changes

Your changes are now in the HEAD of the local repository. Execute the following command to commit these changes to the remote repository:
git push origin master
replace master with 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 add it using the following command:
git remote add origin <server>
so you can push your changes to the added server.

branch

Branches are used to insulate feature development. When you create a repository, master is the "default" branch. Develop on other branches and merge them into master when done.

Create a branch called "feature_x" and switch it:
git checkout -b feature_x
switch back to the master branch:
git checkout master
delete the new branch:
git branch -d feature_x
unless you push the branch to the remote repository, the branch is invisible to others :
git push origin <branch>

Update and merge

To update your local repository to the latest changes, execute: to fetch and merge the remote changes in
git pull
your working directory . To merge other branches into your current branch (e.g. master), do: In both cases, git will try to merge changes automatically. Unfortunately, this may not be successful every time and conflicts may arise . At this time, you need to modify these files to manually merge these conflicts (conflicts) . After making changes, you need to execute the following command to mark them as merged successfully: Before merging the changes, you can preview the differences with the following command:

git merge <branch>

git add <filename>

git diff <source_branch> <target_branch>

Label

Creating tags for software releases is recommended. This concept has existed for a long time, in SVN as well. You can create a tag called 1.0.0 by running the following command :
git tag 1.0.0 1b2e1d63ff
1b2e1d63ff is the first 10 characters of the commit ID you want to tag. The commit ID can be obtained using the following command:
git log
You can also use a lesser number of first digits of the commit ID, as long as it points to something unique.

log

If you want to know the history of the local repository, the easiest command is to use:
git log
you can add some parameters to modify its output to get the result you want. Just look at one person's commits:
git log --author=bob
a compressed output that takes only one line for each commit:
git log --pretty=oneline
or you want to display all branches in an ASCII art tree structure, each marked with his name and label :
git log --graph --oneline --decorate --all
See which files have changed:
git log --name-status
These are just a small subset of the parameters you can use. For more information, refer to:
git log --help

replace local changes

If you make a mistake (of course, this should never happen), you can replace the local changes with the following command:
git checkout -- <filename>
This command will replace the files in your working directory with the latest content in HEAD. Changes that have been 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 go to the server to get the latest version history and point your local master branch to it:
git fetch origin
git reset --hard origin/master

Practical Tips

Built-in graphical git:
gitk
Colored git output:
git config color.ui true
When displaying history, only one line of information per commit is displayed:
git config format.pretty oneline
Interactively adding files to the staging area:
git add -i

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326267821&siteId=291194637