git常用命令

git download

初始化GIT项目

1
git init

查看当前状态

1
git status

查看所有分支

1
2
git branch -a
git branch -vva

查看代码最后修改时间及作者

1
git blame file

创建新的分支

1
git checkout -b  branchname

切换分支

1
git checkout branchname

删除分支

1
2
3
4
5
# git branch --delete --force
git branch -D branchname

# git branch --delete
git branch -d branchname

删除远程分支

1
2
3
4
5
git push -u origin :branchname
git push -u origin --delete branchname
git push -u origin -d branchname
# on your local branch
git branch --unset-upstream

添加修改到本地

1
2
3
4
git add file
git add directory
# add all if position is the root of project
git add .

提交修改到本地

1
2
3
4
5
6
git commit 
git commit -m "commit with comment"
# close issue #1
git commit -m "Closed #1"
# resolve issue #1
git commit -m "Resolved #1"

不同的GIT可能有多种形式或兼容形式,大概有以下几种方式

  • fix #xxx
  • fixes #xxx
  • fixed #xxx
  • close #xxx
  • closes #xxx
  • closed #xxx
  • resolve #xxx
  • resolves #xxx
  • resolved #xxx

撤销commit

1
git commit --amend

删除最近commit和修改

1
git reset --hard HEAD^

删除最近commit但保留修改

1
git reset --soft HEAD^

删除最后一次commit后的所有修改

1
git reset --hard HEAD

解决冲突

1
2
3
4
git checkout master
git pull
git checkout yourbranch
git rebase master

如果遇到冲突文件, 编辑该文件
这里可以结合blame命令来查看代码修改最后一次记录时间及作者

add 冲突文件

1
2
3
4
5
6
7
git add {conflict filename}
# 解决所有冲突后commit
gti commit -m "fix conflict"
git rebase --continue
# 没有任何冲突文件后
git rebase --skip
git push -u origin yourbranchname
#

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×