Git & GitHub commonly used commands

Finishing summary from "GitHub entry and Practice" - (Japan) Hiroshi Otsuka remember, branch Peng Hao Bin translation

Git part


basic configuration

  1. Set the name and e-mail address when using Git
$ git config --global user.name "Firstname Lastname"
$ git config --global user.email "[email protected]"
  1. Improve the readability of the output of the command
$ git config --global color.ui auto

Local warehouse

Basic Operations
  1. Create a directory prepared as a local repository, perform initialization after entering the git init
$ mkdir git-tutorial
$ cd git-tutorial
$ git init 
# 下为示例显示内容
Initialized empty Git repository in /Users/hirocaster/github/github-book/git-tutorial/.git/

.Git directory in which to store the necessary data warehouse to manage the current directory content. In Git we call the current directory (such as / git-tutorial) is "attached to the working tree warehouse." File editing and other operations carried out in the working tree, and then recorded the warehouse, in order to manage historical snapshot file. To restore files to their original state, snapshots, open work before the tree retrieved from the warehouse.

  1. Use git status command to view the current status of warehouse
$ git status
  1. Use git add command to add files to the staging area (such as creating test.txt file as a test)
$ git add test.txt
# 添加目录下全部文件
$ git add .

4. Use the git commit command to save the warehouse History

$ git commit -m "create test.txt"

After the -m parameter content for this submission instructions (Overview). If after git commit command without directly -m and parameters, the editor will automatically open, the following format may commit message detail record:

  • The first line: change the content submitted by brief line of text
  • The second line: a blank line
  • The third line and beyond: describes the causes and details of changes
  • Row represents the beginning of the comment content pound sign

At this point you want to terminate the blank may be submitted directly submit information and close the editor.

  1. May be used in the following manner were combined with the commit add
$ git commit -am "合并提交"
  1. Use git log command to view the commit log
$ git log
# 只显示提交信息第一行
$ git log --pretty=short
# 只显示指定目录、文件的日志
$ git log test.txt
# 显示文件改动(查看文件提交日志及提交前后差别)
$ git log -p test.txt
  1. View differences working tree and the temporary area
$ git diff
  1. View tree and differential latest work submitted
$ git diff HEAD

Branch operation

9. Create, branch switching

$ git branch feature-A
$ git checkout feature-A
# 上述两条命令合并
$ git checkout -b feature-A
  1. Display a list of branches
$ git branch
# 下为示例显示内容
* feature-A
  master
  1. Switch back to a branch
$ git checkout -
  1. The combined branch (assuming that the master merge feature-A)
$ git checkout master
Switched to branch 'master'
$ git merge --no-ff feature-A

Then start the editor for merging information into submission.

  1. View branch in chart form
$ git log --graph
  1. Historical version back
$ git reset --hard 目标时间点哈希值(git log可查看)
  1. View the current operation log warehouse have been executed
$ git reflog
  1. Eliminate conflicts
    content file conflicts arise when the merger might look like this:
# test.txt
<<<<<<< HEAD
     - feature-A
=======
     - feature-B
>>>>>>> feature-B

We manually in the editor to change the way you want, save and exit and commit to the implementation of the appropriate add:

# test.txt
     - feature-A
     - feature-B

GitHub part


basic configuration

  1. Local Settings SSH Key
$ ssh-keygen -t rsa -C "[email protected]"
# 下为显示内容
Generating public/private rsa key pair.
Enter file in which to save the key 
(/Users/your_user_directory/.ssh/id_rsa):#此处键入回车
Enter passphrase (empty for no passphrase):#此处键入密码(直接回车设置为无密码)
Enter same passphrase again:#此处再次输入上步所设密码
Your identification has been saved in /Users/your_user_directory/.ssh/id_rsa.
Your public key has been saved in /Users/your_user_directory/.ssh/id_rsa.pub.
The key fingerprint is:
fingerprint值 [email protected]
# 下为示例显示内容
The key's randomart image is:
+--[ RSA 2048 ]----+
|       .+    +    |
|     =  o  O  .   |
#下略

At this time next /Users/your_user_directory/.ssh directory will generate id_rsa, id_rsa.pub two files id_rsa private key (cautious properly preserved) , id_rsa.pub is the public key. After the SSH connection, depending on the configuration may also generate known_hosts file, as is visited remote server public key (this can only assume the remote server for the first time visiting the correct and safe) , the next time access to the same server when, SSH will check the public, if the public key different, SSH warns avoid middleman attacks.

  1. Add the public key to GitHub
    select SSH Keys menu in GitHub account settings (Account Settings), click the Add SSH Key, add the key name in the Title field, Key field enter the local id_rsa.pub file generated in step content, use the following command quick look at:
$ cat ~/.ssh/id_rsa.pub
ssh-rsa 公开密钥的内容 [email protected]

After successfully added Create an account mailbox will receive a "public key addition is complete" message.

  1. Local authentication and communication with GitHub
$ ssh -T [email protected]
# 下为显示内容
The authenticity of host 'github.com(207.97.227.239)' can't be established.
RSA key fingerprint is fingerprint值.
Are you sure you want to continue connecting (yes/no)? #输入yes
# 出现如下结果即为成功
Hi 你的用户名 ! You've successfully authenticated, but GitHub dose not provide shell access.

Remote warehouse

Basic Remote Operations
  1. GitHub repository to create a new page using the toolbar New repository
  • Repository name: the name of the warehouse.
  • Description: Warehouse Description (optional).
  • Public, Private choices: whether to open a warehouse, Private can set access permissions.
  • Initialize this repository with a README: automatic initialization warehouse and set the README file, allowing users to clone warehouse immediately. To add to an existing Git repository GitHub hands, do not check, direct manual push.
  • Add .gitignore: .gitignore file may be selected automatically generated by the pull-down menu, the file records without adding Git repository version management file formats (including the frame and the main language), i.e., no not make a selection.
  • Add a license: add a license file, refer to the mainstream open-source license Discrimination .

After GitHub repository is successfully created, URL format is generally as follows: https://github.com/user_name/repo_name , README.md contents of the file will be automatically displayed on the home page warehouses, generally including but not limited to software summary, the use of processes, licensing agreements, etc. information. Another, GitHub Flavored Markdown (GFM) is an expansion of the grammar GitHub Markdown syntax on the basis, can be widely used, such as README.md written expression, Issue, WiKi, reviews and other functional areas GitHub content.

  1. clone an existing warehouse (SSH and HTTPS links can be, according to different on-demand)
$ git clone [email protected]:hirocastest/Hello-World.git
# 下为示例显示内容
Cloning into 'Hello-World'...
remote:Counting objects:3,done.
remote:Total 3 (delta 0),reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
  1. Be push into the local repository clone, and make changes with a range of local git operations
$ git push
# 下为示例显示内容
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 328bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:hirocastest/Hello-World.git
     46ff713..d23b909 master -> master

Remote Operations Branch
  1. Adding a remote repository
$ git remote add origin [email protected]:hirocastest/git-tutorial.git
  1. Pushed to the appropriate branch of a remote repository
$ git push -u origin master(或其他目标分支名)
  1. View all branches (including remote branch)
$ git branch -a 
# 下为示例显示内容
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature-D
  remotes/origin/master
  1. Get Remote branch
$ git checkout -b feature-D origin/feature-D
  1. Get the latest remote branch warehouse
$ git pull origin feature-D

Reproduced in: https: //www.jianshu.com/p/75a4b0506acb

Guess you like

Origin blog.csdn.net/weixin_33933118/article/details/91183455