「Git」- 快速开始、常用操作、基本配置

  CREATED BY JENKINSBOT

克隆远程仓库到本地:

git clone "protocol://path/to/repo"

git clone "protocol://path/to/repo" "/path/to/local/folder" # 检出到特定目录

检查到本地非空目录(git – How do I clone into a non-empty directory?):

// fatal: destination path '.' already exists and is not an empty directory

git init
git remote add origin PATH/TO/REPO
git fetch
git reset origin/master  # Required when the versioned files existed in path before "git init" of this repo.

# 如果本地不存在,则重置文件
git reset --hard

# 清理已经版本控制的本地文件
git checkout -t origin/master

在命令行中,无密码检出(How to give username/password to git clone in a script, but not store credentials in .git/config):

# 方法一、直接指定密码

git clone https://user:password@github.com/name/repo.git

# 方法二、自定义密码程序

cat > /tmp/git-ask-pass.sh <<EOF
#!/bin/sh
exec echo "$GIT_PASSWORD"
EOF

GIT_ASKPASS=/tmp/git-askpass-helper.sh GIT_PASSWORD=password git clone "https://username@hostname/repo"

第二步、提交修改(git commit)

git add .
git commit -m "Initial Commit."

修改在提交信息中的邮箱(How can I change the author (name / email) of a commit?):

git commit --amend --author="k4nz <k4nzdroid@163.com>"

在提交时,关闭钩子(githooks – Skip Git commit hooks):

git commit --no-verify

将文件添加到当前提交(How to add a file to the last commit in git?):

git add "/path/to/file"
git commit --amend --no-edit

第三步、推送远程(git push)

推送远程仓库:

git push origin master

同时推送到多个远程仓库(bjmiller121/multiple-push-urls.md):

#1、添加额外的远程仓库:
git remote set-url --add --push origin https://example.com/demo/foo.git

#2、然后推送到远程仓库(此时可以推送到两个远程仓库):
git push origin master

#3、查看已有的远程仓库:
git remote -v

#4、删除某个推送地址(有时候还是编辑配置文件更快)
git remote set-url --delete --push origin https://example.com/demo/foo.git

创建标签(Tag)

How To Delete Local and Remote Tags on Git – devconnected

# 移除查看所有tag
git tag

# 增加
git tag <tagname>
git tag <tagname> --message ".." # 添加注解
git tag <tagname> -a # 打开编辑器,以添加注解

# 删除
git tag --delete v1.0
git push --delete origin v1.0

CMD DESC
git push origin –tags 全部TAG推送到远程的仓库中
git push origin <tagname> 将特定TAG推送到远程分支
git diff branchA…branchB 对比分支间的差异

获取 TAG 所指向提交的哈希值(SHA1)(git – Get the commit hash for a tag):

git rev-parse "v1.25.0^{}" # git rev-parse "tag^{}"

git rev-parse "v1.25" # 用于获取 TAG 自身的 SHA1

代码合并

git-merge,

-m <msg>
设置用于合并提交的说明信息(如果一个已经创建)。
如果指定了–log选项,被合并的提交的短记录(shortlog)将被附加到指定的消息。
git fmt-merge-msg命令可以用来为自动git merge调用提供一个很好的默认值。自动化的消息可以包含分支描述。

更新仓库

# --all:获取所有的远程分支。
# --tags:同时获取所有的远程tags。
# --prune:fetch前移除那些在remote中已经不存在的分支。
git fetch --all --tags --prune

文件,恢复,重置(file)

查看,某次提交,特定文件,内容:

# git show REVISION:/path/to/file

// 将内容写入文件

# git show REVISION:/path/to/file > /path/to/file.copy

复制其他分支的文件

How do I copy a version of a single file from one git branch to another?
Hard reset of a single file
git: checkout files from another branch into current branch (don’t switch HEAD to the other branch) – Stack Overflow

git checkout master "path/to/file.txt"
git checkout origin/master "path/to/file.txt"
git checkout "commit-id" "path/to/file.txt"

使用 branchSrc 覆盖 branchDst(复制分支的全部文件)

git checkout "other-branch-name" -- .

恢复意外重置

Recover from git reset –hard? – Stack Overflow

$ git reflog show

4b6cf8e (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: reset: moving to origin/master
295f07d HEAD@{1}: pull: Merge made by the 'recursive' strategy.
7c49ec7 HEAD@{2}: commit: restore dependencies to the User model
fa57f59 HEAD@{3}: commit: restore dependencies to the Profile model
3431936 HEAD@{4}: commit (amend): restore admin
033f5c0 HEAD@{5}: commit: restore admin
ecd2c1d HEAD@{6}: commit: re-enable settings app

# assuming you want to get back to 7c49ec7 (restore dependencies to the User model)

$ git reset HEAD@{2}

获取帮助(git help -a)

git list all available commands

显示全部可用的 GIT 命令,或执行 ls -l /usr/lib/git-core/ 命令。

调试

debugging – How can I debug git/git-shell related problems? – Stack Overflow

GIT_CURL_VERBOSE=1 GIT_TRACE=1 git pull origin master

相关文章

多长的哈希才能被视为唯一标识?

How much of a git sha is *generally* considered necessary to uniquely identify a change in a given codebase?
Wikipedia/Birthday problem

仓库命名约定

Is there a naming convention for git repositories?
Devising a Git Repository Naming Convention