新建仓库
git init # 在当前目录下新建git仓库
git init xxx_repo # 新建一个目录并将其初始化成一个git仓库;
配置
Git的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。
git config -list # 显示当前配置
git config --global --list # 显示全局配置
git config -e [--global] #编辑config文件
git config --global user.name "xxx_name" #重要,提交或clone代码时需要
git config --global uesr.email "xxx_email"
添加/移除文件
git add [file1] [file2] ... # 添加文件到暂存区(Untracked->Staged)
git add . # 添加当前工作区目录下所有文件到暂存区
git add dir # 添加目录到暂存区
git add -p # 依次列出该工作区所有修改,选择性的添加到暂存区
git add -A # 提交所有修改
git add -f #强制添加文件到暂存区,避免使用
git rm --cache xxx_file # * -> Untracked
git rm xxx_file # 在所有“区”删除该文件
git mv src_file dst_file # 重命名并将这个重命名放入暂存区
提交代码到本地仓
git commit -m xxx_msg # 提交整个暂存区到本地仓库,并附带提交信息;
git commit [file1] [file2] ... -m xxx_msg # 提交暂存区的指定文件到本地仓库
git commit --amend -m xxx_msg #重做上次提交,如果上次提交没有代码变换,可以用来修改上一次的提交信息;
git commit --amend [file1] [file2] ... -m xxx_msg # 重做上次提交,提交暂存区指定文件到本地仓库
git commit -a # 【不建议】将工作区自上次commit之后的变化直接提交到本地仓库
远程
git remote -v # 显示所有远程仓库
git remote show xxx_remote # 显示某个远程仓库
git remote add xxx_remote_name xxx_url # 添加某个远程仓库并取名
git fetch xxx_remote # 下载远程仓库的所有变动
git merge xxx_branch # 合并指定分支到当前分支
git pull xxx_remote xxx_branch # [git fetch + merge]
git push xxx_remote xxx_branch # 上传代码到远程仓库
分支
git branch # 列出所有本地分支
git branch -r # 远程分支
git branch -a # 本地+远程
git show xxx_branchnae # 查看分支信息
git branch xxx_name # 新建分支,停留在当前分支
git checkout xxx_branchname # 切换到指定分支
git checkout - # 切换到上一个分支
git checkout -b xxx_name # 新建分支并切换到新分支
git checkout -b xxx_name xxx_tag # 新建分支指向特定tag
git branch xxx_name xxx_commit_id # 新建分支指向特定commit
git cherry-pick xxx_commit_id # 选一个commit合并到当前分支
git branch -d xxx_name #删除分支
git push origin -d xxx_branch_name #删除远程分支
标签
给指定commit打tag,tag一旦生成,位置便永远不会变.
git tag #列出所有本地标签
git show xxx_tagname # 查看分支信息
git tag xxx_tagname # 给当前commit打个tag
git tag xxx_tagname xxx_commita_id # 给指定commit 打个tag
git tag -d xxx_tagname # 删除本地指定tag
git push orgin:refs/tags/xxx_tagname # 删除远程tag
查看信息
git status # 查看有变动的文件
git status -uno #查看在缓冲区中有变动的文件
git log # 显示当前分支的版本log
git log --stat # 显示commit历史
git log -S xxx_keyword # 根据关键词搜索commit历史
git blame xxx_file # 显示指定文件的所有修改人信息
git diff # 显示暂存区和工作区的差异
撤销
git checkout branch_name #释放指定分支的所有代码到工作区
git checkout xxx_file # 恢复暂存区指定文件到工作区
git checkout xxx_commit_id xxx_file # 恢复某次commit中使用的暂存区中的指定文件到工作区
git reset xxx_file #重置暂存区指定文件,使之与上一次commit之前保持一致,工作区不变
git reset --hard # 重置暂存区和工作区
git reset xxx_commit_id # 重置当前分支指针到特定commit 工作区不变;
git reset --hard xxx_commit_id ; # 重置暂存区和工作区
git revert xxx_commit_id # 撤销指定commit
忽略文件
git config core.