-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-backup
More file actions
66 lines (56 loc) · 2.07 KB
/
git-backup
File metadata and controls
66 lines (56 loc) · 2.07 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
echo "Select an operation:"
echo "1. Backup"
echo "2. Restore"
read -p "Enter your choice (1 or 2): " choice
case $choice in
1)
# Option 1: Backup
echo "Backing up changes..."
# Stage all changes
git add *.py *.sh
# Get the last commit id
last_commit_id=$(git rev-parse HEAD)
# Commit all changes, using the last commit id in the commit message
git commit -m "Backup commit, previous commit id was $last_commit_id"
# Get the new commit id
new_commit_id=$(git rev-parse HEAD)
repo_name=$(git rev-parse --show-toplevel | xargs basename)
# Create a patch file from the last commit
git format-patch -1 $new_commit_id --stdout >${repo_name}_backup_${last_commit_id}.patch
echo "Backup created: ${repo_name}_backup_${last_commit_id}.patch"
;;
2)
# Option 2: Restore
# Provide two options for restoring changes
echo "Restoring changes..."
echo "Select a restore method:"
echo "1. Git reset to the previous commit"
echo "2. Git checkout and apply patch"
read -p "Enter your choice (1 or 2): " restore_choice
case $restore_choice in
1)
# Option 1: Git reset to the previous commit
echo "Resetting to the previous commit..."
git reset --soft HEAD^
;;
2)
# Option 2: Git checkout and apply patch
echo "Enter the name of the backup patch file:"
read patch_file
# Extract commit id from the patch file name
commit_id=${patch_file#backup_}
commit_id=${commit_id%.patch}
echo "Checking out to commit $commit_id and applying patch..."
git checkout $commit_id
git apply $patch_file
;;
*)
echo "Invalid choice. Please enter 1 or 2."
;;
esac
;;
*)
echo "Invalid choice. Please enter 1 or 2."
;;
esac