Git使用记录

最近弄了个小项目,放在了GitHub上面。基本的使用方法可以参考简介 – Git教程 – 廖雪峰的官方网站以及https://github.com/gouliming4437/Git。

实际使用又慢慢学会了一些新的东西,记录下来以便参考:

场景一:修改文件

  1. 使用github的时候,在本地修改了文件后commit,但是没有push到远程仓库;然后又在远程仓库在线修改了文件,如果两次修改的是同一个文件

首先都需要将远程仓库的最新修改拉取到本地:
git pull origin main # 将 main 分支的最新修改拉取到本地

其中:origin 是远程仓库的别名,你可以根据自己的配置进行修改。main 是分支名称,同样可以根据你的分支结构进行修改。

如果本地和远程的修改在同一个文件的同一行产生了冲突,Git 会在该行添加特殊的标记,例如 <<<<<<<, =======, >>>>>>>。需要手动打开有冲突的文件,并根据实际情况修改代码,删除冲突标记。解决冲突后,使用 git add 命令将修改的文件添加到暂存区,然后使用 git commit 命令提交:

# 拉取远程仓库的最新修改
git pull origin main

# 解决冲突(假设文件名为 file.txt)
# 打开 file.txt,手动解决冲突

# 提交修改
git add file.txt
git commit -m "解决冲突,合并本地和远程修改"

# 推送修改
git push origin main
  1. 使用github的时候,在本地修改了文件后commit,但是没有push到远程仓库;然后又在远程仓库在线修改了文件,如果两次修改的不是同一个文件

无论哪种情况,首先都需要将远程仓库的最新修改拉取到本地。

# 拉取远程仓库的最新修改
git pull origin main

# 直接推送
git push origin main

场景二:创建项目的不同版本

通过创建不同的分支,可以实现对同一个项目的多个版本进行并行开发和管理。

首先创建分支:git branch <新分支名>;然后切换分支:git checkout <分支名>;在切换到目标分支后,就可以开始修改代码了。当某个分支上的功能开发完成,可以将其合并到主分支或其他分支上:git merge <要合并的分支>

假设你正在开发一个网站,现在想同时开发一个移动端版本和一个桌面端版本。

创建两个分支:

git branch mobile
git branch desktop

切换到 mobile 分支,开发移动端版本:

git checkout mobile
# 在 mobile 分支上进行移动端相关的开发

切换到 desktop 分支,开发桌面端版本:

git checkout desktop
# 在 desktop 分支上进行桌面端相关的开发

合并分支(例如,将 mobile 分支上的功能合并到主分支):

git checkout main
git merge mobile

场景三:更改项目文件夹

问题:我的项目本来在文件夹A,并将其推送到远程仓库C;现在我想新建一个文件夹B,然后将远程仓库C拉取到这个新文件夹B,并删除文件夹A和远程仓库C之间的联系。

  1. 将远程仓库C拉取到新文件夹B:
    在文件夹B中git init,克隆远程仓库:git clone <远程仓库地址>;克隆完成后,你可以使用 ls 命令查看文件夹B中的内容,确认代码是否已经成功拉取。

如果你想将远程仓库克隆到B文件夹的某个子目录中,可以在克隆命令后面指定子目录名:
git clone https://github.com/example/myproject.git myproject_copy

  1. 删除文件夹A和远程仓库C之间的联系:
    cd到文件夹A,然后删除远程仓库的配置,如git remote remove origin,这将会删除名为 origin 的远程仓库配置。origin 是默认的远程仓库别名,你可以根据你的配置进行修改。

如果你想彻底删除A文件夹中de 本地分支,可以使用以下命令:

  • 删除本地分支的命令:git branch -d <分支名>
  • 强制删除: 如果分支存在未合并的提交,可以使用 -D 选项强制删除:git branch -D <分支名>

备注:

  • 查看分支: 在删除分支之前,可以使用 git branch 命令查看当前所有的本地分支。
  • 恢复删除的分支: 如果误删了分支,可以通过 git reflog 命令查看历史操作记录,然后使用 git checkout -b <新分支名> <提交哈希值> 命令恢复。

场景四:SSH Push

已生成SSH Key,并复制到GitHub。

  1. git remote add origin my-https-url.git or git clone my-https-url.git

    • Purpose: This step either adds a remote named origin to your local Git repository or creates the local git repository as clone of an existing repository.
    • git remote add origin my-https-url.git:
      * When to Use: You would use git remote add origin my-https-url.git if you created a repository locally first and then wanted to connect it to a remote repository on GitHub (or elsewhere). You’ll have made changes, committed, and now need to connect it with a remote repo
      * What it Does: This command adds the remote repository URL as the remote named origin in your local Git configuration. It establishes the connection to the remote server. The my-https-url.git would be a placeholder for https://github.com/gouliming4437/ArchiveTemplate4Wordpress.git (for example).
    • git clone my-https-url.git
      • When to Use: You would use git clone my-https-url.git if you have no local git repo yet
      • What it Does: This command creates a copy of the remote repository. It also sets the remote repository URL as the remote named origin in the local Git configuration.
  2. git remote set-url origin git@github.com:gouliming4437/ArchiveTemplate4Wordpress.git

    • Purpose: As discussed before, this command changes the remote URL associated with origin from the HTTPS URL to an SSH URL. This step assumes the origin remote exists from a prior step.
  3. git push -u origin main

    • Purpose: This command pushes your local main branch to the remote origin repository using SSH and sets the upstream tracking relationship (if you are pushing that branch for the first time).

Why You Need git remote add origin (or git clone):

  • git remote set-url modifies an existing remote: The git remote set-url command is designed to modify the URL of an existing remote. It can’t create a remote from scratch.
  • origin needs to be defined: The origin remote represents the "main" remote repository for your project. Git needs to know this remote exists and what its URL is before you can change it.
  • The Importance of Initializing a Remote
    • When working with Git, particularly when starting a new project with a remote repository on GitHub, GitLab, or any other Git server, you need to establish that connection from your local Git repository to the remote server. This is where git remote add origin my-https-url.git or git clone my-https-url.git plays a crucial role.
    • It sets up an entry point in the local repository, creating the connection to the remote repository you are working with and allowing Git to track changes and coordinate with the remote.

Example scenarios:

  • Scenario 1: You started locally:

    • You created a local Git repo (git init), made commits, then created the repo on Github. In this case, you need to first add the remote with git remote add origin https://github.com/gouliming4437/ArchiveTemplate4Wordpress.git then you use git remote set-url and finally you git push -u.
  • Scenario 2: You cloned an existing repo:

    • You started with a github repo. In this case, you are using git clone https://github.com/gouliming4437/ArchiveTemplate4Wordpress.git and Git will set up the remote called origin for you.