你不知道的Git系列☞实战篇

72 阅读3分钟

前言

今天分享一个我平时用的git工具手册,助力开发,喜欢的可以点赞+收藏下 ,本篇主要介绍常见的git配置操作指令(篇幅较长可根据目录直达)

温馨提示

如对git基础概念还是特别熟悉的小伙伴可以浏览下一下相关文章恶补下

1.4 Git常用操作

image.png

1.4.1 配置

Git 用户的配置文件位于 ~/.gitconfig

Git 单个仓库的配置文件位于 ~/$PROJECT_PATH/.git/config  

--global:当前用户所有仓库生效,--local:当前Git仓库生效

列举所有配置

$ git config -l

为命令配置别名

$ git config --global alias.co checkout
 
$ git config --global alias.ci commit
 
$ git config --global alias.st status

$ git config --global alias.br branch

设置提交代码时的用户信息

$ git config [--global] user.name "[name]"

$ git config [--global] user.email "[email address]"

设置Git编辑器

$ git config --global core.editor nano/vim/gedit/emac

取消Git配置

$ git config –-unset [--global] core.editor user.name

1.4.2 初始化

--recurse-submodules:递归下载所有子模块

--branch:下载指定分支的代码

# 在当前目录新建一个Git代码库
$ git init

# 推送现有文件夹以初始化远端空仓库

$ cd existing_folder

$ git init --initial-branch=master

$ git remote add origin your_url

$ git add .

$ git commit -m "Initial commit"

$ git push -u origin master

 

# 推送现有的 Git 仓库到远端空仓库

$ cd existing_repo

$ git remote rename origin old-origin

$ git remote add origin your_url

$ git push -u origin –all (git push –u origin local_branch:remote_branch)

$ git push -u origin –tags

 

# 下载一个项目和它的整个代码历史(Git only)

$ git clone [--recurse-submodules] [url] [--branch branch_name]

1.4.3 增删文件

把文件名或者目录添加到 .gitignore 文件里,Git 会停止跟踪其状态。

# 添加当前目录的所有文件到暂存区
$ git add .

# 添加指定文件到暂存区
$ git add <file1> <file2> ...

# 添加指定目录到暂存区,包括其子目录
$ git add < dir >

# 删除工作区文件,并且将这次删除放入暂存区
$ git rm [file1] [file2] ...

# 停止追踪指定文件,但该文件会保留在工作区
$ git rm --cached [file]

# 改名文件,并且将这个改名放入暂存区
$ git mv [file-original] [file-renamed]

1.4.4 分支

# 列出所有本地分支
$ git branch
​
# 列出所有本地分支和远程分支
$ git branch –a
​
# 列出所有本地分支及其追踪的远程分支
$ git branch –vv

# 新建一个分支,但依然停留在当前分支
$ git branch [branch-name]# 新建一个分支,并切换到该分支,该分支追踪remote**-**branch
$ git checkout -b [new_branch] [remote-branch]# 切换到指定分支,并更新工作区

$ git checkout [branch**-**name]# 合并指定分支到当前分支

$ git merge [branch]# 选择一个 commit,合并进当前分支

$ git cherry-pick [commit]# 删除本地分支,-D 参数强制删除分支

$ git branch -d [branch-name]# 删除远程分支

$ git push [remote] : [remote-branch](git push origin --delete [remote _branch])

1.4.5 提交

# 提交暂存区到仓库区

$ git commit -m [message]# 提交工作区与暂存区的变化直接到仓库区

$ git commit -a

​
# 提交时显示所有 diff 信息

$ git commit -v

​
# 提交暂存区修改到仓库区,合并到上次修改,并修改上次的提交信息

$ git commit --amend -m [message]# 上传本地指定分支到远程仓库

$ git push [remote] [remote-branch]

 
# 上传本地指定分支作为远程仓库指定分支

$ git push [remote] [local-branch]:[remote-branch]

1.4.6 拉取

# 下载远程仓库的所有变动 (Git only)

$ git fetch [remote]

​
# 显示所有远程仓库 (Git only)

$ git remote -v

​
# 显示某个远程仓库的信息 (Git only)

$ git remote show [remote]

​
# 增加一个新的远程仓库,并命名 (Git only)

$ git remote add [remote-name] [url]

​
# 取回远程仓库的变化,并与本地分支合并,(Git only), 若使用 Git-SVN,请查看Git-SVN章节

$ git pull [remote] [branch]

​
# 取回远程仓库的变化,并与本地分支变基合并,(Git only), 若使用 Git-SVN,请查看Git-SVN章节

$ git pull --rebase [remote] [branch]

1.4.7 撤销(不影响未追踪文件)

# 恢复暂存区的指定文件到工作区

$ git checkout [file]

​
# 恢复暂存区当前目录的所有文件到工作区

$ git checkout .

​
# 恢复工作区到指定 commit

$ git checkout [commit]

​
# 重置暂存区的指定文件,与上一次 commit 保持一致,但工作区不变

$ git reset [file]

​
# 重置暂存区与工作区,与上一次 commit 保持一致

$ git reset --hard

​
# 重置当前分支的指针为指定 commit,同时重置暂存区,但工作区不变

$ git reset [commit]

​
# 重置当前分支的HEAD为指定 commit,同时重置暂存区和工作区,与指定 commit 一致

$ git reset --hard [commit]

​
# 新建一个 commit,用于撤销指定 commit

$ git revert [commit]

​
# 将未提交的变化放在储藏区

$ git stash


​# 将未提交的变化放在储藏区

$ git stash list


# 将储藏区的内容恢复到当前工作区

$ git stash pop

1.4.8 清理未追踪文件

# 显示将要删除的文件

$ git clean –nd


# 删除指定文件

$ git clean –f [path]

 
# 删除所有未追踪文件和目录

$ git clean –df

 
# 删除所有未追踪文件和目录(包括被.gitignore忽略的文件和目录)

$ git clean –xdf

1.4.9 查询

# 查看工作区文件修改状态

$ git status               

​
# 查看工作区文件修改具体内容   

$ git diff [file]# 查看暂存区文件修改内容

$ git diff --cached [file]# 查看版本库修改记录

$ git log                  

​
# 查看某人提交记录

$ git log --author=someone# 查看某个文件的历史具体修改内容

$ git log -p [file]        

​
# 查看某次提交具体修改内容

$ git show [commit]

1.4.10 Git-SVN

# 下载一个 SVN 项目和它的整个代码历史,并初始化为 Git 代码库

$ git svn clone -s [repository]# 查看当前版本库情况

$ git svn info# 取回远程仓库所有分支的变化

$ git svn fetch# 取回远程仓库当前分支的变化,并与本地分支变基合并

$ git svn rebase# 上传当前分支的本地仓库到远程仓库

$ git svn dcommit# 拉取新分支,并提交到远程仓库

$ svn copy [remote_branch] [new_remote_branch] -m [message]# 创建远程分支对应的本地分支

$ git checkout -b [local_branch] [remote_branch]

后言

感兴趣的小伙伴可以浏览下以下系列相关文章(欢迎点赞+关注)

文章链接