git add
& git commit
git among add
and commit
with the svn reasons differ: Distributed vs Centralized
Since the distribution of git determines that each of our computers is a complete repository (repository), the add
sum commit
is relative to our local repository.
There are many things in the repository, the most important of which are:
master
, and a pointer to the master HEAD
.HEAD
HEAD
It means the latest version of the current branch, but in fact it directly points to the current branch.
If there is only one main branch, that is master
, the main branch, it HEAD
points to master
, and master
then points to the latest version
$ git add a.txt//Add a file to the temporary storage area of the local repository $ git add b.txt $ git add.//Add all non-version files to the local library temporary storage area $ git commit -m "add 2 files"//Submit all files in the temporary storage area to the branch (such as master)
Add files to the Git repository in two steps:
git add <file>
- the staging area
attention, can be used repeatedly to add multiple files;
git commit
complete. - Branch
noted that after commit, staging area will be cleared
ps. At this time, it is only added to the local library, not the remote version library github
git remote add origin git@server-name:path/repo-name.git
After the association, use the command git push -u origin master to push all the contents of the master branch for the first time;
After that, after each local submission, as long as necessary, you can use the command git push origin master to push the latest changes;
$ git clone git@github.com:michaelliao/learngit.git ... $ git branch *master
When using the clone command to clone a remote library, by default, only the local master branch can be seen.
What if you want to get the dev branch and develop on it?
//The dev branch of the remote origin must be created to the local $ git checkout -b dev origin/dev
$ git remote origin $ git remote -v//detailed information octocat https://github.com/octocat/Spoon-Knife.git (fetch) octocat https://github.com/octocat/Spoon-Knife.git (push) origin https://github.com/zfengqi/Spoon-Knife.git (fetch) origin https://github.com/zfengqi/Spoon-Knife.git (push) //The above shows the address of origin that can be crawled and pushed. If you don't have push permission, you won't see the push address.
$ git push origin master//Specify the local branch, and then it will be pushed to the corresponding branch $ git push origin dev
Because the remote branch version is newer than your local one, you need to git pull
try to merge first , that is, update code
If git pull
there is a conflict in the merge, you need to resolve the conflict and submit it locally;
If git pull prompts no tracking information , it means that the link relationship between the local branch and the remote branch has not been created
$ git branch --set-upstream <branch-name> <origin/branch-name>
HEAD
The version pointed to is the current version, so Git allows us to shuttle between the version history, using the command$ git reset --hard commit_id $ git reset --hard HEAD^//roll back the last version $ git reset --hard HEAD^^//Roll back to the previous two versions $ git reset --hard HEAD~100//Roll back to the previous 100 versions
$ git log commit 5a396f23af1729fa0c0049c58d42fe0d71541665 Author: kei <zfengqi90@gmail.com> Date: Fri Jul 10 15:20:21 2015 +0800 Update README.md commit e325faab05ad640d1e1dc31873dfd362b9c00a92 Author: zhangfengqi <jackyzfq@qq.com> Date: Fri Jul 10 12:50:58 2015 +0800 JavaScript inheritance summary //If you think the output information is too much and you are dazzled, you can try $ git log --pretty=oneline //Exit or terminate log press q
git reflog
view command history to determine which version to go back to in the future$ git reflog 9a2f804 HEAD@{0}: pull: Merge made by the'recursive' strategy. 3bb1509 HEAD@{1}: commit: Summary of function currying e325faa HEAD@{2}: commit: javascript inheritance summary bd0213d HEAD@{3}: clone: from https://github.com/zfengqi/blog-and-note.git
With commandgit checkout -- <file>
The command git checkout -- readme.txt
means to undo all the modifications of the readme.txt file in the work area. There are two situations here:
ps. The content of the file in the workspace is modified, that is, the local file.
In two steps
git reset HEAD <file>
to return to scenario 1, that is, to cancel the add operation to this file!git checkout -- <file>
The meaning of attentiongit checkout -- <file>
In fact, the version in the version library (including the temporary storage area) is used to replace the version of the workspace. Regardless of whether the workspace is modified or deleted, it can be "restored with one click."
In SVN, the speed of creating and switching branches is very slow, while in git it is a matter of 1 second.
$ git branch//View existing branches $ git branch new_branch//Create branch (without switching) $ git checkout new_branch//switch branch $ git checkout -b new_branch//Create + switch to new_branch branch $ git branch -d new_branch//delete branch $ git branch -D <name>//If you want to discard a branch that has not been merged, you can delete it forcibly $ git merge new_branch//merge on the main master Updating d17efd8..fec145a Fast-forward readme.txt | 1 + 1 file changed, 1 insertion(+)
Fast-forward
Tell us that this merge is in "fast forward mode", that is, the master is directly pointed to the current commit of dev, so the merge speed is very fast.
If there is no add and commit content in the current branch, it is impossible to switch to another branch
What if I don’t want to commit for the time being, but I need to switch to another branch?
$ git stash//"store" the current work site, and continue to work after returning to this branch in the future $ git stash list stash@{0}: WIP on dev: 6224937 add merge
When I returned to this branch, I found that the temporary storage area was empty! Need to restore the previous stash content
$ git stash apply//After recovery, the stash content is not deleted, you need to use git stash drop to delete $ git stash pop//delete the stash content while restoring
Another use scenario of stash: When the current branch code does not have commit, it is impossible to pull the code down, and then you need to stash first
When you modify the same location of the same file in different branches, and merge after each commit:
In this case, Git cannot perform "quick merge" and can only try to merge their respective modifications, but this merge may have conflicts.
zhangfengqi@ED63038F43AD14D/D/zfengqi/github/Spoon-Knife (master) $ git merge branch222 Auto-merging styles.css CONFLICT (content): Merge conflict in styles.css Automatic merge failed; fix conflicts and then commit the result.
At this time, the branch status will become
zhangfengqi@ED63038F43AD14D/D/zfengqi/github/Spoon-Knife (master|MERGING)
Manual conflict resolution: open the conflicting file and keep the final part
Git with <<<<<<<
, =======
, >>>>>>>
marking the contents of the different branches
After modification, add & commit again
$ git log --graph//You can see the branch merge graph.
//pwd is used to display the current directory $ pwd /Users/michael/learngit //cat $ cat readme.txt Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes.
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
var gitalk = new Gitalk({ clientID: '82f3fb6574634b43d203', clientSecret: '5790d9fdbfe9340d29810b694b3b2070a9a3b492', repo:'zfengqi.github.io', owner:'zfengqi', admin: ['zfengqi'], id: winMode: true .loc.pathname, }); gitalk.render('gitalk-container');