GIT pure command operation, Install,clone, Commit,push,pull, version fallback, undo update, Branch creation/switchover/update/commit/merge, code conflict
This is followed by the next release of the distributed repository--windows git environment deployment and on GitHub on the open source of their own projects, the main thing is to use GUI to graphical interface operation, but generally we programmers do not do so, with command and easy and pleasant, so, Here I opened another article to specifically say how the command is how to operate, but pay attention to Oh, in fact, Liaoche Teacher's website is very good learning resources Oh!
- Liaoche Teacher: http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
Let's start with the installation, my previous Ubuntu configuration blog also said the installation, here I will talk about three platforms together!
One. Install
Installation is actually the simplest way.
Windows
Installation of Windows as long as you download a Help tool,
- https://git-for-windows.github.io/
After installation, we have a git Bash here on the desktop right click
- Ubuntu
Linux installation is a matter of command, I also have this blog on the Android source code Analysis (ii)--ubuntu root,git,vmware Tools, installation Input method, theme beautification, Dock, installation JDK and configuration environment
In the terminal input
sudo apt-get install git
If you are the root permission of #, you can use sudo
X OS
Mac system Installation Although it is also a command, but he needs to use auxiliary tools, of course, you can also directly install Xcode, it is integrated with git, but need to configure, we as an Android developer, this xcode actually look at personal interests, we use another way to install
http://brew.sh/
Just download the installation Homebrew,mac terminal to search for the installation software.
Input
sudo brew install git
It is also easy to wait for the installation to succeed and to check if the installation is successful, just enter
version
Just look at his version.
Two. Clone
Clone is downloading the project from GitHub, and we're building a new project on GitHub called Gittest! If this step does not work, please move: Distributed repository--windows git environment deployment and open source project on GitHub
We've got this link to remember.
https://github.com/LiuGuiLinAndroid/GitTest.git
Here, I'll start with a new Git folder on D, then go to this directory, right-click on git Bash and type
clone https://github.com/LiuGuiLinAndroid/GitTest.git
We can download the code.
We go into this folder and we can see that he has only one file readme.md, we enter
vim README.md
To visit him
Here vim is an edit command, we launch this VIM environment as long as the input
q!
You can do it.
Three. Commit
To commit the code, let's start with Git's working mode, in fact, Git is so famous for his patterns, and our chapters are following his pattern.
// add->commit->push
First add, that is, the code you want to submit to the buffer, and then commit to the local repository, and then push pushed to the remote repository, that is, on GitHub, here, we first modified the readme.md file, we edit, plus a little text
After we saved, the green file just became an exclamation point, indicating that there has been a modification, this and SVN, we go back to the command line, here, we have a very convenient command
git status
To view the current status
Here is the prompt you this file is already red, changed, need to submit it?
So, let's just type
add README.md
Which is added to the buffer.
He's got nothing. Note that add is successful, this time we look at the state again, you will find
The status is Green, Ok, this time we need to commit, enter
commit README.md
And then he's going to show such a message, here is to write some instructions, I wrote first commit
Enter HERE
x
Save exit
And then we look at the state again.
Sure enough, this shows you have a commit that requires push, that is, push
Four. Push
Since he has a friendly hint that we can push, let's just push it, just enter
push
Then he will prompt you to enter your account number and password
After we lose, we wait a little while, and they will naturally be done.
Here, our git push is OK, so let's take a look on GitHub and we'll see.
Five. Pull
This is a good understanding, Kaiyuan the author of his project has been a modification, there are new things, we will go to update, also go to the code down, so that we modify on GitHub
That's the way it is now.
This is obviously not the same as my local, so we need to go to pusll and enter
git pull
Okay, pull it down.
We can use the VIM command to check it out.
As you can see, the code is pulled down.
Six. Version fallback
Write code write write to find yourself into the pit, how to do, and the code also submitted, before the teammates have not pull, hurriedly version back, then how to do? We can do this, first of all, we enter
log
To view the records of our submitted code
Here we can see that the first time we were initialized by the system, the second time we thought of the commit, and the third, which we submitted on GitHub when we pull, I'm now going to show you how to go back to first commit.
But we have to note that, in the log log of a long string of yellow ID, this is what we want to pay attention to, we only through this ID to the version fallback, so, here I first copy the ID of a commit:
a0c401fcda58304938767820f35d6b4bcaad28db
OK, here we go, the version fallback command is
gitreset--harda0c401fcda58304938767820f35d6b4bcaad28db
Good
He hints that he has retreated to first commit, and we verify that by looking at the information in readme.md, we can use the VIM command to see
Here is sure to show the first commit, indicating that we have a successful version fallback, version fallback risk, operation need to be careful ha!
If you say you want to go back to the original state, is also possible, follow the above method to go through the process, but note that this time to get the ID is not input git log, here comes a new command
git reflog
Log all the logs, and then you'll know how to do it.
But here we are pulling out of our undo operation out of the
Seven. Undo the update
So much has been said before, this part is actually written command, all not actually demo
--文件名//如果你已经add了代码到缓存区git reset HEAD 文件名//但是如果你已经commit了,就需要版本回退了--hard 版本号//要是你推送到了远程仓库,比如github.....你就很痛苦了
Eight. Creation and switching of branches
As we all know, there is a main branch master branch by default, but we sometimes need to create a branch if we need to test some features or whatever, and when it feels like the branch code can be merged into the main branch, the benefit is to ensure the cleanliness and security of the main branch, OK, Let's take a look at how it operates:
gitbranch[分支名]
gitcheckout[分支名]
That's it, and there's another convenient way
- Create and switch branches
gitcheckout-b[分支名]
In this diagram, we can see that we first looked at the branch where we were, was in the main branch, and then I created a branch called Hello, and then I switched to the Hello Branch and looked at my branch again, that is, hello, and then switch back to the main branch. Then create the Android branch with the command created and switched, and then look at the branch, we're on the Android branch.
Nine. Branch updates and Submissions
Branch push and pull, in fact, and the operation of the main branch is slightly different, is the command
push origin 分支名
Look, he can submit, show us the password of the account
Here, we have pushed the Android branch up, and the Hello Branch has not, so we entered
-a
View so the branch
Here, the white is the local branch, the green one, the branch you're in, the red One, the remote branch.
now that the push is going to be handled, pull must be the same as push
.
git pull origin 分支名
OK, this is okay.
10. Branch Merging
Here we can see that the branch where we are located is Android
We make changes to the README.MD
Then add commit
Now that our Android branch has been modified, but the main master branch disease is not, we think that the Android branch of the code can be used, need to merge into the main branch, then what should we do?
As we can see here, we first switch to the main branch and then enter
merge 分支名
To merge the code, now, let's take a look at
Okay, so the code is merged successfully.
11. Code Conflicts
This code conflict, in SVN is also a wonderful encounter, or their own version control this piece of unskilled, general conflict git words are two cases
- A code violation occurs when you push the code
- A code conflict occurs when merging branches
For example, you write in the branch of the Code, the main branch is similar, there is a great possibility of conflict, we have to test, we do not demonstrate, I tell a general idea, when there is a conflict, the command line tells us which file merge error, when found this file
vim 文件名
You can see that there is a special arrow wrapped around a piece of code, that is two blocks of code, you need to manually fix
Then, you need to resubmit, but when you commit again,
-i-m‘更新说明‘ 文件名
This will allow you to push the file directly.
OK, little friends, git use has learned my group, the Magic Journey to Android: 555974449, Welcome to come in exchange technology!
GIT pure command operation, Install,clone, Commit,push,pull, version fallback, undo update, Branch creation/switchover/update/commit/merge, code conflict