git basic command
Git's job is to create and save snapshots of your project and compare them with subsequent snapshots.
This chapter describes the commands for creating and submitting snapshots of your project.
Git commonly uses the following six commands: git clone, git push, git add, git commit, git checkout, and git pull, which will be described in detail later.
explain:
- Workspace: workspace
- Staging area: staging area / cache area
- Local repository: version library or local repository
- Remote repository: remote repository
A simple procedure:
$ git init $ git add . $ git commit
- git init - initialize the warehouse.
- git add. - adds files to the staging area.
- git commit - adds staging contents to the repository.
Create warehouse command
The following table lists git commands for creating warehouses:
command | explain |
---|---|
git init | Initialize warehouse |
git clone | Copy a remote warehouse, that is, download a project. |
git init
Git uses git init command to initialize a git repository. Many git commands need to be run in Git repository, so git init is the first command to use GIT.
After the git init command is executed, the git repository will generate a. git directory, which contains all metadata of resources, and other project directories remain unchanged.
usage method
Using the current directory as the Git repository, we just need to initialize it.
git init
After the command is executed, a. git directory will be generated in the current directory.
Use the directory we specified as the Git repository.
git init newrepo
After initialization, a directory named. Git will appear in the newrepo directory. All data and resources required by git are stored in this directory.
If several files in the current directory want to be included in version control, you need to tell Git to start tracking these files with the git add command, and then submit:
$ git add *.c $ git add README $ git commit -m 'Initialize project version'
The above command submits the. c and README files in the directory to the warehouse.
Note: in Linux system, single quotation marks are used for commit information, and in Windows system, double quotation marks are used for commit information.
Therefore, in git bash, git commit -m 'Submit instructions' is OK. In the Windows command line, use double quotation marks git commit -m "submit instructions".
git clone
We use git clone to copy items from the existing Git repository (similar to svn checkout).
The command format of clone warehouse is:
git clone <repo>
If we need to clone to the specified directory, we can use the following command format:
git clone <repo> <directory>
Parameter Description:
- repo:Git warehouse.
- Directory: local directory.
For example, to clone the Git code warehouse grid of Ruby language, you can use the following command:
$ git clone git://github.com/schacon/grit.git
After executing this command, a directory named grid will be created in the current directory, which contains a. git directory to save all the downloaded version records.
If you want to define the name of the project directory to be created by yourself, you can specify a new name at the end of the above command:
$ git clone git://github.com/schacon/grit.git mygrit
to configure
git is set using the git config command.
Display the current git configuration information:
$ git config --list credential.helper=osxkeychain core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true core.ignorecase=true core.precomposeunicode=true
Edit git profile:
$ git config -e # For the current warehouse
Or:
$ git config -e --global # For all warehouses on the system
Set user information when submitting Code:
$ git config --global user.name "runoob" $ git config --global user.email test@runoob.com
If the -- global parameter is removed, it is only valid for the current warehouse.
Submission and modification
Git's job is to create and save snapshots of your project and compare them with subsequent snapshots.
The following table lists commands for creating and submitting snapshots of your project:
command | explain |
---|---|
git add | Add file to warehouse |
git status | View the current status of the warehouse and display the changed files. |
git diff | Compare the differences between files, that is, the differences between staging area and workspace. |
git commit | Submit staging area to local warehouse. |
git reset | Fallback version. |
git rm | Delete workspace file. |
git mv | Move or rename workspace files. |
Commit log
command | explain |
---|---|
git log | View submission history |
git blame <file> | View the modification history of the specified file as a list |
We can use the git log --oneline option to view a concise version of the history.
Remote operation
command | explain |
---|---|
git remote | Remote warehouse operation |
git fetch | Get code base from remote |
git pull | Download remote code and merge |
git push | Upload remote code and merge |
Git branch management
Almost every version control system supports branching in some form. Using branching means that you can separate from the development main line and continue to work without affecting the main line.
Some people call Git's branching model a must kill feature, which distinguishes Git from the version control system family.
Create branch command:
git branch (branchname)
Switch branch command:
git checkout (branchname)
When you switch branches, Git will replace the contents of your working directory with the last committed snapshot of the branch, so multiple branches do not need multiple directories.
Merge branch command:
git merge
You can merge to a unified branch multiple times, or you can choose to delete the merged branch directly after the merge.
Git branch management
List branches
List branch basic commands:
git branch
Without parameters, git branch will list your local branches.
$ git branch * master
This example means that we have a branch called master, and this branch is the current branch.
When you execute git init, Git will create a master branch for you by default.
If we want to create a branch manually, just execute git branch (branchname).
$ git branch testing $ git branch * master testing
Now we can see that there is a new branch testing.
When you create a new branch after submitting the update last time in this way, if an update is submitted later, and then switch to the testing branch, Git will restore your working directory to what it was when you created the branch.
Next, we will show how to switch branches. We use git checkout (branch) to switch to the branch we want to modify.
$ ls README $ echo 'runoob.com' > test.txt $ git add . $ git commit -m 'add test.txt' [master 3e92c19] add test.txt 1 file changed, 1 insertion(+) create mode 100644 test.txt $ ls README test.txt $ git checkout testing Switched to branch 'testing' $ ls README
When we switch to the testing branch, the new file test.txt we added is removed. When we switch back to the master branch, they reappear.
$ git checkout master Switched to branch 'master' $ ls README test.txt
We can also use the git checkout -b (branchname) command to create a new branch and immediately switch to the branch to operate in the branch.
$ git checkout -b newtest Switched to a new branch 'newtest' $ git rm test.txt rm 'test.txt' $ ls README $ touch runoob.php $ git add . $ git commit -am 'removed test.txt,add runoob.php' [newtest c1501a2] removed test.txt,add runoob.php 2 files changed, 1 deletion(-) create mode 100644 runoob.php delete mode 100644 test.txt $ ls README runoob.php $ git checkout master Switched to branch 'master' $ ls README test.txt
As you can see, we created a branch, removed some files test.txt from the top of the branch, added the runoob.php file, and then switched back to our main branch. The deleted test.txt file came back, and the newly added runoob.php did not exist in the main branch.
Using branches to break up work allows us to do things in different development environments and switch back and forth.
Delete branch
Delete branch command:
git branch -d (branchname)
For example, we want to delete the testing branch:
$ git branch * master testing $ git branch -d testing Deleted branch testing (was 85fc7e7). $ git branch * master
Branch merge
Once a branch has independent content, you will eventually want to merge it back to your main branch. You can merge any branch into the current branch using the following command:
git merge
$ git branch * master newtest $ ls README test.txt $ git merge newtest Updating 3e92c19..c1501a2 Fast-forward runoob.php | 0 test.txt | 1 - 2 files changed, 1 deletion(-) create mode 100644 runoob.php delete mode 100644 test.txt $ ls README runoob.php
In the above example, we merged the newtest branch into the main branch, and the test.txt file was deleted.
After merging, you can delete branches:
$ git branch -d newtest Deleted branch newtest (was c1501a2).
After deletion, only the master branch remains:
$ git branch * master
Merge conflict
Merging is not just a simple operation of adding and removing files. Git will also merge and modify files.
$ git branch * master $ cat runoob.php
First, we create a file called change_ Switch to the branch of site. We will change the content of runoob.php to:
<?php echo 'runoob'; ?>
Create change_site branch:
$ git checkout -b change_site Switched to a new branch 'change_site' $ vim runoob.php $ head -3 runoob.php <?php echo 'runoob'; ?> $ git commit -am 'changed the runoob.php' [change_site 7774248] changed the runoob.php 1 file changed, 3 insertions(+)
Submit the modified content to change_ In the site branch. Now, if we switch back to the master branch, we can see that the content is restored to the one before our modification (empty file, no code), and we modify the runoob.php file again.
$ git checkout master Switched to branch 'master' $ cat runoob.php $ vim runoob.php # The amendments are as follows: $ cat runoob.php <?php echo 1; ?> $ git diff diff --git a/runoob.php b/runoob.php index e69de29..ac60739 100644 --- a/runoob.php +++ b/runoob.php @@ -0,0 +1,3 @@ +<?php +echo 1; +?> $ git commit -am 'Modify code' [master c68142b] Modify code 1 file changed, 3 insertions(+)
Now these changes have been recorded in my "master" branch. Next, we merge the "change_site" branch.
$ git merge change_site Auto-merging runoob.php CONFLICT (content): Merge conflict in runoob.php Automatic merge failed; fix conflicts and then commit the result. $ cat runoob.php # Open the file and see the conflict content <?php <<<<<<< HEAD echo 1; ======= echo 'runoob'; >>>>>>> change_site ?>
When we merge the previous branch into the master branch, a merge conflict occurs. Next, we need to modify it manually.
$ vim runoob.php $ cat runoob.php <?php echo 1; echo 'runoob'; ?> $ git diff diff --cc runoob.php index ac60739,b63d7d7..0000000 --- a/runoob.php +++ b/runoob.php @@@ -1,3 -1,3 +1,4 @@@ <?php +echo 1; + echo 'runoob'; ?>
In Git, we can use git add to tell Git that the file conflict has been resolved
$ git status -s UU runoob.php $ git add runoob.php $ git status -s M runoob.php $ git commit [master 88afe0e] Merge branch 'change_site'
Now we have successfully resolved the conflict in the merge and submitted the results.
Interact with remote warehouse
Add remote library
To add a new remote warehouse, you can specify a simple name for future reference. The command format is as follows:
git remote add [shortname] [url]
This example takes Github as a remote warehouse. If you don't have Github, you can use it on the official website https://github.com/ Registration.
Since the transmission between your local Git warehouse and GitHub warehouse is encrypted through SSH, we need to configure the authentication information:
Use the following command to generate the SSH Key:
$ ssh-keygen -t rsa -C "youremail@example.com"
Your behind_ email@youremail.com Change to the email you registered on Github, and then you will be asked to confirm the path and enter the password. We will use the default all-way return.
If successful, the. ssh folder will be generated under ~ /. Go in and open the id_rsa.pub, copy the key inside.
$ ssh-keygen -t rsa -C "429240967@qq.com" Generating public/private rsa key pair. Enter file in which to save the key (/Users/tianqixin/.ssh/id_rsa): Enter passphrase (empty for no passphrase): # Direct enter Enter same passphrase again: # Direct enter Your identification has been saved in /Users/tianqixin/.ssh/id_rsa. Your public key has been saved in /Users/tianqixin/.ssh/id_rsa.pub. The key fingerprint is: SHA256:MDKVidPTDXIQoJwoqUmI4LBAsg5XByBlrOEzkxrwARI 429240967@qq.com The key's randomart image is: +---[RSA 3072]----+ |E*+.+=**oo | |%Oo+oo=o. . | |%**.o.o. | |OO. o o | |+o+ S | |. | | | | | | | +----[SHA256]-----+
Go back to github and enter account = > Settings - > SSH and GPG keys - > New SSH key
To verify success, enter the following command:
$ ssh -T git@github.com The authenticity of host 'github.com (52.74.223.119)' can't be established. RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes # Enter yes Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts. Hi tianqixin! You've successfully authenticated, but GitHub does not provide shell access. # Success information
The following command indicates that we have successfully connected to Github.
View the current remote library
To view which remote warehouses are currently configured, you can use the command:
git remote
example
$ git remote origin $ git remote -v origin git@github.com:tianqixin/runoob-git-test.git (fetch) origin git@github.com:tianqixin/runoob-git-test.git (push)
Extract remote warehouse
Git has two commands to extract updates from the remote repository.
1. Download new branches and data from remote warehouse:
git fetch
After the command is executed, you need to execute git merge remote branch to your branch.
2. Extract data from the remote warehouse and attempt to merge to the current branch:
git merge
This command is to execute git fetch, and then execute git merge remote branch to any branch where you are.
Suppose you have configured a remote warehouse and you want to extract the updated data, you can execute it first git fetch [alias] Tell Git to get the data you don't have, and then you can execute it git merge [alias]/[branch] To merge any updates on the server (assuming someone pushed it to the server at this time) into your current branch.