-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-basics.txt
248 lines (146 loc) · 9.38 KB
/
git-basics.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
=============================================================================================================
Git Basic Commandline
=============================================================================================================
1. To initialize a Git repository:
$git init
2. checking the status
$git status
3. Adding Changes
$git add <filename>
staged:
Files are ready to be committed.
unstaged:
Files with changes that have not been prepared to be committed.
untracked:
Files aren't tracked by Git yet. This usually indicates a newly created file.
deleted:
File has been deleted and is waiting to be removed from Git.
4. add all changes at once
$git add -A
5. git reset: to remove a file or files from the staging area.
$git reset <filename>
Staging Area:
A place where you can group files together before you "commit" them to Git.
Commit
A "commit" is a snapshot of our repository. This way if you ever need to look back at the changes you've made (or if someone else does), you will see a nice timeline of all changes.
6. commiting -- to store changes in repository
$git commit -m "add a comment what is changed"
7. To see the differences betyouen the previous commit and last commit
$git diff
8. Using wildcards to add many files of the same type.
$git add '*.txt'
Wildcards:
you need quotes so that Git will receive the wildcard before our shell can interfere with it. Without quotes our shell will only execute the wildcard search within the current directory. Git will receive the list of files the shell found instead of the wildcard and it will not be able to add the files inside of the octofamily directory.
=============================================================================================================
Git Log or History
=============================================================================================================
9. History - to see what is changed
$git log
Use git log --summary to see more information for each commit. You can see where new files youre added for the first time or where files youre deleted. It's a good overview of what's going on in the project.
10. If you want to see a indivisual commit work, you can do it by using commits id. Don't need to copy full id from history, a little is enough.
$git show 95b56kl
11. Checkout -- To back to the previous any commit
$git checkout 95b56kl
12. To back to present commit again
$git checkout master
13. To back to a single file -- steps are following
$git log 'task.txt'
$git checkout af1832 'task.txt'
$git checkout master -f
=============================================================================================================
Git Resest
=============================================================================================================
14. To soft reset
$git reset --soft
15. To see the accidental commit
$git diff HEAD
The HEAD is a pointer that holds your position within all your different commits. By default HEAD points to your most recent commit, so it can be used as a quick way to reference that commit without having to look up the SHA.
Another great use for diff is looking at changes within files that have already been staged. Remember, staged files are files you have told git that are ready to be committed.
$git add octocat/octodog.txt
$git diff --staged
this option to see the changes you just staged. see octodog.txt is just added
$git reset octocat/octodog.txt
unstage files by using the git reset command. octodog.txt file is unstaged or removed. but see notice it is still there.
$git checkout -- octocat.txt
Files can be changed back to how they youre at the last commit by using the command: git checkout -- <target>. Go ahead and get rid of all the changes since the last commit for octocat.txt
Note:
The '--': git checkout seems to work fine without it. It's simply promising the command line that there are no more options after the '--'. This way if you happen to have a branch named octocat.txt, it will still revert the file, instead of switching to the branch of the same name.
16. To hard reset
$git reset --hard
17.
18.
=============================================================================================================
Git Remote Repositories
=============================================================================================================
19. Remote Repositories - Add remote server like github,bitbucket etc.
$git remote add origin https://github.com/try-git/try_git.git
git remote:
Git doesn't care what you name your remotes, but it's typical to name your main one 'origin'.
It's also a good idea for your main repository to be on a remote server like GitHub in case your machine is lost at sea during a transatlantic boat cruise or crushed by three monkey statues during an earthquake.
20.
$git remote show
21.
$git remote origin
22. pusihig Remotely -- The name of our remote is origin and the default local branch name is master. The -u tells Git to remember the parameters, so that next time you can simply run git push and Git will know what to do.
$git push -u origin master
23. Pulling Remotely -- check for changes on our GitHub repository and pull down any new changes by running:
$git pull origin master
=============================================================================================================
Git stash commands
=============================================================================================================
24. git stash: to temporaily save
$git stash
Sometimes when you go to pull you may have changes you don't want to commit just yet. One option you have, other than commiting, is to stash the changes.
Use the command 'git stash apply' to re-apply your changes after your pull.
=============================================================================================================
Git Branching
=============================================================================================================
25. Branching Out: When developers are working on a feature or bug they'll often create a copy (aka. branch) of their code they can make separate commits to. Then when they're done they can merge this branch back into their main master branch.
To create a branch 'clean_up'
$git branch clean_up
Branching
Branches are what naturally happens when you want to work on multiple features at the same time. You wouldn't want to end up with a master branch which has Feature A half done and Feature B half done.
Rather you'd separate the code base into two "snapshots" (branches) and work on and commit to them separately. As soon as one was ready, you might merge this branch back into the master branch and push it to the remote server.
26. Switching Branches:
To see the branches -- it will show main branch 'master' and other branches like 'clean_up'
$git branch
To switch the main branch to other local branch done by checkout command
$git checkout clean_up
Note:
All at Once--
$git checkout -b <new_branch>
to checkout and create a branch at the same time. This is the same thing as doing:
$git branch new_branch
$git checkout new_branch
27. Removing All The things:
So you're in the clean_up branch. you can finally remove all those pesky octocats by using the git rm command which will not only remove the actual files from disk, but will also stage the removal of the files for us.
$git rm '*.txt'
(using wildcard to remove all .txt files)
$git rm -r <folder_of_cats>
This will recursively remove all folders and files from the given directory.
28. Commiting Branch Changes:
Now that you've removed all the cats you'll need to commit your changes.
Feel free to run git status to check the changes you're about to commit.
$git commit -m "Remove all the cats"
to see the branch status
$git status
The '-a' option:
If you happen to delete a file without using 'git rm' you'll find that you still have to 'git rm' the deleted files from the working tree. You can save this step by using the '-a' option on 'git commit', which auto removes deleted files with the commit.
git commit -am "Delete stuff"
29. Switching Back to master:
$git checkout master
Pull Requests
If you're hosting your repo on GitHub, you can do something called a pull request.
A pull request allows the boss of the project to look through your changes and make comments before deciding to merge in the change. It's a really great feature that is used all the time for remote workers and open-source projects.
30. Preparing to Merge
To merge the changes from the local branches to main branch. Here we are in main branch, So we merge the clean_up branch to master
$git merge clean_up
Merge Conflicts
Merge Conflicts can occur when changes are made to a file at the same time. A lot of people get really scared when a conflict happens, but fear not! They aren't that scary, you just need to decide which code to keep.
31. Keeping Things Clean
We are done with clean_up branch as we merge all code to main branch. So we don't need it anymore.
You can use git branch -d <branch name> to delete a branch
$git branch -d clean_up
Force delete:
What if you have been working on a feature branch and you decide you really don't want this feature anymore? You might decide to delete the branch since you're scrapping the idea. You'll notice that git branch -d bad_feature doesn't work. This is because -d won't let you delete something that hasn't been merged.
You can either add the --force (-f) option or use -D which combines -d -f together into one command.