-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_init.py
More file actions
executable file
·32 lines (25 loc) · 1.04 KB
/
git_init.py
File metadata and controls
executable file
·32 lines (25 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#! python3
import git
import os
import sys
COMMIT_HASH = 'a0a4b033c12974e4c8251f19c8d2c6fd1255f8a1'
try:
try:
# Initialize the repository object (assumes running in the repo root or subdirectory)
repo = git.Repo(os.getcwd(), search_parent_directories=True)
except git.InvalidGitRepositoryError:
print("Error: Not a valid Git repository.")
sys.exit(1)
# Ensure the repository is not dirty (optional but recommended)
if repo.is_dirty():
print("Warning: Repository has uncommitted changes. Stash or commit them first.")
# Get the reference for the commit
# GitPython will find the commit object whether it's a full hash, short hash, or a tag/branch name
commit_ref = repo.commit(COMMIT_HASH)
# Checkout the commit
# This detaches HEAD, meaning you are no longer on a branch.
repo.git.checkout(commit_ref)
print(f"Successfully checked out commit: {COMMIT_HASH}")
print(f"Current HEAD: {repo.head.commit.hexsha}")
except Exception as e:
print(f"An error occurred: {e}")