(1) Git uses vi or vim commands to open, close, and save files (2) The problem of entering the username and password every time when git push is solved (3) git updates the code to the local (4) The basic steps of git upload operation

Closer to home, recently someone asked in the group how to upload the newly created local code to github. Here is a brief record. I like to use the command line. Here, it is all implemented by the command line.


Step 1: Create a git repository 
cd to your local project root directory and execute the git command

git init
  • 1

Step 2: Add all files of the project to the repository

git add .
  • 1

If you want to add a specific file, just replace . with a specific file name

Step 3: Commit the add file to the repository

git commit -m "注释语句"
  • 1

Step 4: Go to github to create your own Repository, and create a page as shown below: 
write picture description here

Click on Create repository below, you will enter a page similar to the following, and get the https address of the created repository. The red box indicates the
write picture description here

Step 5: The point is here, associate the local repository to github

git remote add origin https://github.com/hanhailong/CustomRatingBar
  • 1

Replace the following https link address with your own warehouse url address, which is the address marked in the red box above

Step 6: Before uploading to github, pull it first and execute the following command:

git pull origin master
  • 1

After hitting enter, the output will be similar to the following 
write picture description here

The seventh step, which is the last step, upload the code to the github remote repository

git push -u origin master
  • 1

After the execution, if there is no exception, the upload will be successful after the execution is completed. You may be asked to enter the Username and Password in the middle. You only need to enter the github account and password.

Finally, attach the screenshot after the code is uploaded successfully: 

write picture description here

The following are other actions:

(1) Git uses vi or vim commands to open, close, and save files

1. vi & vim have two working modes:

(1) Command mode: the mode for accepting and executing vi & vim operation commands, the default mode after opening the file;

(2) Edit mode: a mode for adding, deleting, and modifying the contents of an open file;

  #Press ESC in edit mode to return to command mode.

2. Create and open a file: $ vi  [ filename ]

(1) Use the vi plus file path (or file name) mode to open the file, if the file exists, open the existing file, if the file does not exist, create a new file, and the bottom line of the terminal shows that a new file is opened.

(2) Enter the letter "i" or "Insert" key on the keyboard to enter the most commonly used insert editing mode.

3. Save the file:

(1) Edit the file in insert edit mode.

(2) Press the "ESC" key to exit the editing mode and switch to the command mode.

(3) Type "ZZ" or ":wq" in command mode to save the changes and exit vi.

(4) If you only want to save the file, type ":w", and the bottom line after the carriage return will prompt you to write the result of the operation, and stay in the command mode.

4. Abandon all file modifications:

(1) Abandon all file modifications: Press the "ESC" key to enter the command mode, type ":q!" and press Enter to abandon the modification and exit vi.

(2) Abandon all file modifications, but do not exit vi, that is, return to the state of the last save operation after the file was opened, and continue the file operation: press the "ESC" key to enter the command mode, type ":e!", return to Return to command mode after the car.


(2) The problem of entering the username and password every time when git push is solved

  • Changed the ssh key and found that every time git push origin master needs to enter the username and password
  • The reason is that https is used when adding remote libraries. . So every time you use https to push to the remote library
  • Check out the transport protocol used:
git remote -v
  • 1

wuxiao@wuxiao-C-B150M-K-Pro:~/MyGithub/DailyBlog$ git remote -v 
origin https://github.com/toyijiu/DailyBlog.git (fetch) 
origin https://github.com/toyijiu/DailyBlog.git (push)

  • The way to reset to ssh:
git remote rm origin
git remote add origin git@github.com:username/repository.git
git push -u origin master
  • 1
  • 2
  • 3
  • Look at the current transmission protocol again: 
    wuxiao@wuxiao-C-B150M-K-Pro:~/MyGithub/DailyBlog$ git remote -v 
    origin [email protected]:toyijiu/DailyBlog.git (fetch) 
    origin git@github. com:toyijiu/DailyBlog.git (push)

(3) git update code to local

Formal process

  1. git status (view local branch file information to ensure that there are no conflicts when updating)

  2. git checkout – [file name] (If the file is modified, it can be restored to the original state; if the file needs to be updated to the server, it should be merged to the server first, and then updated to the local)

  3. git branch (view the current branch situation)

  4. git checkout  remote branch  (if the branch is a local branch, you need to switch to the remote branch of the server)

  5. git pull

If the command is executed successfully, the update code is successful!

fast process

The above is a safer practice, if you can be sure that nothing has been changed, just update the local code 
1. git pull (one command to get it)

(4)git上传操作基本步骤

###(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库
```git
git init
```
###把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点“.”,意为添加文件夹下的所有文件
```git
git add .
```
###用命令 git commit告诉Git,把文件提交到仓库。引号内为提交说明
```git
git commit -m'名称自己取'
```
###链接远程仓库
```git 
git remote add origin
##如:
git remote add origin https://gitee.com/pintian/yii_backstage.git
```
###获取远程库与本地同步合并(如果远程库不为空必须做这一步,否则后面的提交会失败)
```git
git pull --rebase origin master ##master为你的远程库地址
```
###更新远程仓库
```git
git fetch origin ## origin->远程仓库名
```
###将远程主分支合并到本地当前分支
```git
git merge origin/master
```
###把本地库的内容推送到远程,使用 git push命令,实际上是把当前分支master推送到远程。执行此命令后会要求输入用户名、密码,验证通过后即开始上传
```git 
git push -u origin master  ##第一次上传后面上传就不需要-u
git push origin master
```
###状态查询
```git
git status
```
###查询本地提交记录
```git
git log
```
###clone远程到本地
```git
git init  ##git初始化仓库前请先建好项目文件
git clone
##如:
git clone https://gitee.com/pintian/yii_backstage.git
```
###查看修改的内容
```git 
git diff
```
###查看某文件修改内容
```git 
git diff file_name
```
注释.doc
origin为远程仓库名称

master为远程分支名称


备注:

1、当github跟本地同时修改同个文件的时候

On branch master

Your branch and 'origin/master' have diverged,

and have 1 and 1 different commits each, respectively.

  (use "git pull" to merge the remote branch into yours)

You have unmerged paths.

  (fix conflicts and run "git commit")

  (use "git merge --abort" to abort the merge)

Unmerged paths:

  (use "git add <file>..." to mark resolution)

        both modified:   zenghui.txt

no changes added to commit (use "git add" and/or "git commit -a")


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325902497&siteId=291194637
Recommended