Experiment Description
This section of the experiment is the first experiment for getting started with git, which helps you become familiar with how to create and use a git repository.
The initialization of Git
Before we use Git for code management, we start by initializing git.
Git Configuration
The first thing you can do with Git is to set up your name and email, which is the signature you make when you commit a commit, and that information is included in each commit record.
Configure using the git config command:
$ "YOURNAME"$ "YOURMAILADDRESS"
After executing the above command, a file called. Gitconfig is created under the home directory (/home/shiyanlou) (this file asks the hidden file, which needs to be seen using Ls-al). The content is generally like this, you can use VIM or cat to view the contents of the file:
$ cat ~/.gitconfig[user] email = [email protected].com name = Scott Chacon
The above configuration file is the GIT global configuration file
The general configuration method is
<配置名称><配置的值>
If you want to make a difference between a value in the project and the previous global setting (for example, changing a private email address to a work mailbox)
You can use it in your project
`gitconfig命令不带--global选项来设置.这会在你当前的项目目录下创建.git/config,从而使用针对当前项目的配置。`
Get a git repository
Now that we've set everything up, we need a git repository.
There are two ways to get it:
-Clone (clone, copy) from an existing Git repository;
Or
-Create a new warehouse and version control of files that are not versioned.
Clone a warehouse
To get a copy of a project, we need to know the address of the project repository (the Git URL).
Git can be used under many protocols, so git URLs may start with ssh://, HTTP (s)://, git://. Some warehouses can be accessed through more than one protocol.
We have provided a public repository named Gitproject for everyone to test on git.shiyanlou.com, which can be clone using the following method:
$ http://git.shiyanlou.com/shiyanlou/gitproject
When the clone operation is complete, you will find that there is a Gitproject folder in the current directory, and the contents of this folder are the code we just clone. Since the current ' gitproject is only a test project, there is only one readme.md file in it.
$ cd gitproject/(master)$ README.md
The attentive classmate can find a more (master) in front of the command prompt. This is because the experimental building of Linux is using ZSH shell,zsh will determine whether the current directory has a git repository, if so, it will automatically display the current Git branch in the prompt. The concept of Git branching will be described later.
Initialize a new warehouse
You can use the following command for an existing folder to place it under git versioning control.
Create Code Catalog Project:
$ cd /home/shiyanlou/$ mkdir project进入到代码目录,创建并初始化Git仓库:$ cd project$ git init
Git will output:
emptyin /home/shiyanlou/project/.git/
Pass
ls -la
The command will find that a directory named. Git is created under the project directory, which means that a repository is initialized. You can go to the. Git directory to see what's under it.
The normal work flow
The basic process of Git is as follows:
1. Create or modify a file
1. Use the git add command to add a newly created or modified file to the local cache (Index)
1. Commit to the local code base using the git commit command
1. (optional, sometimes there is no remote code base that can be synchronized) use the git push command to synchronize the local code base to the remote code base
Example
Go to the project directory that we just created and create the file separately File1,file2,file3
$ cd project$ touch file1 file2 file3
Modify the file, you can use vim to edit the content, you can also directly echo add test content.
echo"testcontent1"echo"testcontent2"echo"testcontent3" >> file3
You can now use the git status command to view the status of the current git repository:
files: "git add <file>..."toincludein what will be committed) file1 file2 tofiles"git add"to track)
As you can see, there are three files in the untracked state, and the next step is to add them to the buffer (Index) with the git add command.
Use the git add command to add the newly created file to:
$ git add file1 file2 file3然后再次执行git status就会发现新的变化:$ git statusOnto be committed: (use"git rm --cached <file>..."to unstage) newfile: file1 newfile: file2 newfile: file3
Now that you're ready for commit, you can use the git diff command plus the –cached parameter to see which files in the cache have been modified. After entering the Git diff–cached interface, you need to enter Q to exit:
$gitdiff--cached
Without the –cached parameter, git diff will show you all the changes you have made but not added to the index.
If you want to make further changes, do so, and then add the newly modified file to the buffer after you have finished.
When all new and modified files are added to the cache, we will commit to the local repository using git commit:
$ "add 3 files"
You need to use-M to add a comment for this modification, and a new project version will be recorded when you are finished. In addition to using the git add command, we can also submit all modifications not added to the cache with the following command, but the-a command does not add the newly created file.
-a-m"add 3 files"
Re-enter GIT status to view the status, you will find that the current code base has no pending files, the buffer has been emptied.
So far, we've completed our first code submission, and we've created three new files in this commit code.
It is important to note that if you are modifying a file, you also need to add the git add command to the buffer to submit it.
If you delete a file, the deleted file information is automatically added to the cache when you delete it directly using the git RM command, and the corresponding file in the local repository is deleted after the Git commit commits.
At this point, if the local repository is connected to a remote git server, you can use the following command to synchronize the local repository to remote servers:
$ git push origin master
You may need to enter your username and password on the GIT server.
Branching and merging
The GIT branch allows you to commit code outside the main Line (master branch) without affecting the code base mainline.
The role of the branch in multi-person collaborative development, such as a team to develop software, you are responsible for a separate function takes one months to complete, you can create a branch, only the function of the code submitted to this branch, and other colleagues can still continue to use the mainline development, Your daily submissions will not have any effect on them. Once you have finished the function, test by merging your functional branches into the main line.
1. Branch
A git repository can maintain many development branches. Now let's create a new branch called experimental:
$ git Branch experimental
Run the git branch command to view the current branch list, which branch is already in the current development environment:
$ git branch experimental* master
The experimental branch is the one you just created, and the Master branch is the main branch that the GIT system creates by default.
The asterisk identifies which branch you are working on
Enter a git checkout branch name to switch to another branch:
$ git checkout experimentalSwitched‘experimental‘
Switch to the experimental branch, after the switch is complete, edit one of the files inside, then commit (commit) changes, and finally switch back to the "master" branch:
# 修改文件file1$ "update" >> file1# 查看当前状态$ git status# 添加并提交file1的修改$ git add file1$ "update file1"# 查看file1的内容$ cat file1testupdate# 切换到master分支$ git checkout master
Viewing the contents of the next file1 will reveal that the changes you have just made have not been seen. Because the changes were just under the experimental branch, now switch back to the master branch, the files in the directory are the files on the master branch.
You can now make a few different changes under the master branch:
# 修改文件file2$ "update again" >> file2# 查看当前状态$ git status# 添加并提交file2的修改$ git add file2$ "update file2 on master"# 查看file2的内容$ cat file2testupdate again
At this point, the two branches have their own different changes, the content of the branch is different, how to merge multiple branches?
The following git merge command can be used to merge experimental to mainline branch master:
# 切换到master分支$ git checkout master# 将experimental分支合并到master$ git merge ‘merge experimental branch‘ experimental-m参数仍然是需要填写合并的注释信息。
Since two branch have modified two different files, there will be no conflict when merging, and the merge will be completed after executing the above command.
If there is a conflict, such as two branches have changed a file file3, the merge will fail. First we modify the File3 file on the master branch and submit:
# 切换到master分支$ git checkout master# 修改file3文件$ "master: update file3" >> file3# 提交到master分支$ ‘update file3 on master‘然后切换到experimental,修改file3并提交:# 切换到experimental分支$ git checkout experimental# 修改file3文件$ "experimental: update file3" >> file3# 提交到master分支$ ‘update file3 on experimental‘
Switch to master for merging:
$ git checkout master$ git merge experimentalAuto-merging file3CONFLICT (content): Merge conflict in file3Automatic merge failed; fix conflicts and then commit the result.合并失败后先用git status查看状态,会发现file3显示为both modified,查看file3内容会发现:$ cat file3test<<<<<<< HEADmaster: update file3=======experimental: update file3>>>>>>> experimental
The above content can also be used
git diff
View, previously mentioned Git diff does not add parameters to display the modifications that were not submitted to the cache.
You can see that the conflicting content is added to the file3, we use vim to edit this file, remove the <<<<<< and other symbols that git automatically generates a flag conflict, save as needed, and then use git add File3 and Git commit commands to submit the merged File3 content, which is the process of manually resolving conflicts.
# 编辑冲突文件$ vim file3# 提交修改后的文件$ git add file3$ ‘merge file3‘
When we complete the merge and no longer need experimental, we can use the following command to delete:
-d-d只能删除那些已经被当前分支的合并的分支. 如果你要**强制删除某个分支的话就用git branch –D**
2. Scatter a merger
If you think your merged state is mess and you want to discard the current changes, you can use the following command to return to the previous state of the merge:
$ HEAD^# 查看file3的内容,已经恢复到合并前的master上的文件内容$ cat file3
3. Fast Forward Merge
There is also a situation that requires special treatment, which is not mentioned earlier. Typically, a merge produces a merge commit (commit) that merges the contents of each row in the two parent branch.
However, if there is no difference in content between the current branch and the other branch, that is, each commit (commit) of the current branch already exists in another branch, and Git executes a fast forward operation, and Git does not create any new commits (commit) , just point the current branch to the merged branch.
git log 1. View Logs
The git log command can show all commits (commit):
logloglog查看,例如下面的命令就是找出所有从"v2.5“开始在fs目录下的所有Makefile的修改:$ git log v2.5.. Makefile fs/Git会根据git log命令的参数,按时间顺序显示相关的提交(commit)。
2. Log statistics
If you use ' git log ' with the –stat option, it will show which files have been modified in each commit (commit), how many rows were added or deleted, and this command is equivalent to printing a detailed commit record:
log--stat###3.格式化日志你可以按你的要求来格式化日志输出。--pretty 参数可以使用若干表现格式,如oneline:log--pretty=onelineshortlog--pretty=short你也可用medium,full,fuller,email 或raw。 如果这些格式不完全符合你的相求, 你也可以用--pretty=format参数定义格式。--graph 选项可以可视化你的提交图(commit graph),会用ASCII字符来画出一个很漂亮的提交历史(commit history)线:log--graph --pretty=oneline
4. Log sorting
Log records can be displayed in different order. If you want to specify a specific order, you can add sequential parameters to the git log command.
By default, commits are displayed in reverse chronological order, and you can specify the –topo-order parameter to have the commit appear in topological order (that is, the child commits are displayed before their parent commits):
$gitlog--pretty=format:‘%h:%s‘--topo-order--graph
You can also use the –reverse parameter to reverse display all commit logs.
Summarize
This section explains several basic commands:
-git config: Configure related information
-git clone: Copy repository
-git init: Initializing the Repository
-git add: Add updated content to the index
-git diff: compare content
-Git Status: Get current project status
-Git commit: Commit
-Git branch: branch-related
-git checkout: Switch branches
-git merge: Merging branches
-Git Reset: Recovery version
-git log: View log
Create and use a git repository