GIT笔记

来源:互联网 发布:大数据时代变革 编辑:程序博客网 时间:2024/05/01 19:26

GIT笔记

上传代码到 github

echo "# zzzz" >> README.mdgit initgit add README.mdgit commit -m "first commit"git remote add origin https://github.com/username/repository.gitgit push -u origin master

linux内核代码下载

$ git clone git://git.kernel.org/pub/scm/git/git.git$ git clone http://www.kernel.org/pub/scm/git/git.githttps://github.com/notepad-plus-plus/notepad-plus-plus

git设置

# 正常显示中文文件名$ git config –global core.quotepath false# 不进行LF--CRLF转换$ git config --global core.autocrlf false# 解决git clone时显示"Filename too long"错误git config --global core.longpaths true# 设置邮箱用户名和密码$ git config --global user.email "lonet@qq.com"$ git config --global user.name wgj

git diff

# 查看以追踪但尚未暂存的文件更新了哪些部分, 不添加额外参数$ git diff# 对使用了add添加到了暂存区的内容, 使用--staged参数或者--cached$ git diff --staged$ git diff --cached [file]$ git diff --check  # 检查修改是否引入了空白字符$ git diff topic master    (1)$ git diff topic..master   (2)$ git diff topic...master  (3)1.Changes between the tips of the topic and the master branches.2.Same as above.3.Changes that occurred on the master branch since when the topic branch was started off it.

git apply

# --check只检查而不应用patch;--reject禁用原子操作,即应用可应用的patch hunk,拒绝的hunk保存在*.rej文件git apply [--check] [--reject] [<patch>…?] 

git log

# 查看最近的两次提交内容$ git log -p -2# 使用限制符-S后面紧跟要查询的字符串$ git log -Smethod_name# 或者针对一个文件进行更改的提交查询, 只需要在后面追加文件名称即可$ git log fileName# 查看20161101号到07号,苏的提交记录$ git log --pretty="%h - %s" --author=PC --since="2016-11-01" --before="2017-01-27"# 显示在test分支但是不在release分支中的提交$ git log --name-status release..test# 显示在本地master分支但是不在远程master分支中的提交$ git log master --not --remotes=*/master# Shows all commits that are in any of local branches but not in any of # remote-tracking branches for origin (what you have that origin doesn’t).$ git log --branches --not --remotes=origin# 查看将把什么推到远程,同git log origin/master..$ git log origin/master..HEAD

git commit

$ git commit -m "修改内容"# 重新修改上次提交(新添加一修改文件)$ git add project.property$ git commit --amend -m "修改内容"# 直接提交file,不经过git add$ git commit file -m "修改内容"# 取消上次commit,提交的修改还在(remove the commit entirely)$ git reset HEAD^$ git reset HEAD~1 # 同上# 取消前两次commit,提交的修改都还在$ git reset HEAD~2# 取消上次commit,用--hard丢弃提交的修改$ git reset --hard HEAD^

取消暂存的文件

对文件进行了git add操作. 这个时候可以reset# 把暂存区的更新移出到工作区中,修改内容还在(use "git reset HEAD <file>..." to unstage)$ git reset HEAD file  # 实际去掉HEAD也一样$ git reset -- file    # 同上,--表示没有选项git reset <paths> is the opposite of git add< paths>.撤销对文件的修改丢弃工作区中的修改(use "git checkout -- <file>..." to discard changes in working directory)$ git checkout -- file$ git checkout [file]  # 这个同上# 放弃当前目录所有修改,从HEAD中检出并且把它恢复$ git checkout .# 取其他commit中的test.c文件$ git checkout master~2 test.c# 恢复某个commit的指定文件到工作区$ git checkout [commit] [file]          $ git reset --mixed HEAD   # 将改动从暂存区中移除,但是改动还留在工作区中。$ git reset --hard HEAD    # 如果你想完全舍弃你没有提交的改动。$ git reset --mixed <commit_id>$ git reset --hard origin/master       # 把HEAD指向最新下载的版本git reset --hard remotes/origin/master # 应该和上面命令一样# pull 后发现有问题,取消pull$ git reset --hard ORIG_HEAD  # 回到上次状态,但会丢弃本地修改$ git reset --merge ORIG_HEAD # 回到上次状态,不会丢弃本地修改

