Git基础及常用命令
跳到导航
跳到搜索
目录
date
Sep 2024
常用命令
日常使用
添加 key 后 clone #配置 git config --global user.name "evan886" git config --global user.email "[email protected]" #日常用得最多就这几个了 git pull origin master git add youfile git commit -m " " git push origin master note 当我们修改了很多文件,而不想每一个都add,想commit自动来提交本地修改,我们可以使用-a标识。 git commit -a -m "Changed some files" 有时提交不成 冲突了 请用 git rm -f filename …or create a new repository on the command line echo "# learn-english" >> README.md git init git add README.md git commit -m "first commit" git remote add origin [email protected]:evan886/learn-english.git git push -u origin master …or push an existing repository from the command line git remote add origin [email protected]:evan886/learn-english.git git push -u origin master …or import code from another repository You can initialize this repository with code from a Subversion, Mercurial, or TFS project. Git 新加了两个命令: git switch用于切换分支 git restore用于恢复原样 两者可以取代 git checkout。 #创建分支命令: git branch (branchname) #切换分支命令: git checkout (branchname) git branch dev evan@myxps:~/github/learn-c$ git checkout dev M co M turtle/src/3.c 切换到分支 'dev' git 退回上一个版本 git reset --hard HEAD^ 退回单个文件 git log p128.groovy ➜ devops6-shared-library git:(main) git checkout 7a7dfa905067e92f913e593f2cdcfee87ba70538 p128.groovy
打标签
在Git中打标签非常简单,首先,切换到需要打标签的分支上: $ git branch * dev master $ git checkout master Switched to branch 'master' 然后,敲命令git tag <name>就可以打一个新标签: $ git tag v1.0 可以用命令git tag查看所有标签: $ git tag v1.0
git将本地代码提交到远程仓库
#以我的i3wm配置文件作例子 # create a new repository on the command line echo "# myi3" >> README.md git init git add README.md git commit -m "first commit" git remote add origin [email protected]:evan886/myi3.git git push -u origin master
clone 指定分支
-b, --branch <分支> 检出 <分支> 而不是远程 HEAD git clone -b day-08 https://github.com/michaelliao/awesome-python3-webapp.git day8
git分支的合并
在git中,可以使用git merge 和git rebase两个命令来进行分支的合并
合并分支到master/main上
现在在dev分支上,可以用下面命令查看当前分支 git branch 刚开发完项目,执行了下列命令 git add . git commit -m 'dev' git push -u origin dev 把dev分支的代码合并到master分支上 该如何? 首先切换到master分支上 git checkout master 多人开发的话 需要把远程master上的代码pull下来 git pull origin master 我们把dev分支的代码合并到master上 git merge dev 然后查看状态 git status On branch master Your branch is ahead of 'origin/master' by 12 commits. (use "git push" to publish your local commits) nothing to commit, working tree clean 上面的意思就是你有12个commit,需要push到远程master上 执行下面命令即可 git push origin master
解决冲突
准备新的feature1分支,继续我们的新分支开发: $ git switch -c feature1 Switched to a new branch 'feature1' 修改readme.txt最后一行,改为: Creating a new branch is quick AND simple. 在feature1分支上提交: $ git add readme.txt $ git commit -m "AND simple" [feature1 14096d0] AND simple 1 file changed, 1 insertion(+), 1 deletion(-) 切换到master分支: $ git switch master Switched to branch 'master' Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) Git还会自动提示我们当前master分支比远程的master分支要超前1个提交。 在master分支上把readme.txt文件的最后一行改为: Creating a new branch is quick & simple. 提交: $ git add readme.txt $ git commit -m "& simple" [master 5dc6824] & simple 1 file changed, 1 insertion(+), 1 deletion(-) 现在,master分支和feature1分支各自都分别有新的提交,变成了这样: git-br-feature1 图片没显示 这种情况下,Git无法执行“快速合并”,只能试图把各自的修改合并起来,但这种合并就可能会有冲突,我们试试看: $ git merge feature1 Auto-merging readme.txt CONFLICT (content): Merge conflict in readme.txt Automatic merge failed; fix conflicts and then commit the result. 果然冲突了!Git告诉我们,readme.txt文件存在冲突,必须手动解决冲突后再提交。git status也可以告诉我们冲突的文件: $ git status On branch master Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits) You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both modified: readme.txt no changes added to commit (use "git add" and/or "git commit -a") 我们可以直接查看readme.txt的内容: Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes of files. <<<<<<< HEAD Creating a new branch is quick & simple. ======= Creating a new branch is quick AND simple. >>>>>>> feature1 Git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容,我们修改如下后保存: Creating a new branch is quick and simple. 再提交: $ git add readme.txt $ git commit -m "conflict fixed" [master cf810e4] conflict fixed 现在,master分支和feature1分支变成了下图所示: git-br-conflict-merged 用带参数的git log也可以看到分支的合并情况: $ git log --graph --pretty=oneline --abbrev-commit * cf810e4 (HEAD -> master) conflict fixed |\ | * 14096d0 (feature1) AND simple * | 5dc6824 & simple |/ * b17d20e branch test * d46f35e (origin/master) remove test.txt * b84166e add test.txt * 519219b git tracks changes * e43a48b understand how stage works * 1094adb append GPL * e475afc add distributed * eaadf4e wrote a readme file 最后,删除feature1分支: $ git branch -d feature1 Deleted branch feature1 (was 14096d0). 工作完成。 小结 当Git无法自动合并分支时,就必须首先解决冲突。解决冲突后,再提交,合并完成。 解决冲突就是把Git合并失败的文件手动编辑为我们希望的内容,再提交。 用git log --graph命令可以看到分支合并图。
https://www.liaoxuefeng.com/wiki/896043488029600/900004111093344
Rebase
https://liaoxuefeng.com/books/git/branch/rebase/index.html
usage first
添加key后 #配置 git config --global user.name "evan886" git config --global user.email "[email protected]"
error: 推送一些引用到 'https://github.com/xxx/xxx' 失败
error: 推送一些引用到 'https://github.com/xxx/xxx' 失败 提示:更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支。 提示:再次推送前,先与远程变更合并(如 'git pull ...')。详见 提示:'git push --help' 中的 'Note about fast-forwards' 小节。 原因; 这个问题是远程仓库与本地不同步引起的 解决方案: 因为当前分支的最新提交落后于其对应的远程分支,所以我们先从远程库fetch到更新再和本地库合并,之后就可以git push操作了。 git fetch origin git merge origin/main
usage from gitlab
Git global setup git config --global user.name "Administrator" git config --global user.email "[email protected]" Create a new repository git clone [email protected]:root/monitor.git cd monitor git switch -c main touch README.md git add README.md git commit -m "add README" git push -u origin main Push an existing folder cd existing_folder git init --initial-branch=main git remote add origin [email protected]:root/monitor.git git add . git commit -m "Initial commit" git push -u origin main Push an existing Git repository cd existing_repo git remote rename origin old-origin git remote add origin [email protected]:root/monitor.git git push -u origin --all git push -u origin --tags
git 基础安装使用
git 服务器搭建
服务器上 apt-get install libcurl4-gnutls-dev libexpat1-dev gettext \ libz-dev libssl-dev apt-get install git git --version git version 2.20.1 使用我们指定目录作为Git仓库。 git init newrepo useradd git #也可以让这个用户不可以登录,为了安全嘛 #试过 这个是成功的 mkdir testrepo root@cailuw-test:/data# chown -R git.git testrepo/ root@cailuw-test:/data# cd testrepo/ root@cailuw-test:/data/testrepo# git init --bare test.git Initialized empty Git repository in /data/testrepo/test.git/ 以上命令Git创建一个空仓库,服务器上的Git仓库通常都以.git结尾。然后,把仓库所属用户改为git: root@cailuw-test:/data/testrepo# chown -R git.git test.git/ #添加key mkdir /home/git/.ssh -p cp /root/.ssh/authorized_keys /home/git/.ssh/ chown -R git.git /home/git/.ssh/authorized_keys client 在你的 客户端机器上 ssh -T git账号名@服务器IP #客户端验证连接 ssh -T [email protected] git clone [email protected]:/data/testrepo/test.git 正克隆到 'test'... warning: 您似乎克隆了一个空仓库。 #配置 git config --global user.name "evan886" git config --global user.email "[email protected]"
Git 服务器搭建 在CentOS/RHEL上源码或者 yum 安装Git 以及 linux and win client 使用
http://blog.linuxchina.net/2015/06/18/how-to-use-git-%e8%bd%ac/
git usage on win
#git init git config --global --edit 配置为你的用户和用户名 # This is Git's per-user configuration file. [user] # Please adapt and uncomment the following lines: name = evan email = [email protected] After doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 日常用得最多就这几个了 git pull origin master git add youfile git commit -m " " git push origin master [Win-1.lxtx_fengjw] ➤ git push Username for 'http://gitlab.net': Password for 'http://[email protected]': To http://gitlab.net/lxtx-backend-docs/docs.git ! [rejected] master -> master (fetch first) error: failed to push some refs to 'http://gitlab.net/lxtx-backend-docs/docs.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Git自动账号密码填充 git pull记住用户名和密码
2021 进入项目目录 git config --global credential.helper store 然后会生成一个本地文件用于记录用户名和密码,这个文件我们无需关心 再次git pull一下,会让输入用户名和密码。这次输入之后以后就不会每次输入了。 如果要清除用户名和密码 运行一下命令缓存输入的用户名和密码 git config --global credential.helper wincred 清除掉缓存在git中的用户名和密码 git credential-manager uninstall 1、每次操作都需要输入用户名和密码感觉很繁琐,解决方法,在本地的工程文件夹的.git下打开config文件添加: [credential] helper = store或者在git bash 中执行 git config --global credential.helper store再输入一次用户名密码后就可以保存住了。 2、不行就用以下方法: 先用Git拉一次东西,拉的时候会提醒你输入帐号的密码 输入正确的帐号和密码后,等东西拉完以后输入 git config --global credential.helper store #这样只针对单个project [remote "origin"] url = http://huangweiqing:[email protected]/zt_tech/_interface.git