最近常用到的一些命令,记录下来


git 显示命令和帮助

git help <command> 打开对应命令的帮助信息

git status 查看工作区状态

git diff 查看文件的修改内容

git add <file> 缓存修改

git add . 缓存所有修改,同 git add -all (-a)

git commit -m "<comment>" 提交修改并添加comment

git push 将本地分支的更新,推送到远程主机

git pull 拉取远程代码到本地

git branch 查看当前分支

git branch -a 查看所有分支(local和remote)

git branch <newBranch> 切换新的分支 newBranch

git checkout <branch> 切换到branch分支

git checkout -b <newBranch> 创建并切换到新的分支newBranch

git branch -d <branch_name> 删除本地分支

git push origin -d <branch_name>git push origin --delete <branchName>,或者 git push origin :<branch_name> 删除远程分支

git checkout --<file> 撤销未缓存的文件修改

git rm --cached <file>git rm -r --cached <files> 删除新加入ignore的文件

git reset HEAD <file> 撤销已缓存的文件修改

git reset HEAD~1 --hard 通过reset HEAD的方式来撤销一次提交(~2 两次)

git push <remote> <branch> --force (-f) 推送到服务器

git rm <file> 删除文件,之后需要commit

git remote set-url origin git@github.com:<Username>/<Project>.git 更换远程仓库地址(ssh)

git config --global alias.acm '!git commit -a -m' 可以自定义acm命令,实现add + commit -m的功能,之后就可以使用git acm

使用git在本地创建一个项目的过程

1
2
3
4
5
6
7
8
$ makdir ~/hello-world    //创建一个项目hello-world
$ cd ~/hello-world       //打开这个项目
$ git init             //初始化
$ touch README
$ git add README        //更新README文件
$ git commit -m 'first commit'     //提交更新,并注释信息“first commit”
$ git remote add origin git@github.com:defnngj/hello-world.git     //连接远程github项目
$ git push -u origin master     //将本地项目更新到github项目上去