分支切换(所谓的分支切换就是HEAD指针的指向的改变)

$ git branch dev                 # 创建了一个名为dev 的新分支,但是没有切换到dev分支$ git checkout -b dev            # 创建并且换到新分支$ git checkout -b dev origin/dev # 新建并切换到dev分支,并设置其跟踪分支$ git checkout -m dev  # 切换到dev分支,且合并当前的修改到dev# 可以通过`git log`命令查看各个分支所指向的对象$ git log --oneline --decorate# 如果你想查看图形式的分叉历史,可以这样:$ git log --oneline --decorate --graph --all

分支操作

$ git branch     # 查看本地分支信息(前带*的表示当前检出的分支)$ git branch -vv # 查看本地分支信息$ git branch (-m | -M) [<oldbranch>] <newbranch> # 修改分支名,-M是指强制# 删除dev分支,如果dev分支还有未合并到上游分支的内容则无法删除$ git branch -d dev # -D 强制删除分支# --merged 查看哪些分支已经合并到当前分支$ git branch --merged        # 一般这个列表展示的除了*号的其他分支,都可以删除$ git branch --no-merged     # 查看所有包含未合并工作的分支跟踪分支(也叫“上游分支”)是与远程分支有直接关系的本地分支,如果在一个跟踪分支上输入git pull,git 能自动地识别去哪个服务器上抓取、合并到哪个分支。创建一个跟踪分支,运行 git checkout -b [branch] [remotename]/[branch]。 git 提供了 --track 快捷方式:$ git checkout --track origin/dev# 如果想要自定义本地分支名字$ git checkout -b 任意分支名字 origin/dev# 设置HEAD指向的分支的上游为远程dev分支$ git branch -u origin/dev     # -u同--set-upstream-to$ git branch --unset-upstream  # 取消当前分支的跟踪分支----------------------------------------------------------------# 合并其他分支的一个修改$ git cherry-pick <commit-id># 整个工程PUSH到远程一个新分支$ git push origin HEAD:refs/heads/Test_Branch# 提交步骤$ git add$ git commit -m "comment"$ git push origin HEAD:refs/for/branch_name--------------------------------------------------------------从 develop 分支建一个 feature 分支,并切换到 feature 分支$ git checkout -b myfeature developSwitched to a new branch "myfeature"合并feature 分支到 develop$ git checkout developSwitched to branch 'develop'$ git merge --no-ff myfeature  # 禁用fast-forwardUpdating ea1b82a..05e9557(Summary of changes)$ git branch -d myfeatureDeleted branch myfeature$ git push origin develop

stash

有时,在一个分支上做了一些工作,修改了很多代码,而这时需要切换到另一个分支干点别的事。但又不想将只做了一半的工作提交。stash 将工作区与暂存区中的内容做一个提交,保存起来,然后使用reset hard 选项恢复工作区与暂存区内容。我们可以随时使用 stash apply 将修改应用回来。stash 实现思路将我们的修改提交到本地仓库,使用特殊的分支指针(.git/refs/stash)引用该提交,然后在恢复的时候,将该提交恢复即可。git stash list [<options>]            # 列出stash(带<message>)git stash show [<stash>]              # 列出最新的一个stashgit stash show -p [<stash>]           # 列出最新的一个stash(还详细修改内容)git stash drop [-q|--quiet] [<stash>]            # 删除一个stash,默认为stash@{0}git stash pop [--index] [-q|--quiet] [<stash>]   # 还原代码并且删除stashgit stash apply [--index] [-q|--quiet] [<stash>] # 还原代码但不删除stashgit stash branch <branchname> [<stash>]git stash [save [--patch] [-k|--[no-]keep-index] [-q|--quiet] [-u|--include-untracked] [-a|--all] [<message>]] # 添加到stashgit stash clear

