Git 的使用
Dionysen

分布式版本控制系统,适合个人中小企业使用

Installation

sudo pacman -S git

Usage

基本配置

配置git 的用户名和邮箱:

git config --global user.name "dionysen"
git config --global user.email "solongnight@outlook.com"

新建一个仓库

Initiate git repository on the local:

git init  

or set the file path:

git init path/to/repo

A repository was created, but it is empty.
You can add some files to the repository:

git add [filename]  // e.g. "git add ."

Then you add this files to the stages and you need to commit this to the repository.

git commit -a -m "Changed some files"

-a does not commit any new files.
-m means that you should give the commit message.
Add a remote repository:

git remote add origin git@gitee.com:sential/source.git

Push the local repository to the remote repository:

git push origin master
  • 若要在一个新的设备上使用远程仓库,首先将此仓库克隆到本地:
git clone git@gitee.com:sential/source.git

# 值得注意的是gitee的仓库公钥管理方式导致必须使用ssh克隆,否则难以实现无密码修改远程仓库

# 官方提示:使用SSH公钥可以让你在你的电脑和 Gitee 通讯的时候使用安全连接(Git的Remote要使用SSH地址)

添加个人公钥

然后按照 gitee 上的提示添加个人公钥:

ssh-keygen -t ed25519 -C "xxxxx@xxxxx.com"  
# Generating public/private ed25519 key pair...

cat ~/.ssh/id_ed25519.pub
# ssh-ed25519 AAAAB3NzaC1yc2EAAAADAQABAAABAQC6eNtGpNGwstc....

ssh -T git@gitee.com

与远程仓库同步

每次编辑时要执行。

git pull origin master

# 然后开始编辑
# 完成后执行:

git add .
git commit -a -m "Changed some files"
git push origin master

或者每次编辑完成后,在另一处pull一次,那样不用每次编辑前都要再拉去一下了。
写两个脚本自动拉取和提交。

  • 当没有拉取最新版本的远程仓库同时又修改了本地仓库时,拉取会提示错误,需要选择合并或者放弃某一端,如果放弃本地仓库,执行以下命令:
git reset --hard
git pull origin master

分支切换

查看分支:

git branch -a 	# 查看远程分支
git branch # 查看本地分支

新建分支:

git checkout -b linux origin/linux
#完成新分支的修改后
git add .
git commit -a -m "Changed some files"
git push --set-upstream origin origin/linux

之后即可正常使用,切换分支使用命令:

git checkout main # 切换到主分支
显示评论