git操作:常用版本控制技巧汇总

导读:本文结合个人的常用git操作场景,梳理和总结了常用版本控制技巧,便于随时翻看和使用。

1 配置git

配置默认文本编辑器(text editor)。windows党还是自觉使用Notepad吧,免费又熟悉

  • 下载安装Notepad++(默认安装路径)

  • 打开GitBash,并输入如下代码

$ git config --global core.editor "'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

配置合并检查工具(mergetool)。这个必须要图形化GUI的,p4merge就是一个不错的选择。

  • 下载并安装p4merge。默认安装路径为"C:Program Files\Perforce\p4merge.exe"

  • 打开GitBash,并输入如下代码:

$ git config --global diff.tool p4merge
$ git config --global difftool.p4merge.path 'C:\Program Files\Perforce\p4merge.exe'

$ git config --global merge.tool p4merge
$ git config --global mergetool.p4merge.path 'C:\Program Files\Perforce\p4merge.exe'

2 常用操作

2.1 合并分支branch

$ git checkout master
$ git merge hotfix

2.2 删除本地分支

$ git branch -d branch_name
$ git branch -D branch_name

2.3 删除远程分支

$ git push <remote_name> --delete <branch_name>
$ git push <remote_name> :<branch_name>

3 gitbash常用文件及文件夹命令

  1. cd: change directory的简写,改变目录的意思,就是切换到哪个目录下, 如 cd e:切换 E 盘下面的fff 目录。

  2. cd ..: 回退到上一个目录。我们在写js,引入文件时,.. 表示的就是上一个目录, 所以 cd .. 回退到上一个目录就很好理解了。注意,cd 和两个点点..之间有一个空格,

  3. pwd: print working directory, 打印工作目录,它会显示我们当前所在的目录路径。

  4. ls: list, 列出当前目录中的所有文件, 只不过ll(两个ll)列出的内容更为详细。

  5. touch: 新建一个文件如 touch index.js就会在当前目录下新建一个index.js文件。

  6. rm: remove,删除一个文件, rm index.js 就会把index.js文件删除.

  7. mkdir: make directory 新建一个目录,就是新建一个文件夹. 如mkdir src 新建src 文件夹.

  8. rm -r : 删除一个文件夹, r (recusive 是递归的意思), 删除用的就是递归,先删除文件夹里面的内容,再删除文件夹。 rm -r src 删除src目录。

  9. mv 移动文件, mv index.html src index.html 是我们要移动的文件, src 是目标文件夹,当然, 这样写,必须保证文件和目标文件夹在同一目录下.

  10. reset 清屏,把git bash命令窗口中的所有内容清空。

  11. exit 可以直接退出窗口,

4 常用git命令

  1. 视图化查看提交历史、各个分支的指向以及项目的分支分叉情况
$ git log --oneline --decorate --graph --all

5 git 新挑战

5.1 超越github文件容量限制(单个文件50M以上)

挑战的问题:git bash 命令push时,如果单个文件大于50M以上,则会提示推送失败。

参考的解决办法包括:

实际的解决办法为:

# git bash下确认是否成功安装

$ git lfs install
> Git LFS initialized.
# 追踪大容量文件所在的文件夹
$ git lfs track "data-process/"
  • 提交并推送LFS
# 添加文件夹
$ git add data-process/

# 提交并推送
$ git commit -m "add LFS directory data-process/"
$ git push origin master

5.2 忽略文件设置

工作思路是:

  • 在工作目录下新建.gitigore文件
$ touch .gitignore
  • git bash调用文本编辑工具(windows死粉的notepad++),编辑并设置.gitignore文件
# 配置notepad(64bit system -windows)
$ alias notepad="/c/Program\ Files\ \(x86\)/Notepad++/notepad++.exe"

# 打开编辑器,设置忽略规则
$ notepad .gitignore
  • 忽略规则的一个样例
# ignore the whole file directory

data-raw/

# ignore Rproj user info file

.Rproj.user

5.3 忽略文件无效的处理办法

有时候.gitignore里虽然进行了修改设定,但并不会马上生效。

此时,问题可能就出在缓存上(cash)(参看My .gitignore file is ignored by git and it does not work,或者参看Gitignore is not Working)。

解决办法是清理缓存-提交:

$ git rm -r --cached .
$ git add .
$ git commit -m "Untracked files issue resolved to fix .gitignore"

场景实例:(两台设备上)git操作时反复出现.Rproj.user/shared/notebooks/paths

处理办法(参考):

# Once you add files to git, it will keep tracking them,  
# so we have to delete them and commit your deletion
git rm -r --cached .Rproj.user/**

# Commit the deleted files
git commit -m "Removed files...."

# now add it to the `.gitignore` and the files will be ignored
echo '.Rproj.user/**' > .gitignore

要点:**表示文件夹。

5.4 无法把本地repo推送到远程repo

这种情形下,直接push会提示如下报错:

git error: failed to push some refs to 'https://github.com/

问题可能有多个:

  • 没有进行远程设置。可以通过如下代码查看是否进行了远程设置。
# 正常情况下显示结果如下:
$git remote -v
myOrigin ssh://git@example.com:1234/myRepo.git (fetch)
myOrigin ssh://git@example.com:1234/myRepo.git (push)


# 否则就应该进行远程设置:
$git remote add origin ssh://git@example.com:1234/myRepo.git
  • 然后再进行推送:
# origin是远端,master是本地分支
$git push origin master
# 报错信息
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:huhuaping/netlify.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
# 同步远端和本地
$ git pull origin master

# 如果显示冲突conflict,则利用合并工具处理
$ git mergetool

5.5 分支push找不到上游分支

如果是本地新建分支,则需要设定上游分支,否则push就会报错:

# 报错信息。
$ git push
fatal: The current branch dev has no upstream branch.
To push the current branch and set the remote as upstream, use

处理办法是:

$ git push --set-upstream origin dev

5.6 回到远端状态

回到远端状态(例如,本地已提交,但是push提示合并冲突时,想把本地pull为远端状态)。参看队长问答

git reset --hard origin/<your branch>

5.7 放弃本地的修改(未提交)

参看队长问答.

#删除remove所有untracked files
git clean -df

#清除clear 所有untracked files
git checkout -- .

#保存痕迹
git stash save --keep-index --include-untracked

# 不保存痕迹
git stash save --keep-index

5.8 git 查看commit历史

查看历史commit:

git log --pretty=oneline
git log --oneline --decorate --graph --all

5.9 删除已提交记录中的文件并添加到ignore列表

参看队长问答

First of all your files are already committed so you have to remove it from the repo:

# Once you add files to git, it will keep tracking them,  
# so we have to delete them and commit your deletion
git rm -r --cached .Rproj.user/**

# Commit the deleted files
git commit -m "Removed files...."

# now add it to the `.gitignore` and the files will be ignored
echo '.Rproj.user/**' > .gitignore
Hu Huaping
Hu Huaping
PhD on Agricultural Economic and Management

My research interests include Data Science, Statistics, Agricultural Economics and Management.

Related