Learning materials:
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
Http://codingnow.cn/version/212.html
http://blog.jobbole.com/34503/
Http://www.cnblogs.com/lwzz/archive/2013/02/23/2921426.html
Git is a centralized version-control system. Install on Mac: Install Xcode,xcode directly from AppStore git, choose Menu "Xcode", "Preferences", find "Downloads" in the pop-up window, select "Command line Tools", Click "Install" to complete the installation.
1. Repository also known as the Warehouse, English name repository, you can easily understand a directory, all the files in this directory can be managed by git, each file modification, deletion, git can track, so that any time can track history, Or you can "restore" at some point in the future.
- Initialize a git repository, using
git init
commands.
- Update a git repository to add files to the Git repository in two steps: (Add to Staging Area (stage), commit to Workspace)
-
- The first step, use the command
git add <file>
, note, can be used repeatedly, add multiple files;
- The second step, using
git commit
the command, is done. The -m “xxx”
instructions for this submission. Git tracks and manages changes, not files, and is git commit
only responsible for submitting staging area changes.
- View the warehouse status, which changes from the last updated version.
- git status which files have changed (not put to staging area also shown). Once submitted, if you do not make any changes to the workspace, then the workspace is "clean".
- Git diff <file> What's changed.
- View the history of the warehouse update, and the
git log
command displays the commit log from the most recent to the farthest, and you can see 3 commits.
- To the specified version:
HEAD
to represent the current version,HEAD^是上一个版本,HEAD^^是上上一个版本
,HEAD~100往上100个版本。
git reset--hard head^ fallback to previous version "add distributed"
-
- Go to a version with a version number (hash ID). The version number is not necessary to write the whole, the first few can be, git will automatically go to find.
git reset--hard 3628164
-
- git provides a command
git reflog
to keep track of every command you make. This allows you to find the version number back to "future versions".
Ps:git version fallback is very fast because git has a pointer to the current version inside, and HEAD
when you roll back the version, Git simply modifies the head and updates the workspace.
- Git checkout--<file> Let this file go back to
git commit
git add
the last state or time. No --
, it becomes the "Create a new branch" command
- As with ADD, RM deletes a file and commits to take effect. Undo Delete Can be used checkout--
2. Remote Storage
Only one machine has an original repository, and since then, other machines can "clone" the original repository, and each machine's repository is actually the same, and there is no primary and secondary points.
Find a computer to play the role of the server, 24 hours a day, everyone else from the "server" to clone a copy to their own computer, and each to push their own submissions to the server warehouse, but also from the server warehouse to pull someone else's submission.
Because the transfer between your local git repository and the GitHub repository is encrypted via SSH, a bit of setup is required:
- Create an SSH Key.
- Login to GitHub to add SSH Key.
1) Use remote add to associate the contents of the local repository to the remote repository (at least create an empty to remote repository first).
$ git Remote add origin [email protected]:<git account name >/< Local warehouse name >.git
Once added, the name of the remote library is the origin
default for Git.
Use the command push to push all the contents of the master branch for the first time, and then push does not need to add-U.
Git push-u Origin Master
2) A remote library is first, then cloned from the remote library.
git clone [email protected]:<git account name >/< Remote Warehouse name >.git
The address can also be an https://github.com/michaelliao/gitskills.git
address that supports SSH and HTTP two protocols.
3. Branch
What is the use of branching in practice? Suppose you are ready to develop a new feature, but it takes two weeks to complete, the first week you write 50% of the code, if submitted immediately, because the code is not finished, the incomplete code base will cause others can not work. There is a huge risk of losing your daily progress if you wait until the code is all written and then submitted again.
Now that you have a branch, don't be afraid. You create a branch of your own, others do not see, but also continue to work on the original branch, and you work on your own branch, you want to submit the submission, until the development is complete, then once merged into the original branch, so that it is safe, and does not affect the work of others.
1)
- Each node is a commit, linked with a timeline, the branch is actually a pointer to the node, creating, switching, merging, deleting branches and so on is actually the operation of pointers.
git checkout-b feature1 //Create a new branch feature1 and switch git checkout master //Switch back to Maseter
- When git cannot merge branches automatically, you must resolve conflicts first. After resolving the conflict, submit again, and the merge is complete.
git status
can tell us the conflicting files. You can view conflicting text directly, and git uses <<<<<<<
it to =======
mark the >>>>>>>
contents of different branches.
git merge feature1git status
-M "Conflict fixed"
git log--graph--pretty=oneline--abbrev-commit //with parameters git log
you can see the merge of the branches git branch-d feature1//Delete branch FE Ature1
Merging branches, plus --no-ff
parameters can be combined with normal mode, the merged history has branches, you can see that there has been a merger, and the fast forward
merger will not be seen to have been merged.
Git merge--no-ff-m "merge with No-ff" Dev
First, the master
branch should be very stable, that is, only to release the new version, usually do not work on it;
So where do you work? The work is on the dev
branch, that dev
is, the branch is unstable, to some point, such as the release of 1.0, then the dev
branch to merge master
, in the master
Branch Release 1.0 version;
You and your friends each work on the dev
branch, everyone has their own branch, and occasionally merge on the dev
branch.
-
When fixing a bug, we fix it by creating a new bug branch, then merging and finally deleting it, and when the work is not done, first put the job on the spot git stash
, then fix the bug, fix it, and go back to the git stash pop
job site.
To develop a new feature, it is best to create a new branch; If you want to discard a branch that has not been merged, you can remove it by git branch -D <name>
force.
2) interaction with the remote repository about the branch
View Remote library information, use git remote -v
;
Local new branches are not visible to others if they are not pushed to the remote;
Push branches from local, use git push origin branch-name
, if push fails, first use git pull
crawl remote new commit;
-
master
The branch is the main branch, so synchronize with the remote at all times;
dev
Branch is the development branch, all members of the team need to work on it, so also need to synchronize with the remote;
- Bug branches are only used to fix bugs locally, there is no need to push remote, unless the boss wants to see how many bugs you have fixed every week;
- Whether or not the feature branch is pushed to the remote depends on whether you are working with your little partner to develop it.
Local and remote branches corresponding to the branch, use git checkout -b branch-name origin/branch-name
, local and remote branch names are best consistent;
Establishing the Association of local and remote branches, using git branch --set-upstream branch-name origin/branch-name
;
From the remote crawl branch, use git pull
, if there is a conflict, you must first handle the conflict.
Manual:
- git help < commands > can see doc.
- The most commonly used git commands is:
Add add file contents to the index
Bisect Find by binary search the introduced a bug
Branch List, create, or delete branches
Checkout checkout a branch or paths to the working tree
Clone clone a repository into a new directory
Commit Record changes to the repository
Diff Show Changes between commits, commit and working tree, etc
Fetch Download objects and refs from another repository
grep Print lines matching a pattern
Init Create an empty Git repository or reinitialize an existing one
Log Show Commit Logs
Merge Join or more development histories together
MV Move or rename a file, a directory, or a symlink
Pull Fetch from and integrate with another repository or a local branch
Push Update remote refs along with associated objects
Rebase Forward-port Local commits to the updated upstream head
Reset Reset Current HEAD to the specified state
RM Remove files from the working tree and from the index
Show show various types of objects
Status Show the working tree status
Tag Create, list, delete or verify a tag object signed with GPG
Git common knowledge notes