Basic use of Git

Source: Internet
Author: User
Tags diff

Basic use of Git

GIT bash operations

0.git process


Contains 6 commonly used commands

Special noun translation

    • Workspace: Work Area
    • Index/stage: Staging Area
    • Repository: Warehouse area (or local warehouse)
    • Remote: Repository
1.git Basic Configuration

git config--list can view the parameters that have been configured

 有三个级别的存储位置 --system 所有用户(本系统) --global 当前用户(全局) --local 本地配置(当前目录), 默认使用--local
    • Configure the user name and mailbox when submitting code
   $ git config --global user.name ‘name‘   $ git config --global user.email [email protected]
2. Create a git repository
    • Initialize the current directory, create a git repository (generate a. git folder)

       $ git init
    • Create a new directory and initialize it to a git repository

       $ git init [dir-name]
    • Connecting to remote libraries
      $ git remote add `origin` https://github.com/xiangbeiw/TIM.git
      The use of the above command is wrong, indicating that a remote library has been associated locally, you can use git remote-v to view all remote library information that is currently associated.

    • Delete an existing GIT remote library
      $ git remote rm `库名称`

3. Get a git repository
如果需要克隆远程仓库可以使用git clone [url]    $ git clone  https://github.com/xiangbeiw/TIM.git
4. Submit Updates

Submission Process:

Perform the update project first

$ git pull origin master

(Sometimes the first time you create a project, you also need to perform an update, because the Readme file is sometimes added automatically when creating new projects). Multi-person cooperation need to update the project, then write code, avoid conflict!
Then first execute:

$ git add 【要上传的文件夹名称】

Re-execution:

$ git commit -m "提交的描述"

Final execution:

$ git push origin master

Complete the submission.

There are three states for each file in Git: Committed, staged, modified.

All the files in the Git repository we get are committed state, and if you modify the local file A, the status of a becomes modified, and if you use Git add a, the status of a is changed to staged, and if you use Git commit, A's state becomes committed.

There is also a file state, non-tracking status (unversioned/untracked), through git add can turn the non-tracking state into staged, through the git RM can turn staged or committed state into a tracking state.

git status

Before committing, check the status of the next file in the current git directory before checking what is modified.

$ git statusOn branch masterYour branch is up to date with ‘github/master‘.nothing to commit, working tree clean

Just submitted, very clean ...

git add

Git add can add files or directories to staging area, or you can use wildcard characters. Like what:

$ git add Readme.md    # add file only$ git add *.js         # add all js files$ git add /home/code/  # add all files in /home/code$ git add .            # add all files in /$ git add -p           # 对于同一个文件的多处变化,可以实现分次提交

Git diff

Git diff can see all the changes in the current directory.
Before submitting, it is also a separate confirmation of which files are in the staged state and that the modifications are correct. In practical applications, you may also need to use Git diff to export patches for code-for-the-daytime.
You can use Git diff--staged or git diff--cached to see the difference between staged and the last commit snapshot.

git rm

git rm deletes files from the current directory (the deleted files are not saved). If you need to remove from the Git repository but remain in the current working directory, i.e. from the tracking list, you can use:

$ git rm --cached readme.md

Git MV

Rename the file and put this name in staging area

$ git mv [file-original] [file-renamed]

Git commit

