from git import Repo import os # 配置参数 repo_url_https = "https://github.com/fulian23/Maxtool" # HTTPS克隆地址 repo_url_ssh = "git@github.com:fulian23/Maxtool.git" # SSH推送地址 local_path = r"C:\Users\32965\Maxtool" # 本地存储路径 ssh_key_path = r"C:\Users\32965\.ssh\id_rsa" # SSH私钥路径 # 1. 克隆仓库(使用HTTPS) # Repo.clone_from(repo_url_https, local_path) # # 2. 修改远程仓库地址为SSH格式 # repo = Repo(local_path) # origin = repo.remotes.origin # origin.set_url(repo_url_ssh) # # # 3. 创建测试文件 # test_file = os.path.join(local_path, "test.txt") # with open(test_file, "w") as f: # f.write("This is a test file added via GitPython") # # # 4. 提交更改 # repo.git.add("test.txt") # repo.git.commit("-m", "Add test file") # # # 5. 配置SSH密钥并推送 # ssh_cmd = f"ssh -i {ssh_key_path}" # with repo.git.custom_environment(GIT_SSH_COMMAND=ssh_cmd): # origin.push() # local_path = os.path.join('test', 't1') from git import Repo repo = Repo("../9992cddb-b7d1-99ec-1bd2-35fdc177e623/test") # branches = repo.remote().refs # for item in branches: # print(item.remote_head) # 1. 基础状态 # 2. 本地与远程差异 active_branch = repo.active_branch tracking_branch = active_branch.tracking_branch() ahead = sum(1 for _ in repo.iter_commits(f"{active_branch}..{tracking_branch}")) behind = sum(1 for _ in repo.iter_commits(f"{tracking_branch}..{active_branch}")) conflicts = repo.index.unmerged_blobs() conflicted = [path for path, entries in conflicts.items()] created_files = repo.untracked_files current = repo.active_branch.name head_commit = repo.head.commit tree = head_commit.tree all_files = [item.path for item in tree.traverse() if item.type == 'blob'] diffs = repo.index.diff(None) deleted = [d.a_path for d in diffs if d.change_type == 'D'] detached = repo.head.is_detached ignored_files = repo.git.execute(["git", "ls-files", "--others", "--ignored", "--exclude-standard"]).split("\n") modified_files = [d.a_path for d in diffs] untracked_files = repo.untracked_files staged_entries = repo.index.entries staged = [path[0] for path, _ in staged_entries.items()] tracking = active_branch.tracking_branch().name status = {"ahead": ahead, "behind": behind, "condlicted": conflicted, "created": created_files, "current": current, "deleted": deleted, "detached": detached, "files": all_files, "ignored": ignored_files, "modified": modified_files, "not_added": untracked_files, "staged": staged, "tracking": tracking} print(status)