清理未跟踪的文件和文件夹

$ git clean -fdx [path]

git rm

$ git rm file# 如果文件修改,并添加了暂存区, 需要使用-f参数来强制删除(force)$ git rm -f file# 可以使用glob模式,如下$ git rm log/*.log         # 删除log目录下所有名称是.log结尾文件$ git rm *~                # 删除以~结尾的所有文件

打包并压缩

$ git archive --format=tar --prefix=git-1.4.0/ v1.4.0 | gzip >git-1.4.0.tar.gz

repo操作

repo forall -c git reset --hard HEADrepo forall -c git clean -fdxrepo forall -p -c git branch         # 打印出每个仓库的路径repo sync -cdx  # 更新同时清理工程repo init -urepo sync -c --no-tags -j8repo start branch_name --allgit push caf HEAD:refs/for/branch_namerepo upload .

其他

-n或--no-tags 不下载tag,fetch不会合并更新到当前分支,后续还要merge$ git fetch origin$ git fetch origin master # 只fetch master分支$ git merge origin/master$ git pull origin master----------------------------------------------------------------merge会自动提交;当前工作区最好不要有未提交的修改$ git merge -m <msg> <commit>.... # 用msg代替自动merge说明$ git merge --no-commit dev # merge dev分支到当前分支但是不自动提交$ git merge --abort         # is equivalent to git reset --merge when MERGE_HEAD is present.----------------------------------------------------------------Interrupted workflow$ git checkout feature ;# you were working in "feature" branch and$ work work work       ;# got interrupted$ git commit -a -m "snapshot WIP"                 (1)$ git checkout master$ fix fix fix$ git commit ;# commit with real log$ git checkout feature$ git reset --soft HEAD^ ;# go back to WIP state  (2)$ git reset                                       (3)1.This commit will get blown away so a throw-away log message is OK.2.This removes the WIP commit from the commit history,  and sets your working tree to the state just before you made that snapshot.3.At this point the index file still has all the WIP changes you committed as snapshot WIP.  This updates the index to show your WIP files as uncommitted.--------------------------------------------------------------Undo a commit and redo$ git commit ...$ git reset --soft HEAD^      (1)$ edit                        (2)$ git commit -a -c ORIG_HEAD  (3)1.This is most often done when you remembered what you just committed is incomplete,  or you misspelled your commit message, or both. Leaves working tree as it was before "reset".2.Make corrections to working tree files.3."reset" copies the old head to .git/ORIG_HEAD; redo the commit by starting with its log message.   If you do not need to edit the message further, you can give -C option instead.--------------------------------------------------------------$ tree -L 1.|-- HEAD         # 这个git项目当前处在哪个分支里|-- config       # 项目的配置信息,git config命令会改动它|-- description  # 项目的描述信息|-- hooks/       # 系统默认钩子脚本目录|-- index        # 索引文件|-- logs/        # 各个refs的历史信息|-- objects/     # Git本地仓库的所有对象 (commits, trees, blobs, tags)`-- refs/        # 标识你项目里的每个分支指向了哪个提交(commit)。--------------------------------------------------------------rebaseAssume the following history exists and the current branch is "topic":          A---B---C topic         /    D---E---F---G masterFrom this point, the result of either of the following commands:git rebase mastergit rebase master topicwould be:                  A'--B'--C' topic                 /    D---E---F---G mastergit rebase [--onto <newbase>]   [<upstream> [<branch>]]transplant a topic branch based on one branch to another, to pretend that you forked the topic branch from the latter branchFirst let’s assume your topic is based on branch next. For example, a feature developed in topic depends on some functionality which is found in next.    o---o---o---o---o  master         \          o---o---o---o---o  next                           \                            o---o---o  topicWe want to make topic forked from branch master; for example, because the functionality on which topic depends was merged into the more stable master branch. We want our tree to look like this:    o---o---o---o---o  master        |            \        |             o'--o'--o'  topic         \          o---o---o---o---o  nextWe can get this using the following command:git rebase --onto master next topic
0 0