Be cautious before submitting. Calling git commit directly will pop up the editor and enter the commit log (if it's a multi-line log, recommended).
For single-line log submissions, you can use the following command:

git commit -m "add readme"git commit -v #提交时显示所有diff信息# 使用一次新的commit,替代上一次提交# 如果代码没有任何新变化,则用来改写上一次commit的提交信息git commit --amend -m [message]# 重做上一次commit,并包括指定文件的新变化$ git commit --amend [file1] [file2] ...

There is also a quick way to submit, directly skip the stage buffer, directly submit all the changes in the current directory of Git commit-a (before using this command, it is recommended to confirm that the current directory changes are correct, must).

5. Submit History View

You can use git log to view all the submitted logs for the current project.

$ git log --stat      # 仅显示摘要选项$ git log --pretty=oneline        # 定制记录格式$ git log --graph     # 图像化分支和版本更新# 显示当前分支的版本历史$ git log# 显示commit历史,以及每次commit发生变更的文件$ git log --stat# 搜索提交历史,根据关键词$ git log -S [keyword]# 显示某个commit之后的所有变动,每个commit占据一行$ git log [tag] HEAD --pretty=format:%s# 显示某个commit之后的所有变动,其"提交说明"必须符合搜索条件$ git log [tag] HEAD --grep feature# 显示某个文件的版本历史,包括文件改名$ git log --follow [file]$ git whatchanged [file]# 显示指定文件相关的每一次diff$ git log -p [file]# 显示过去5次提交$ git log -5 --pretty --oneline# 显示所有提交过的用户,按提交次数排序$ git shortlog -sn# 显示指定文件是什么人在什么时间修改过$ git blame [file]
6. Undo the update
# 恢复暂存区的指定文件到工作区$ git checkout [file]# 恢复某个commit的指定文件到暂存区和工作区$ git checkout [commit] [file]# 恢复暂存区的所有文件到工作区$ git checkout .# 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变$ git reset [file]# 重置暂存区与工作区,与上一次commit保持一致$ git reset --hard# 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变$ git reset [commit]# 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致$ git reset --hard [commit]# 重置当前HEAD为指定commit,但保持暂存区和工作区不变$ git reset --keep [commit]# 新建一个commit,用来撤销指定commit# 后者的所有变化都将被前者抵消,并且应用到当前分支$ git revert [commit]# 暂时将未提交的变化移除,稍后再移入$ git stash$ git stash pop
7. Remote Storage
$ git remote update  --更新远程仓储# 下载远程仓库的所有变动$ git fetch [remote]# 显示所有远程仓库$ git remote -v# 显示某个远程仓库的信息$ git remote show [remote]# 增加一个新的远程仓库,并命名$ git remote add [shortname] [url]# 取回远程仓库的变化,并与本地分支合并$ git pull [remote] [branch]# 上传本地指定分支到远程仓库$ git push [remote] [branch]# 强行推送当前分支到远程仓库,即使有冲突$ git push [remote] --force# 推送所有分支到远程仓库$ git push [remote] --all

You can use Git remote to view the current remote library.
Git remote-v can display the corresponding clone address. (Useful for multiple remote repositories)

Add a remote repository

git remote add [short_name] [url] to add a new repository.

Fetching data from a remote repository

$ git fetch [remote-name] #可以从远程仓库抓取数据到本地。#也可以使用`git pull`

Push data to a remote repository

git push [remote_name] [Branch_name]
Origin and master are used by default.

View Remote Warehouse Information

$ git remote show origin

Deletion and renaming of remote warehouses

$ git remote rename [old_name] [new_name]$ git remote rm [remote_name]
8. Play Tags

You can use git tag to display the tags in the current library.

# 列出所有tag$ git tag# 新建一个tag在当前commit$ git tag [tag]# 新建一个tag在指定commit$ git tag [tag] [commit]# 删除本地tag$ git tag -d [tag]# 删除远程tag$ git push origin :refs/tags/[tagName]# 查看tag信息$ git show [tag]# 提交指定tag$ git push [remote] [tag]# 提交所有tag$ git push [remote] --tags# 新建一个分支,指向某个tag$ git checkout -b [branch] [tag]

Add tags (with notes)

git tag -a v0.1 -m "my version 0.1"`使用如下命令查看Tag日志信息(指定对应标签的名字)`git show v0.1`也可使用SHA-1的提交表示创建tag:`git tag -a v0.2 [SHA-1] -m "my version 0.2"

Share tags

By default, git push does not push tag information to the remote repository and requires explicit push via commands.
Git push Origin v0.1
If you need to push all the labels, use the
Git push Origin--tags

9. Git Branch

The GIT branch is lightweight and fast, logging only the index information.

Show All Branches

Use Git branch to display all current branches.

# 列出所有本地分支$ git branch# 列出所有远程分支$ git branch -r# 列出所有本地分支和远程分支$ git branch -a

You can use--merged and--no-merged to view the branches that have been merged and not merged.

Creating and switching branches

You can switch and create a branch directly using the following command
git checkout-b Testing
Equivalent to

$ git branch testing    # 创建testing 分支,但是仍停留在当前分支$ git checkout testing  # 切换到testing分支,并更新工作区# 新建一个分支,指向指定commit$ git branch [branch] [commit]# 新建一个分支,与指定的远程分支建立追踪关系$ git branch --track [branch] [remote-branch]# 切换到上一个分支$ git checkout -# 建立追踪关系,在现有分支与指定的远程分支之间$ git branch --set-upstream [branch] [remote-branch]

Note When you switch branches, keep the working directory with no uncommitted modifications. GIT encourages the use of branches, and then merges the branches after the problem is done.

Branch Merge

Merging the hotfix branch to master (the main branch) requires the following command:

$ git checkout master$ git merge hotfix

After merging, you can use Git branch-d hotfix to delete a branch.
If there is a conflict in the merge, you need to modify it manually.

10. Other
# 生成一个可供发布的压缩包$ git archive

Thank

Upload operation

fly with the dream https://www.cnblogs.com/smfx1314/p/8426115.html

Command actions

Genius Wolong https://www.cnblogs.com/chenwolong/p/GIT.html

tocy https://www.cnblogs.com/tocy/p/git-command-line-manual.html

Basic use of Git

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.