Git常用操作
一、概括
git fetch
是将远程主机的最新内容拉到本地,用户在检查了以后决定是否合并到工作本机分支中。
git pull
是将远程主机的最新内容拉下来后直接合并,即:git pull = git fetch + git merge
,这样可能会产生冲突,需要手动解决。
二、初始化
-
方式一:本地项目push到服务器
-
在本地建立Git版本库:
-
初始化
git init
-
添加.gitignore文件
touch .gitignore .class .classpath .project build bin .bak .settings Thumbs.db .DS_Store ._.DS_Store logs target *.iml .idea
-
添加README.md文件
touch README.md ### 一、概述
-
提交到本地仓库
git add . git commit -m "first commit" # 添加并提交 # git commit -am "first commit"
-
-
在gitee或者github上创建项目
**创建项目选择语言就行了,不要选择添加 .gitignore。**创建.gitignore就相当于在服务端已经git init了,再同步就需要编辑冲突了。
-
建立和连接远程仓库:
origin是默认别名
# 在github上,新建 Repository,名称和本地仓库一致,选择SSH地址 git remote add origin ssh://duke147@~/demo.git # 重新指定 # git remote set-url origin ssh://duke147@~/demo.git git push -u origin master
-
-
方式二:服务器项目clone到本地
git clone ssh://duke147@~/cbei_isp_doc.git
三、常用操作
-
远程仓库
# 查看远程仓库 git remote -v # 添加或指定远程仓库地址 git remote set-url origin "ssh:..." git config remote.origin.url "ssh:..." # 查看git配置 git config --list # 删除连接 git remote rm origin
-
不提交某个文件
方式一:git stash暂存
# 暂存,但是你本地的修改会消失 git stash # 把暂存的取回来 git stash apply git stash pop
方式二:.gitignore文件
方式三:git update-index
# 忽略跟踪 git update-index --assume-unchanged application-dev.properties # 恢复跟踪 git update-index --no-assume-unchanged application-dev.properties
-
主干合并分支
# 更新代码 (develop)$ git pull # 提交到本地develop分支 (develop)$ git commit -m "message" # 提交到服务器develop分支 (develop)$ git push # 将不提交部分放入stash中 (develop)$ git stash # 切换到主干 (develop)$ git checkout master # 更新代码 (master)$ git pull # 将develop合并到master (master)$ git merge develop # 提交到本地master (master)$ git commit -m '备注' # 提交到主干服务器 (master)$ git push # 切换到分支 (master)$ git checkout develop # 从stash中恢复未提交部分 (develop)$ git stash pop
-
常用命令
# 查看状态gst = git status git status # 将服务器内容拉到本地仓库 git fetch # 切换到master分支 git checkout master # 将dev分支合并到当前分支(master)中 git merge dev # 比较workspace和Repository差异 git diff # 查看本地所有分支 git branch # 查看远程所有分支 git branch -r # 查看本地和远程的所有分支 git branch -a # 提交到本地 git commit -m "message" # 提交到服务器 git push # 可以查看所有分支的所有操作记录(包括已经被删除的 commit 记录和 reset 的操作) git reflog # 不能查看已经删除了的commit记录 git log
-
Git快捷命令
# 设置快捷方式 vim ~/.gitconfig
-
参考
g - git gst - git status gl - git pull gup - git pull --rebase gp - git push gd - git diff gdc - git diff --cached gdv - git diff -w "$@" | view gc - git commit -v gc! - git commit -v --amend gca - git commit -v -a gca! - git commit -v -a --amend gcmsg - git commit -m gco - git checkout gcm - git checkout master gr - git remote grv - git remote -v grmv - git remote rename grrm - git remote remove gsetr - git remote set-url grup - git remote update grbi - git rebase -i grbc - git rebase --continue grba - git rebase --abort gb - git branch gba - git branch -a gcount - git shortlog -sn gcl - git config --list gcp - git cherry-pick glg - git log --stat --max-count=10 glgg - git log --graph --max-count=10 glgga - git log --graph --decorate --all glo - git log --oneline --decorate --color glog - git log --oneline --decorate --color --graph gss - git status -s ga - git add gm - git merge grh - git reset HEAD grhh - git reset HEAD --hard gclean - git reset --hard && git clean -dfx gwc - git whatchanged -p --abbrev-commit --pretty=medium gsts - git stash show --text gsta - git stash gstp - git stash pop gstd - git stash drop ggpull - git pull origin $(current_branch) ggpur - git pull --rebase origin $(current_branch) ggpush - git push origin $(current_branch) ggpnp - git pull origin $(current_branch) && git push origin $(current_branch) glp - _git_log_prettily
四、IDEA
- 本地项目上传到GitHub
五、GitHub
-
Fork
Fork,本身并不是git工具中的一个命令,它是在GitHub上的概念,是另一种clone方式——在服务器端的clone。
-
upstream repo和origin repo区别
-
upstream repo:你只可以拉取最新代码(即 git fetch ),从而保证你本地的仓库与源仓库同步
-
origin repo:就是你自己的repo(自己创建的,或者 fork 的项目)你可以做 任何推拉操作(pull and push)
-
六、常见异常
1.Git 无法通过 SSH 连接
本机防火墙或 ISP 设置的防火墙阻止端口 22 上的 SSH 连接。(Free WiFi禁掉22)
> git push --set-upstream origin master
Connection reset by [server_ip] port 22
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
解决:
选择https方式连接或者打开22端口。
2.服务器版本强制同步
git push --set-upstream origin master
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@gitee.com:duke147/java_exception.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 push -f origin master