-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfixing_common_problems.txt
89 lines (74 loc) · 4.07 KB
/
fixing_common_problems.txt
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
There are a few ways to undo a mistake in your GIT repository.
Some commands change the commit history (e.g. ammend, reset).
This is fine if the repository is only accessed by you.
But if other people are pushing to/pulling from the same remote repository it can corrupt their history.
So it is best to use git revert instead (see below).
Remember the process: Modify (working directory) --> Stage (staging area) --> Commit (saved in .git repo) --> Merge (optional)
###############################################
# Need to undo unstaged/staged modifications? #
###############################################
Solution 1: Switch to another branch, and forcibly remove the modifications since last commit.
git switch --force branchname
Solution 2: Hard reset to remove all modifications since last commit
git reset --hard
##########################################################
# Need to move unstaged modifications to another branch? #
##########################################################
So you have modified the wrong branch!
Solution: Use git stash to store the modifications, then switch to the correct branch and pop the stash
See 'stashing branch modifications' in 'branching_and_merging.txt'
############################
# Need to change a commit? #
############################
Solution: Overwrite the commit message without creating a new commit
git commit --amend # commits any new changes to last commit
git commit --amend -m 'type correct message' # changes the message of the last commit
##################################
# Committed to the wrong branch? #
##################################
Solution: Cherry pick the commit from the the list of commits, transfer it to the correct branch, and delete it from the old branch
git switch wrong_branch
git log
copy first 6 characters of hash of the commit you want to move
git switch right_branch
git cherry-pick paste_hash_here
git log # notice the commit is now in this branch
now delete from wrong_branch (see deleting commits below)
############################
# Need to delete a commit? #
############################
Solution: Choose to soft/mixed/hard reset tracked directories/files to a specified commit with hash (deletes all commits since).
Soft - deletes the specified commit, keeps tracked file modifications in staging area
Mixed (default) - deletes the specified commit, keeps tracked file modifications in working directory (unstaged)
Hard - deletes the specified commit, deletes all tracked file modifications since
git log
copy first 6 characters of hash of the commit you want to reset to
git reset --[soft|mixed|hard] paste_hash_here
git clean -n # view files you'll delete with git clean
git clean -fdx # CAREFUL! deletes ignored files, and untracked directories/files -
##############################
# Need to undo a hard reset? #
##############################
Solution: Needs to be done as soon as possible before garbage deletion (git-gc) runs which makes the hard reset unrecoverable.
Use reflog to list commit hashes ordered by git command. Restore the commit and save it as a new branch.
git reflog
copy the hash before the reset
git switch paste_hash_here # creates a 'detatched head' i.e. not in a branch
git branch newbranchname # creates a branch for the restored commit
###############################################################
# Need to amend/reset but don't want to affect commit history #
###############################################################
Solution: Use revert to create a new commit that reverses the affect of a specified earlier commit.
No modifications to earlier commits, so history is protected.
git log
copy and paste the hash of the commit you want to be reversed
git revert paste_hash_here
enter descriptive message
git log # notice extra commit
#########################
# Need to quit a merge? #
#########################
Sometimes you start a merge and realise you can't finish it
Solution: quit the merge, and if you need to, undo the modifications since the last commit
git merge --quit # quits the merge
git switch --force - # deletes file modifications since last commit