-
Notifications
You must be signed in to change notification settings - Fork 0
/
GIT_cheat_sheet.txt
153 lines (94 loc) · 5.42 KB
/
GIT_cheat_sheet.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
1. To clone remote repository:
git clone https://github.com/imochurad/test (with auth: git clone username@host:/path/to/repository)
2. To add file to the local index:
git add 'GIT cheat sheet.txt' (git add *)
NOTE! You have to add changes to the local index every time the change was made. If file was modified after the file was added to the local index, when commiting to local repository, only changes that were made at the moment of addition to the local index will be propagated.
3. To commit changes to the local HEAD:
git commit -m "Commit message"
4. To send changes to the remote repository (branch master):
git push origin master
Username for 'https://github.com': imochurad
Password for 'https://[email protected]':
5. To create new branch in local repository based on the master:
git checkout -b feature_01
6. To push newly created branch to remote repository:
git push origin feature_01
7. To merge current branch(for example master) with another branch(feature_01):
git merge feature01
Note! When merging with conflicts, git will show a following message:
CONFLICT (content): Merge conflict in GIT cheat sheet.txt
When file is opened in the text editor it will show changes made in the 2 branches.
<<<<<<< HEAD is current branch into which changes are about to be merged!
After conflict was resolved change must be added to the local index, then committed to local repository only after that pushed along with other non-conflicting changes:
git add <conflicting_file_name>
git commit -m “Merge message”
git push origin <branch_name>
After merge conflict was detected to avoid resolving them in the text editor one by one, mergetool can be used. To launch it type:
git mergetool
After all the conflicts were resolved and the file has been saved, do not forget to remove file_name.orig files that contain conflict information.
For example:
rm README.md.orig
8. To switch branches use (for example feature_01):
git checkout feature01
9. To delete a branch use:
a) git push origin --delete name_of_the_branch
This will remove remote branch only. To remove branch locally:
git branch -d name_of_the_branch (this will delete merged branches only)
git branch -D name_of_the_branch (this will delete unmerged branches only)
To find out if branch is merged/unmerged:
git branch —merged
git branch —no-merged
OR:
b)
git branch -d name_of_the_branch (this will delete merged branches only)
git branch -D name_of_the_branch (this will delete unmerged branches only)
After this to remove this branch from remote repository run:
git push origin : name_of_the_branch
10. To commit a change straigh to the local repository, without exlpicitly adding it to the index (it will avoid the process of staging a file):
git commit -a -m “Message”
11. To remove a file (force remove) that has been already staged in the local index (it will actually delete a file from the folder and from the index):
git rm -f deleteMe.txt
12. To remove a file from local index (it will stay physically on the disk):
git rm —cached deleteMe.txt
13. To figure out diffs between local repository and a working directory run:
git diff
But this will not show new files that have been added and are still untracked.
14. To figure out diffs betweel local repository and local index (files that have been staged) run:
git diff —cached
15. To show all files modified(created, changed, removed) in the local working directory:
git status
16. To rename a file that was already staged:
git mv fileName newFileName
17. Replace local changes (in case you did something wrong, changes in the working directory can be replaced with the last content in HEAD)
git checkout --<filename>
git checkout .
^ this will revert all changes made to the working copy
Note that changes already added to the index, as well as new files, will be kept.
18. To revert changes made to the index:
git reset
Warning: this will reset all of your unpushed commits to master!
19. To revert a change that has been committed, do this:
git log <—— pick a commit that needs to be reverted
git revert <commit_id>
git git push origin <branch>
Note! in order to revert several commits procedure must be repeated depending on the postion of the commit in the list
20. To preview diffs between 2 branches (might be useful before merge):
git diff <source_branch> <target_branch>
21. To drop all local changes and commits, fetch the latest history from the server and point local master branch at it:
git fetch origin
git reset --hard origin/master
22. To retrieve up-to-date data from the remote repo and merge it with the local work directory:
git pull
Note, it doesn’t require before running this command to check in changes made to the local copy.
23. git branch
Lists all branches for the current repo. The branch with the * near it is the current branch.
24. Rebasing
With the rebase command, you can take all the changes that were committed on one branch and replay them on another one.
Let’s say master branch was cloned to create a feature_x branch.
During feature work there were changes introduced to the feature_x branch.
There were also commits made to the master branch.
Now feature_x branch is behind and ahead master branch at the same time.
Rebasing will replay commits made to the master branch since the point in time when cloning was done and apply on top changes that were made to the feature_x branch.
git checkout feature_x
git rebase master
Never rebase anything you’ve pushed somewhere. Use merge instead.