Git常用指令集

Git 是一个很强大的分布式版本控制系统。它不但适用于管理大型开源软件的源代码,管理私人的文档和源代码也有很多优势。

Git常用操作命令:

1) 远程仓库相关命令

检出仓库:$ git clone git://github.com/jQuery/jquery.git

查看远程仓库:$ git remote -v

添加远程仓库:$ git remote add [name] [url]

删除远程仓库:$ git remote rm [name]

修改远程仓库:$ git remote set-url --push [name] [newUrl]

拉取远程仓库:$ git pull [remoteName] [localBranchName]

推送远程仓库:$ git push [remoteName] [localBranchName]

 

*如果想把本地的某个分支test提交到远程仓库,并作为远程仓库的master分支,或者作为另外一个名叫test的分支,如下:

$git push origin test:master         // 提交本地test分支作为远程的master分支

$git push origin test:test              // 提交本地test分支作为远程的test分支

 

2)分支(branch)操作相关命令

查看本地分支:$ git branch

查看远程分支:$ git branch -r

创建本地分支:$ git branch [name] ----注意新分支创建后不会自动切换为当前分支

切换分支:$ git checkout [name]

创建新分支并立即切换到新分支:$ git checkout -b [name]

删除分支:$ git branch -d [name] ---- -d选项只能删除已经参与了合并的分支,对于未有合并的分支是无法删除的。如果想强制删除一个分支,可以使用-D选项

合并分支:$ git merge [name] ----将名称为[name]的分支与当前分支合并

创建远程分支(本地分支push到远程):$ git push origin [name]

删除远程分支:$ git push origin :heads/[name] 或 $ gitpush origin :[name] 

 

*创建空的分支:(执行命令之前记得先提交你当前分支的修改,否则会被强制删干净没得后悔)

$git symbolic-ref HEAD refs/heads/[name]

$rm .git/index

$git clean -fdx

 

3)版本(tag)操作相关命令

查看版本:$ git tag

创建版本:$ git tag [name]

删除版本:$ git tag -d [name]

查看远程版本:$ git tag -r

创建远程版本(本地版本push到远程):$ git push origin [name]

删除远程版本:$ git push origin :refs/tags/[name]

合并远程仓库的tag到本地:$ git pull origin --tags

上传本地tag到远程仓库:$ git push origin --tags

创建带注释的tag:$ git tag -a [name] -m 'yourMessage'

 

4) 子模块(submodule)相关操作命令

添加子模块:$ git submodule add [url] [path]

   如:$git submodule add git://github.com/soberh/ui-libs.git src/main/webapp/ui-libs

初始化子模块:$ git submodule init  ----只在首次检出仓库时运行一次就行

更新子模块:$ git submodule update ----每次更新或切换分支后都需要运行一下

删除子模块:(分4步走哦)

 1) $ git rm --cached [path]

 2) 编辑“.gitmodules”文件,将子模块的相关配置节点删除掉

 3) 编辑“ .git/config”文件,将子模块的相关配置节点删除掉

 4) 手动删除子模块残留的目录

 

5)忽略一些文件、文件夹不提交

在仓库根目录下创建名称为“.gitignore”的文件,写入不需要的文件夹名或文件,每个元素占一行即可,如

target

bin

*.db

 

=====================

Git 常用命令

git branch 查看本地所有分支
git status 查看当前状态 
git commit 提交 
git branch -a 查看所有的分支
git branch -r 查看本地所有分支
git commit -am "init" 提交并且加注释 
git remote add origin git@192.168.1.119:ndshow
git push origin master 将文件给推到服务器上 
git remote show origin 显示远程库origin里的资源 
git push origin master:develop
git push origin master:hb-dev 将本地库与服务器上的库进行关联 
git checkout --track origin/dev 切换到远程dev分支
git branch -D master develop 删除本地库develop
git checkout -b dev 建立一个新的本地分支dev
git merge origin/dev 将分支dev与当前分支进行合并
git checkout dev 切换到本地dev分支
git remote show 查看远程库
git add .
git rm 文件名(包括路径) 从git中删除指定文件
git clone git://github.com/schacon/grit.git 从服务器上将代码给拉下来
git config --list 看所有用户
git ls-files 看已经被提交的
git rm [file name] 删除一个文件
git commit -a 提交当前repos的所有的改变
git add [file name] 添加一个文件到git index
git commit -v 当你用-v参数的时候可以看commit的差异
git commit -m "This is the message describing the commit" 添加commit信息
git commit -a -a是代表add,把所有的change加到git index里然后再commit
git commit -a -v 一般提交命令
git log 看你commit的日志
git diff 查看尚未暂存的更新
git rm a.a 移除文件(从暂存区和工作区中删除)
git rm --cached a.a 移除文件(只从暂存区中删除)
git commit -m "remove" 移除文件(从Git中删除)
git rm -f a.a 强行移除修改后文件(从暂存区和工作区中删除)
git diff --cached 或 $ git diff --staged 查看尚未提交的更新
git stash push 将文件给push到一个临时空间中
git stash pop 将文件从临时空间pop下来
---------------------------------------------------------
git remote add origin git@github.com:username/Hello-World.git
git push origin master 将本地项目给提交到服务器中
-----------------------------------------------------------
git pull 本地与服务器端同步
-----------------------------------------------------------------
git push (远程仓库名) (分支名) 将本地分支推送到服务器上去。
git push origin serverfix:awesomebranch
------------------------------------------------------------------
git fetch 相当于是从远程获取最新版本到本地,不会自动merge
git commit -a -m "log_message" (-a是提交所有改动,-m是加入log信息) 本地修改同步至服务器端 :
git branch branch_0.1 master 从主分支master创建branch_0.1分支
git branch -m branch_0.1 branch_1.0 将branch_0.1重命名为branch_1.0
git checkout branch_1.0/master 切换到branch_1.0/master分支
du -hs

