Git Config & Common Commands

$git config --global user.name "your username"
$git config --global user.email "your email"
$ssh-keygen -t rsa -C "your email"

之后敲几次回车,然后下面开始查看我们的公钥,并将公钥copy到我们github或gitlab里的ssh setting里配置。

$git init   // 初始化版本库
$git add .   // 添加文件到版本库(只是添加到缓存区),.代表添加文件夹下所有文件 
$git commit -m "first commit" // 把添加的文件提交到版本库,并填写提交备注
$git remote add origin 你的远程库地址  // 把本地库与远程库关联
$git push -u origin master    // 第一次推送时
$git push origin master  // 第一次推送后,直接使用该命令即可推送修改

团队协作git常用命令 Reference Link

Git Branch

Git’s branching functionality lets you create new branches of a project to test ideas, isolate new features, or experiment without impacting the main project.

View Branches

# To view the branches in a Git repository, run the command:
$git branch
# To view both remote-tracking branches and local branches, run the command:
$git branch -a

Checkout a Branch

# To checkout an existing branch, run the command:
$git checkout BRANCH-NAME

Create a New Branch

# To create a new branch, run the command:
$git branch NEW-BRANCH-NAME
# Note that this command only creates the new branch. You’ll need to run git checkout NEW-
# BRANCH-NAME to switch to it.
# There’s a shortcut to create and checkout a new branch at once. 
# You can pass the -b option (for branch) with git checkout. The following commands do the same thing:

# Two-step method
$git branch NEW-BRANCH-NAME
$git checkout NEW-BRANCH-NAME

# Shortcut
$git checkout -b NEW-BRANCH-NAME

Rename a Branch

# To rename a branch, run the command:
$git branch -m OLD-BRANCH-NAME NEW-BRANCH-NAME

# Alternative
$git branch --move OLD-BRANCH-NAME NEW-BRANCH-NAME

Delete a Branch

# Git won’t let you delete a branch that you’re currently on. You first need to checkout a # different branch, then run the command:
$git branch -d BRANCH-TO-DELETE

# Alternative:
$git branch --delete BRANCH-TO-DELETE

# The branch that you switch to makes a difference. Git will throw an error if the changes 
# in the branch you’re trying to delete are not fully merged into the current branch. You 
# can override this and force Git to delete the branch with the -D option (note the capital 
# letter) or using the --force option with -d or --delete :
$git branch -D BRANCH-TO-DELETE

# Alternatives
$git branch -d --force BRANCH-TO-DELETE
$git branch --delete --force BRANCH-TO-DELETE

Compare Branches

# You can compare branches with the git diff command:
$git diff FIRST-BRANCH..SECOND-BRANCH

# You’ll see colored output for the changes between branches. For all lines that have 
# changed, the SECOND-BRANCH version will be a green line starting with a “+”, and the 
# FIRST-BRANCH version will be a red line starting with a “-”. If you don’t want Git to 
# display two lines for each change, you can use the --color-words option. Instead, Git 
# will show one line with deleted text in red, and added text in green.

# If you want to see a list of all the branches that are completely merged into your 
# current branch (in other words, your current branch includes all the changes of the other 
# branches that are listed), run the command git branch --merged .

Update a Branch from Remote

# To update a local branch from remote:
$git stash (optional, to save local changes which differs from the remote repository if any) 

# If you weren’t already on the branch you want to work on:
$git checkout my_local_branch 

# Finally pull from the remote branch
$git pull

Track a Remote Branch

# If you already have a branch and you want to track a remote branch, then you use set-
# upstream-to command:
$git branch --set-upstream-to origin/BRANCH

# Or you can use the -u flag (upstream) when you make your first push:
$git push -u origin BRANCH

Help with Git Branch

# If you forget how to use an option, or want to explore other functionality around the git 
# branch command, you can run any of these commands:
$git help branch
$git branch --help
$man git-branch

Delete remote repository files

# delete specific file
$git rm -f [file_name.txt]

# Removes all files under the directory and the current directory.
$git rm -rf [directory]

#update with remote repositories 
$git add .
$git commit -m "removed file xx"
$git push

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值