概念
- Git: A source code versioning system.
- GitHub,Gitlab: Are services that provides remote access to Git repositories. In addition to hosting your code, the services provide additional features designed to help manage the software development lifecycle. Basic Command
Github使用小技巧
- diff时忽略空格, URL后面添加?w=1
- 查看某个作者的提交历史,URL后面添加?author=username
- 快速引用, 可以选中别人的评论文字,按下r, 这些内容会以引用的形式被复制在文本框中
https ssh
区别
1.克隆项目到本地时,https url 最方便,而SSH url克隆则必须在之前先配置和添加SSH key(必须是该项目的拥有者或是管理员,否则无法添加SSH key)
2.https url 在push时需要验证用户名和密码,SSH 在push时不需要输入用户名,如果配置SSH key的时候设置了密码,则需要输入密码,否则直接是不需要输入密码的
生成并添加第一个ssh key
cd ~/.ssh
ssh-keygen -t rsa -C “mlding@thoughtworks.com"
这时可以一路回车,不输入任何字符,将自动生成id_rsa和id_rsa.pub文件。
-t 指定密钥类型,默认是 rsa ,可以省略。
-C 设置注释文字,比如邮箱。
-f 指定密钥文件存储文件名。
以上代码省略了 -f 参数,因此,运行上面那条命令后会让你输入一个文件名,用于保存刚才生成的 SSH key 代码.当然,你也可以不输入文件名,使用默认文件名(推荐),那么就会生成 id_rsa 和 id_rsa.pub 两个秘钥文件。
ssh -T git@github.com(测试SSH key生成好了)
Git 使用
- git reflog 看到所有提交或删除记录
- git config —list(看git相关配置)
- git config --global user.name "[name]": Set the name you want atached to your commit transactions
- git config --global user.email "[email address]" : Set the email you want atached to your commit transactions.
- git config --global core.ignorecase false : 设置git对大小写敏感.
- git config —global alias.ci commit (git ci === git commit)
- git config —global alias.last ‘log -1 HEAD’ (git last可看到最后一次提交)
create repository
- $ git init [project-name] : Create a new local repository with the specified name.
- $ git clone [url] : Download a project and its entire version history
make changes
- git status
- git add [file]
- 使用 git diff 命令可以查看工作区与暂存区之间的差异。
使用 git diff <gitreversion> 命令可以查看工作区与指定版本之间的差异。
使用 git diff --cached 命令可以查看暂存区与当前 HEAD 指针指向版本之间的差异。
使用 git diff --cached <gitreversion> 命令可以查看暂存区与指定版本之间的差异。
使用 git diff -- <file> 可以查看特定文件在工作区与暂存区之间的差异。
使用 git diff <gitreversion> -- <file> 可以查看特定文件在工作区与指定版本之间的差异。
使用 git diff --cached -- <file> 可以查看特定文件在暂存区与当前 HEAD 指针指向版本之间的差异。
使用 git diff --cached <gitreversion> -- <file> 可以查看特定文件在暂存区与指定版本之间的差异。
- git reset [file] : Unstage the file, but preserve its contents.
- git commit -m "" : Record file snapshots permanently in version history.(message中可以添加一些emoji的图片。Example,git commit -m “init the app @mlding")
- git commit - -amend - -no-edit
- git commit - -amend -m ’new commit message'
- git commit -a这个命令可以直接提交所有修改,省去了你git add和git diff和git commit的工序 注意:无法把新增文件或文件夹加入进来,所以,如果你新增了文件或文件夹,那么就要老老实实的先git add .,再git commit
branch and merge
- git branch: List all local branches in the current repository
- git branch [branch-name] : Create a new branch
- git checkout [branch-name] : Switche to the specified branch and updates the working directory
- git merge [branch] : Combine the specified branch’s history into the current branch
- git branch -d [branch-name] : Delete the specified branch
- git branch |grep 'daily' |xargs git branch -D
这是通过 shell 管道命令来实现的批量删除分支的功能
git branch 输出当前分支列表
grep 是对 git branch 的输出结果进行匹配,匹配值当然就是 daily
xargs 的作用是将参数列表转换成小块分段传递给其他命令
因此,这条命令的意思就是:从分支列表中匹配到指定分支,然后一个一个(分成小块)传递给删除分支的命令,最后进行删除。
- git branch --set-upstream-to=origin/: when add branch local,remeber add branch to remote before pull--rebase git branch -d xx //删除分支,如果还未合master,则会出现不能删除提示,用-D可以强制删除 git branch -v //查看分支的最后修改 分支前带的为没有合并分支到master分支的,无的为已经合并到master中,可以将无的分支删除掉。
- git branch -m old-name new-name(rename your local branch)
- git branch -D pr-mob-910 后想恢复该分支:git reflog(查看要恢复的仓库版本号)—git branch pr-mob-910 commitId(就把要恢复的分支内容恢复到了pr-mob-910分支中了)
refactor the fileName
- $ git rm [file] : Delete the file from the working directory and stages the deletion
- $ git rm --cached [file] : Remove the file from version control but preserves the file locally
- $ git mv —force [file-original] [file-renamed] : Change the file name and prepares it for commit
- git rm -r --cached .idea
review history
- git log --oneline: List version history for the current branch
- git diff [first-branch]...[second-branch] : Show content differences between two branches
- git show [commit] : Output metadata and content changes of the specified commit
save local changes
- $ git stash : Temporarily store all modified tracked(modified but not committed) files
- stash only certain files: 1. git add files you don’t want to stash; 2. git stash save —keep-index
- $ git stash : List all stashed files
- $ git stash pop: Restore the most recently stashed files
- $ git stash drop: Discard the most recently stashed files
tag
- git tag -a 'v8' -m 'fix':Create an annotated tag.
- git show-ref --tag
- git tag: List the available tags.
- git push origin v1.5: 分享标签到远端仓库
- git push origin --tags: 一次推送所有本地新增的标签上去
- git checkout tag_name : Check out the code of this tag
- git tag -d [tag name] : Delete tag
- git push origin :refs/tags/tag名字
- git tag | grep "publish" |xargs git tag -d(批量删除本地tag)
cherry-pick:
在本地 master 分支上做了一个commit 如何把它放到 本地 old_cc 分支上
- git checkout old_cc
- git cherry-pick commit-id
git pull/git push
- git pull <远程主机名> <远程分支名>:<本地分支名>
- git push <远程主机名> <本地分支名>:<远程分支名>
- git push origin —delete MOB 255==git push origin :MOB 255(删除远程分支)
git rebase(rewrite history)
- p, pick = use commit
- r, reword = use commit, but edit the commit message
- e, edit = use commit, but stop for amending
- s, squash = use commit, but meld into previous commit
- f, fixup = like "squash", but discard this commit's log message
- x, exec = run command (the rest of the line) using shell
Tips
Tip0 批量删除git 本地分支、远程分支、tag
批量删除本地分支
git branch -a | grep -v -E 'master|develop' | xargs git branch -D
批量删除远程分支
git branch -r| grep -v -E 'master|develop' | sed 's/origin\///g' | xargs -I {} git push origin :{}
如果有些分支无法删除,是因为远程分支的缓存问题,可以使用git remote prune
批量删除本地tag(删除前现在本地拉取远程所有tags)
git tag | xargs -I {} git tag -d {}
批量删除远程tag
git tag | xargs -I {} git push origin :refs/tags/{}
用到命令说明
grep -v -E 排除master 和 develop
-v 排除
-E 使用正则表达式
xargs 将前面的值作为参数传入 git branch -D 后面
-I {} 使用占位符 来构造 后面的命令
Tip1. 改变commit历史记录
- 改变commit 邮箱:
git rebase -i head~2
change commit from pick to edit
git commit —amend —author=“"
git rebase —continue
如果要改多个commit的author email,可以继续重复改
git rebase —continue(The rebase would complete.)
- amend change to history commit(not the last)
git rebase -i head~2
change commit from pick to edit
git stash pop(当前的改动)
git commit —amend(添加到edit的那个commit上面了)
git rebase —continue
- rewrite history commit message
git rebase -i head~2
change commit from pick to edit
git commit —amend -m ‘ '
git rebase —continue
- 合并 commit
git rebase -i head~2
change commit from pick to squash :eq
input new message
Tip2. delete all local branches except master
git branch | grep -v "master" | xargs git branch -D
Tip3. git config core.autocrlf
- Carriage-Return(回车)
- Line-Feed(换行)
- CRLF(windows-style) 表示句尾使用回车、换行rn
- LF(unix-style) 表示句尾只使用换行
- CR(mac-style) 表示句尾只是用回车
Windows上的msyspit默认设置了autocrlf为true,这样在提交时自动的把行结束符CRLF转换成LF,而签出代码时把LF转换成CRLF,这样保证了在Windows平台上提交的代码都是以LF作为行结束符; Linux平台上,git默认设置autocrlf为false,即它不会自动处理CRLF;x is CRLF(windows) or LF(unix-style), -> is file to commit -> repository -> checked out file:
- autocrlf = true (if you have unix-style, LF in one of your files):x -> LF -> CRLF
- autocrlf = input (if you have windows-style, CRLF in one of your files): x -> LF -> LF
- autocrlf = false (never): x -> x -> x
git reset || git checkout || git revert :
易混淆命令对比
git rm
VS rm
- 用
git rm
来删除文件,同时还会将这个删除操作记录下来,git rm 删除过的文件,执行git commit -m "abc"
提交时,会自动将删除该文件的操作提交上去 - 用
rm
来删除文件,仅仅是删除了物理文件,没有将其从 git 的记录中剔除。用 rm 命令直接删除的文件,执行git commit -m "abc"
提交时,则不会将删除该文件的操作提交上去。不过不要紧,即使你已经通过 rm 将某个文件删除掉了,也可以再通过 git rm 命令重新将该文件从 git 的记录中删除掉,这样的话,在执行 git commit -m "abc" 以后,也能将这个删除操作提交上去。
在被 git 管理的目录中删除文件时,可以选择如下两种方式来记录删除动作:
- rm + git commit -am "abc"
- git rm + git commit -m "abc"
另外,git add . 仅能记录添加、改动的动作,删除的动作需靠 git rm 来完成。
最后,rm 删除的文件是处于 not staged 状态的,
也就是一种介于 “未改动” 和 “已提交过” 之间的状态。
对比图:
git reset
vs git checkout
vs git revert
vs git reset
- git reset
- git checkout (can manipulate commits and individual files)
- git revert (used to undo changes on a public branch)
- git reset (reserved for undoing changes on a private branch)
git reset
commits:
git reset --hard [commit] : Discard all history and changes back to the specified commit
git reset(此为默认方式—mixed,不带任何参数的git reset,就是这种方式,它回退到某个版本,只保留源码,回退commit和暂存区信息)
git reset —soft(会退到某个版本,只回退commit,不会恢复暂存区信息)
git reset --hard e76855b(撤销到e76855b指定的那个commit来,在此之后的commit全都删除了,修改也没有)
git reset e76855b(撤销到e76855b指定的那个commit来,在此之后的commit全都删除了,但是在此之后的修改都会退到暂存区)
files:
git reset HEAD~2 foo.py
The --soft, --mixed, and --hard
flags do not have any effect on the file-level version of git reset, as the staged snapshot is always updated, and the working directory is never updated.
git checkout
git checkout pr-mob-910(switch between branched)
git checkout HEAD~2(since there is no branch reference to the current HEAD, this puts you in a detached HEAD state. This can be dangerous if you start adding new commits because there will be no way to get back to them after you switch to another branch. For this reason, you should always create a new branch before adding commits to a detached HEAD.)
files:
git checkout HEAD~2 foo.py
Questions
Q1.本地一个工程目录推送到远端github仓库:
- 本地该工程目录下:git init(创建一个新的git版本库,这个版本库的配置,存储等信息都会被保存到.git文件夹中,如果不想该目录被git管理,rm -rf .git)
- git remote add origin git@github.com:miyading/redux-test.git
Q2. ~ VS ^
- Both ~ and ^ on their own refer to the parent of the commit (~~ and ^^ both refer to the grandparent commit, etc.)
- But they differ in meaning when they are used with numbers:
- ~2 means up two levels in the hierarchy, via the first parent if a commit has more than one parent
- ^2 means the second parent where a commit has more than one parent (i.e. because it's a merge)
- These can be combined, so HEAD~2^3 means HEAD's grandparent commit's third parent commit.
Q3. transfer A(local commits) to B
A :git daemon --export-all --base-path=.
B :git pull --rebase git://[B's IP Address]/
Q4. git process occupied rm -f ./.git/index.lock Generally such problems occurs when you execute two git commands simultaneously maybe one from command prompt and one from IDE.
Q5. Git has a limit of 4096 characters for a filenamegit config --system core.longpaths true
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。