Git应用笔记
1.基础操作
初始化本地代码仓库
git init
Clone代码
git clone
提交代码
git add . git commit -m "commit comments"
git push origin branchname
更新本地代码
git pull
git update
代码强制回退
#将代码克隆到本地 git clone ssh://git@…… #reset到指定的回退点(commit的代码) git reset --hard 08af54 #创建新的分支 git checkout branch2 #在git仓库种创建branch2分支 #查看本地分支 git branch #查看远程分支 git branch -r #将回滚后的代码强推至新建的远程分支 git push -f -u origin branch2
切换分支
git checkout branchname
提交代码到指定分支
git push origin branchname
删除untracked files
git clean -n #查看将要删除的文件
git clean -f #删除untracked files
切换分支时,提示 you need to resolve your current index first
git resert --merge
放弃本地修改(参考:https://blog.csdn.net/zhizhengguan/article/details/123851862)
#未add的代码 git checkout . #放弃本地修改,强制和远程同步 $ git fetch --all $ git reset --hard origin/master $ git pull
无法切换到远程分支,提示远程分支可能不存在
git branch --set-upstream-to=origin/remoteBranchName branchName
2.Linux配置git ssh访问
1. 切换到应用使用的用户下,~/.ssh目录
2. 查看ssh密钥是否已存在,进行下一步的生成或者替换
3. 生成密钥,输入的内容可以直接回车(如果不需要设置密码)
4. 启动ssh-agent
5. 将私钥添加到ssh_agent中
6. 查看公钥并复制下来,添加到git的设置中(点击头像,account settings / manage account 或其他相似选项,将公钥添加到 ssh key中)
[appuser@PPSSAVL11729 ~]$ cd ~/.ssh [appuser@PPSSAVL11729 .ssh]$ ls -al ~/.ssh total 20 drwx------ 2 appuser appuser 76 Dec 23 2022 . drwx------. 16 appuser appuser 4096 Sep 7 15:29 .. -rw------- 1 appuser appuser 1420 Dec 23 2022 authorized_keys -rw------- 1 appuser appuser 1679 Dec 23 2022 id_rsa -rw-rw-r-- 1 appuser appuser 387 Dec 23 2022 id_rsa.pub -rw-r--r-- 1 appuser appuser 957 May 4 16:01 known_hosts [appuser@PPSSAVL11729 .ssh]$ ssh-keygen -t rsa -b 4096 -C "username" Generating public/private rsa key pair. Enter file in which to save the key (/home/appuser/.ssh/id_rsa): /home/appuser/.ssh/id_rsa already exists. Overwrite (y/n)? y Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/appuser/.ssh/id_rsa. Your public key has been saved in /home/appuser/.ssh/id_rsa.pub. The key fingerprint is: SHA256:lAQMoPQGpi+5FIKj4z...... The key's randomart image is: +---[RSA 4096]----+ | +...ooo*. | |=.o .* * | |*. o o & | |o+o . O o | |=.. + S | |o+ o B | |... + + . | | ..* E . | | ..o . | +----[SHA256]-----+ [appuser@PPSSAVL11729 .ssh]$ eval "$(ssh-agent -s)" Agent pid 92118 [appuser@PPSSAVL11729 .ssh]$ ssh-add ~/.ssh/id_rsa Identity added: /home/appuser/.ssh/id_rsa (/home/appuser/.ssh/id_rsa) [appuser@PPSSAVL11729 .ssh]$ cat id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC1xiHn4ptpef5IxSxwCG7U+013qzUaaLgcm+zK/CGGTfPiqOmrQ9SfmHHstnOTJfpNxAp4/u8UYit/WgR4mI9frAfvIYFxLziFNgzEY/izi5ALw4cmw3Ldr+svA8u/pqEw2sfbG7mN2NEOvL4s6rRh5zVi0Fa57WR02xDVdYctTH78adhQbfJam......
3.windows下使用ssh-key连接git代码仓库
1. 打开git bash(git的命令行窗口)
2. 生成密钥
3. 查看公钥,将其添加到账户中
username@LPH5282 MINGW64 /d/Java $ ssh-keygen -t rsa -C "username" Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/username/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /c/Users/username/.ssh/id_rsa. Your public key has been saved in /c/Users/zckha/.ssh/id_rsa.pub. The key fingerprint is: SHA256:D4s7EPGOKkSqJGw3kKXNQTZ3PWJ8ivohYhh...username The key's randomart image is: +---[RSA 2048]----+ | .+ .... | | .o+ .+ + | | .* .oo + . | |.=+o.... | |==...+ S | |=Bo=o... + | |Xooo+.o . . | |* o E... | | +. .. | +----[SHA256]-----+ username@LPH5282 MINGW64 /d/02_TrainingFiles/Java $ cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBf8go44BdLk6/HTEBEolJiWTXGr1rFWqvH1/mIYAjpmjS9x8eIl2Eaw4xarggEIlYm7xOEbnN2mKVGcp3y9hyETRKuDbpxpuG+TYoee/dUMG/zJfYOs+Q816ehRoSKlzjvQb61qlOIzeNwIQSKdoE4s+LHtBHiDO3YnFPIqxjNVLWSgv8yen6n ... username