-----------------------------------------------------------
mkdir WebApp
cd WebApp
git init
touch README
git add README
git commit -m 'first commit'
git remote add origin git@github.com:daixu/WebApp.git
git push -u origin master

 

Git 常用命令图表 


gitKey的生成:

SSH keys

An SSH key allows you to establish a secure connection between yourcomputer and GitLab. Before generating an SSH key in your shell, check if your systemalready has one by running the following command:

Windows Command Line:

type %userprofile%\.ssh\id_rsa.pub

GNU/Linux/Mac/PowerShell:

cat ~/.ssh/id_rsa.pub

If you see a long string starting with ssh-rsa, you can skip the ssh-keygen step.

Note: It is a best practice to use a password for an SSH key, but it is notrequired and you can skip creating a password by pressing enter. Note thatthe password you choose here can't be altered or retrieved.

To generate a new SSH key, use the following command:

ssh-keygen -t rsa -C "niefei@ibm.com"

This command will prompt you for a location and filename to store the keypair and for a password. When prompted for the location and filename, justpress enter to use the default. If you use a different name, the key will notbe used automatically.

Use the command below to show your public key:

Windows Command Line:

type %userprofile%\.ssh\id_rsa.pub

GNU/Linux/Mac/PowerShell:

cat ~/.ssh/id_rsa.pub

Copy-paste the key to the 'My SSH Keys' section under the 'SSH' tab in youruser profile. Please copy the complete key starting with ssh-rsa and endingwith your username and host.

To copy your public key to the clipboard, use the code below. Depending on yourOS you'll need to use a different command:

Windows Command Line:

type %userprofile%\.ssh\id_rsa.pub | clip

Windows PowerShell:

cat ~/.ssh/id_rsa.pub | clip

Mac:

pbcopy < ~/.ssh/id_rsa.pub

GNU/Linux (requires xclip):

xclip -sel clip < ~/.ssh/id_rsa.pub

Deploy keys

Deploy keys allow read-only access to multiple projects with a single SSHkey.

This is really useful for cloning repositories to your ContinuousIntegration (CI) server. By using deploy keys, you don't have to setup adummy user account.

If you are a project master or owner, you can add a deploy key in theproject settings under the section 'Deploy Keys'. Press the 'New DeployKey' button and upload a public SSH key. After this, the machine that usesthe corresponding private key has read-only access to the project.

You can't add the same deploy key twice with the 'New Deploy Key' option.If you want to add the same key to another project, please enable it in thelist that says 'Deploy keys from projects available to you'. All the deploykeys of all the projects you have access to are available. This projectaccess can happen through being a direct member of the project, or througha group. See def accessible_deploy_keys in app/models/user.rb for moreinformation.

Deploy keys can be shared between projects, you just need to add them to each project.

Applications

Eclipse

How to add your ssh key to Eclipse: https://wiki.eclipse.org/EGit/User_Guide#Eclipse_SSH_Configuration

Tip: Non-default OpenSSH key file names or locations

If, for whatever reason, you decide to specify a non-default location and filename for your GitLab SSH key pair, you must configure your SSH client to find your GitLab SSH private key for connections to your GitLab server (perhaps gitlab.com). For OpenSSH clients, this is handled in the ~/.ssh/config file with a stanza similar to the following:

#
# Main gitlab.com server
#
Host gitlab.com
RSAAuthentication yes
IdentityFile ~/my-ssh-key-directory/my-gitlab-private-key-filename
User mygitlabusername

Another example

#
# Our company's internal GitLab server
#
Host my-gitlab.company.com
RSAAuthentication yes
IdentityFile ~/my-ssh-key-directory/company-com-private-key-filename

Note in the gitlab.com example above a username was specified to override the default chosen by OpenSSH (your local username). This is only required if your local and remote usernames differ.

Due to the wide variety of SSH clients and their very large number of configuration options, further explanation of these topics is beyond the scope of this document.

Public SSH keys need to be unique, as they will bind to your account. Your SSH key is the only identifier you'llhave when pushing code via SSH. That's why it needs to uniquely map to a single user.




图片转自:http://www.cnblogs.com/1-2-3/archive/2010/07/18/git-commands.html

本文转自:http://hi.baidu.com/sunboy_2050/item/ffab7396672895d11a49dfcc

建议参考文献:

Git 命令参数及用法详解

Windows环境中使用版本管理工具Git

GitRef
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值