Git常用操作

clone项目

git clone [仓库地址]

eg:

git clone https://github.com/tianzhijiexian/TestRepository.git

配置用户名&邮箱

git config user.name "用户名"

git config user.email "邮件地址"

eg:

git config user.name "Jiang Xin"

git config user.email "gotgithub@gmail.com"

上面是对当前的仓库做的配置,如果你想做全局的配置,那么就要用git config  –global 命令,有了global这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,比如:

git config --global user.name "wangyubin" 

查看用户名和邮箱

cat ~/.gitconfig

添加新的远程仓库

添加远程仓库

git remote add [仓库名] [链接]

eg:

git remote add kale git://github.com/paulboone/ticgit.git

查看已经存在的远程仓库

git remote

列出详细信息,在每一个名字后面列出其远程url

git remote -v

本地和远程仓库之间进行同步(获取仓库的所有更新,但是不自动合并当前分支)

比如你的远程仓库是kale,你想抓取本地没有,而远程仓库有的东西,就要用fetch命令

git fetch [仓库名]

eg:

git fetch kale

修改远程仓库名

git remote rename [之前的名字] [新的名字]

eg:

git remote rename kale jack

删除远程仓库

git remote rm [仓库的名字]

eg:

git remote rm kale

获取仓库的所有更新, 并且自动合并到当前分支

git pull

提交数据

git commit -a -m “注释 ”

这条的命令是在你已经add了一个或多个文件过之后,然后修改了这些文件,就可以使用该命令进行提交。

git add -A

git commit -m "注释"

 

建立分支

git branch [建立的分支名称]

eg:

git branch dev3.2

切换分支

git checkout [分支名字]

eg:

git checkout dev3.2

git checkout origin/android

新建一个分支并切换到这个分支上

git branch -b [分支名称]

eg:

git checkout -b dev3.2

将本地分支push到远程仓库

git push [仓库名] [分支名]

git push kale dev6.5

强制push

git push -f kale dev6.5

查看远程分支信息

git branch –r

查看新建的远程分支

比如你同事在Git的remote branch中新增branch xxx,但是你发现你在Android Studio中查看存在的branch时,并看不到他增加的branch,这时你用下面的命令就可以了

origin为远程仓库名

git remote update origin --prune

查看所有分支(包括本地分支)

远程分支是红色,本地分支是白色,当前分支是绿色

git branch -a

查看本地分支

git branch

查看本地分支的详细信息

git branch -v

查看已经合并的本地分支

git branch --merged

查看还没合并的本地分支

git branch --no-merged

将本地分支推到远程,这样来新建远程分支

git push origin <local_branch_name>:<remote_branch_name>

删除分支

git branch -d [branch-name]

eg:

git branch -d dev3.3

强制删除某个还未合并的分支

git branch -D [branch-name] 

eg:

git branch -D dev3.3

删除远程的分支和tag

git push [仓库] --delete <branchName>

git push kale --delete dev2.3

或者

git push kale :dev2.3

回到pull之前的commit

用 git reset --hard <COMMIT_ID> 

 

提交流程

git pull [仓库名] [分支名] #先拉一下最新的代码,看看有没有冲突。如果有就解决冲突

git add -A #把改动都存放到缓冲区

git st #查看当前改动的信息

git commit -m "描述信息" #提交更改

git push [仓库名] [分支名] #提交到自己的仓库。eg:git push kale branch01

参考自:

http://www.cnblogs.com/wang_yb/p/3867221.html

http://blog.chinaunix.net/uid-26997997-id-3231891.html

http://zengrong.net/post/1746.htm

原文地址:https://www.cnblogs.com/tianzhijiexian/p/4391185.html