关于GIT

Git 开始使用前,应设置好全局的用户信息。

git config --global user.name "yourname"
git config --global user.email "name@email.com"

其他重要配置

#添加文件到git仓库时,自动把crlf变成lf
git config --global core.autocrlf true
#关闭mode检查
git config --global core.filemode false
#查看当前配置
git config -l

git config xxx 也可在单独仓库中指定特定的用户,去掉 --global 在仓库目录中执行。

推荐使用 Github Desktop 管理 git 仓库。

Github Desktop 在管理非 Github 仓库时,显示的默认头像可在 Gravatar 进行更换。

新仓库操作流程

git clone https://user@github.com/user/repo.git
cd repo
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

批量删除本地分支

git branch -a | grep -v -E 'master' | xargs git branch -D

命令详解

创建版本库

#Start a new repository in current directory
git init
#Clone a repository
git clone <repository-url>

提交文件

#Add all files of current directory
git add .
#Add specific file
git add <path/file.name>
#Add files matching pattern
git add Documentation/\*.txt
#Commit all your local changes
git commit -am “<message>”

检查状态

#Files changed in working directory
git status
#History of changes
git log
#Pretty one line log with encestry graph
git log —-all —-graph —-oneline
#Check what are not merged in 2 branches
git log branch1..branch2

# 列出指定版本到当前修改过的文件列表
git diff --name-only xxxx

更新
执行git pull命令远程命令来同步自己的本地仓库

#Fetches latest changes from origin
git fetch
#Fetches latest changes from origin and merge
git pull

发布

#Push changes to origin
git push
#Push changes to origin
git push <origin> <branch>

分支下的操作

#创建分支
git branch <branch-name>
#列表list your available branches
git branch
#list your available branches including remote branches
git branch -a
#see the last commit on each branch
git branch -v

#切换分支
git checkout <branc-hname>
#创建并立即切换到分支
git checkout -b <branch-name>
#Getting remote branch to local
git fetch
git branch <new-branch-name> origin/<new-branch-name>
#Pushing local branch to remote
git push -u origin <branch-name> (assuming you are on the new branch now)
#Merging branches
#<branch-name> will be merged into current branch
git merge <branch-name> 
#Deleting branch
#delete a local branch
git branch -d <branch-name> 
#delete a remote branch
git push <remote-name> :<branch-name> 

远程操作

#List of remotes
git remote -v
#Adding remote
git remote <name> <url>
#Example adding assembla repo as a new remote
git remote assembla https://assembla/repo/path
#Update from/publish to a specific remote
#Fetching from a specific remote
git fetch <remote-name>
#Fetching from a specific remote and branch
git push <remote-name> <branch-name>

修复错误或冲突

#检查当前版本文件变化与不同 Check changes/differences
git diff = Changes to tracked files
git diff $id1 $id2  = What changed between 2 commits
#放弃所有修改 Discard all modifications
git checkout — <filename>

#还原最后的一次(或特定的)提交 Revert last (or specific) commit
#git revert 生成一个新的提交来撤销某次提交,此次提交之前的commit都会被保留
git revert HEAD
git revert <commit-ref>
#修复最后一次提交 Fix last commit
git commit –amend [-m "updated message"]

#取消某个文件的暂存 Unstage staged modifications of a file
#git reset 回到某次提交,提交及之前的commit都会被保留,但是此次之后的修改都会被退回到暂存区
git reset HEAD <file>

#Resolving with our/theirs
git checkout –ours <path/file>
git checkout –theirs <path/file>


#本地所有修改的,未提交的,都返回到原来的状态
git checkout .

撤销已经commit但是还没有push的代码

git reset --hard [id]
# 完成撤销,同时将代码恢复到id对应的版本

[id] 指:

彻底删除上几次提交

git reset --hard xxxxxxx
git push --force

忽略已经被提交的文件

git rm --cached file.txt
git rm -r --cached folder

# 在.gitignore中添加要忽略的文件

检出指定目录或文件

#参考: http://jasonkarns.com/blog/subdirectory-checkouts-with-git-sparse-checkout/

#初始化空仓储:
mkdir <repo> && cd <repo>
git init
git remote add –f <name> <url>

#打开sparse-checkout特性:
git config core.sparsecheckout true

#配置.git/info/sparse-checkout,列出你想要checkout的目录:
echo some/dir/ >> .git/info/sparse-checkout
echo another/sub/tree >> .git/info/sparse-checkout

#从远端获取代码:
git pull <remote> <branch>

如果是已存在的版本库,配置同上,然后再运行git read-tree即可

git read-tree -mu HEAD

tag

#创建tag
git tag [tag名]

#推送tag
git push --tags

#删除tag
git tag -d [tag名]

#列出远程tag
git ls-remote --tags origin

其他

#只对github.com代理
git config --global http.https://github.com.proxy socks5://127.0.0.1:1080

#取消代理
git config --global --unset http.https://github.com.proxy

# 有时报证书错误,可以通过关闭SSL验证来解决
git config http.sslVerify false

# 可以全局关闭SSL验证
git config --global http.sslVerify false

# 项目目录下对应本地分支的文件所在地,需要清理本地无用分支,可删除对应文件
.git/refs/heads

常见问题

当你准备在拉取线上仓库的代码时,出现大量的提交

  • 原因:远程分支已近被删除了,但是你本地还有此分支,在拉取时相当于把该分支又推送到远程。
  • 解决方法:将此分支删除,重新创建即可

问题延伸,在服务器上也可能存在类似问题,请按下面步骤尝试。

  1. git reset HEAD
  2. git checkout .
  3. git checkout master
  4. git branch -D develop
  5. git pull
  6. git checkout develop

Useful Links

http://git-scm.com/
http://gitref.org/
http://gitready.com/
Common git commands for everyday use
https://github.com/oldratlee/translations/tree/master/git-workflows-and-tutorials http://learngitbranching.js.org/

 10,594 total views,  2 views today

Revisions

No comments yet.

发表评论