├── CODE_OF_CONDUCT.md ├── LICENSE └── README.md /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our 6 | community a harassment-free experience for everyone, regardless of age, body 7 | size, visible or invisible disability, ethnicity, sex characteristics, gender 8 | identity and expression, level of experience, education, socio-economic status, 9 | nationality, personal appearance, race, religion, or sexual identity 10 | and orientation. 11 | 12 | We pledge to act and interact in ways that contribute to an open, welcoming, 13 | diverse, inclusive, and healthy community. 14 | 15 | ## Our Standards 16 | 17 | Examples of behavior that contributes to a positive environment for our 18 | community include: 19 | 20 | * Demonstrating empathy and kindness toward other people 21 | * Being respectful of differing opinions, viewpoints, and experiences 22 | * Giving and gracefully accepting constructive feedback 23 | * Accepting responsibility and apologizing to those affected by our mistakes, 24 | and learning from the experience 25 | * Focusing on what is best not just for us as individuals, but for the 26 | overall community 27 | 28 | Examples of unacceptable behavior include: 29 | 30 | * The use of sexualized language or imagery, and sexual attention or 31 | advances of any kind 32 | * Trolling, insulting or derogatory comments, and personal or political attacks 33 | * Public or private harassment 34 | * Publishing others' private information, such as a physical or email 35 | address, without their explicit permission 36 | * Other conduct which could reasonably be considered inappropriate in a 37 | professional setting 38 | 39 | ## Enforcement Responsibilities 40 | 41 | Community leaders are responsible for clarifying and enforcing our standards of 42 | acceptable behavior and will take appropriate and fair corrective action in 43 | response to any behavior that they deem inappropriate, threatening, offensive, 44 | or harmful. 45 | 46 | Community leaders have the right and responsibility to remove, edit, or reject 47 | comments, commits, code, wiki edits, issues, and other contributions that are 48 | not aligned to this Code of Conduct, and will communicate reasons for moderation 49 | decisions when appropriate. 50 | 51 | ## Scope 52 | 53 | This Code of Conduct applies within all community spaces, and also applies when 54 | an individual is officially representing the community in public spaces. 55 | Examples of representing our community include using an official e-mail address, 56 | posting via an official social media account, or acting as an appointed 57 | representative at an online or offline event. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported to the community leaders responsible for enforcement at 63 | sadypai@gmail.com. 64 | All complaints will be reviewed and investigated promptly and fairly. 65 | 66 | All community leaders are obligated to respect the privacy and security of the 67 | reporter of any incident. 68 | 69 | ## Enforcement Guidelines 70 | 71 | Community leaders will follow these Community Impact Guidelines in determining 72 | the consequences for any action they deem in violation of this Code of Conduct: 73 | 74 | ### 1. Correction 75 | 76 | **Community Impact**: Use of inappropriate language or other behavior deemed 77 | unprofessional or unwelcome in the community. 78 | 79 | **Consequence**: A private, written warning from community leaders, providing 80 | clarity around the nature of the violation and an explanation of why the 81 | behavior was inappropriate. A public apology may be requested. 82 | 83 | ### 2. Warning 84 | 85 | **Community Impact**: A violation through a single incident or series 86 | of actions. 87 | 88 | **Consequence**: A warning with consequences for continued behavior. No 89 | interaction with the people involved, including unsolicited interaction with 90 | those enforcing the Code of Conduct, for a specified period of time. This 91 | includes avoiding interactions in community spaces as well as external channels 92 | like social media. Violating these terms may lead to a temporary or 93 | permanent ban. 94 | 95 | ### 3. Temporary Ban 96 | 97 | **Community Impact**: A serious violation of community standards, including 98 | sustained inappropriate behavior. 99 | 100 | **Consequence**: A temporary ban from any sort of interaction or public 101 | communication with the community for a specified period of time. No public or 102 | private interaction with the people involved, including unsolicited interaction 103 | with those enforcing the Code of Conduct, is allowed during this period. 104 | Violating these terms may lead to a permanent ban. 105 | 106 | ### 4. Permanent Ban 107 | 108 | **Community Impact**: Demonstrating a pattern of violation of community 109 | standards, including sustained inappropriate behavior, harassment of an 110 | individual, or aggression toward or disparagement of classes of individuals. 111 | 112 | **Consequence**: A permanent ban from any sort of public interaction within 113 | the community. 114 | 115 | ## Attribution 116 | 117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 118 | version 2.0, available at 119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 120 | 121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct 122 | enforcement ladder](https://github.com/mozilla/diversity). 123 | 124 | [homepage]: https://www.contributor-covenant.org 125 | 126 | For answers to common questions about this code of conduct, see the FAQ at 127 | https://www.contributor-covenant.org/faq. Translations are available at 128 | https://www.contributor-covenant.org/translations. 129 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Sadanand Pai 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 |

Practical Git Guide

3 |

Practical guide to everyday Git commands

4 | 5 |

6 | 7 | 8 | 9 |

10 | 11 | image 12 | 13 |

Solutions to 80% of your daily git challenges

14 | 15 | Show your support by giving a ⭐ to this repo 16 |
17 | 18 |
19 | 20 | --- 21 | 22 | ### Contents 23 | 24 | 1. [I want to clone a project and start going through the code.](#1-i-want-to-clone-a-project-and-start-going-through-the-code) 25 | 1. [I have cloned the repo. But I am not able to see the proper code!!!](#2-i-have-cloned-the-repo-but-i-am-not-able-to-see-the-proper-code) 26 | 1. [Someone has made a few changes in the code and asked me to pull those changes. What should I do?](#3-someone-has-made-a-few-changes-in-the-code-and-asked-me-to-pull-those-changes-what-should-i-do) 27 | 1. [I have modified the code and added changes. How do I commit my changes?](#4-i-have-modified-the-code-and-added-changes-how-do-i-commit-my-changes) 28 | 1. [What if I want only some of my files added and pushed instead of all the changes?](#5-what-if-i-want-only-some-of-my-files-added-and-pushed-instead-of-all-the-changes) 29 | 1. [I have modified/formatted some code while going through it. Now I want the code to be back to the state as it was.](#6-i-have-modifiedformatted-some-code-while-going-through-it-now-i-want-the-code-to-be-back-to-the-state-as-it-was) 30 | 1. [I have committed my changes. How can I undo my change?](#7-i-have-committed-my-changes-how-can-i-undo-my-change) 31 | 1. [I have made some changes to the branch. Also, I wanted to pull the new changes. But it is not working.](#8-i-have-made-some-changes-to-the-branch-also-i-wanted-to-pull-the-new-changes-but-it-is-not-working) 32 | 1. [After applying the stash, I am getting a lot of conflicts in the code.](#9-after-applying-the-stash-i-am-getting-a-lot-of-conflicts-in-the-code) 33 | 1. [I have made some code changes. But I want to commit to a new separate branch.](#10-i-have-made-some-code-changes-but-i-want-to-commit-to-a-new-separate-branch) 34 | 1. [I just committed but forgot to add a few files to the commit. Is there a way to update the same commit with some modifications?](#11-i-just-committed-but-forgot-to-add-a-few-files-to-the-commit-is-there-a-way-to-update-the-same-commit-with-some-modifications) 35 | 1. [I am asked to raise a Pull Request (PR) to a branch. What am I supposed to do?](#12-i-am-asked-to-raise-a-pull-request-pr-to-a-branch-what-am-i-supposed-to-do) 36 | 1. [I do not want a branch anymore. How can I delete the branch?](#13-i-do-not-want-a-branch-anymore-how-can-i-delete-the-branch) 37 | 1. [I want to rename my local and remote branches. How can I do it?](#14-i-want-to-rename-my-local-and-remote-branches-how-can-i-do-it) 38 | 1. [I have made some changes to the code on the branch on which all of the developers are working. How can I publish my changes?](#15-i-have-made-some-changes-to-the-code-on-the-branch-on-which-all-of-the-developers-are-working-how-can-i-publish-my-changes) 39 | 1. [I created a commit and also pushed it. Is it possible to update that commit now?](#16-i-created-a-commit-and-also-pushed-it-is-it-possible-to-update-that-commit-now) 40 | 1. [I have created single/multiple commits. When I am trying to push my changes, getting a rejected message. I am stuck!!!](#17-i-have-created-singlemultiple-commits-when-i-am-trying-to-push-my-changes-getting-a-rejected-message-i-am-stuck) 41 | 1. [I followed the above steps but got conflicts after git pull.](#18-i-followed-the-above-steps-but-got-conflicts-after-git-pull) 42 | 1. [I have created single/multiple commits. When I am trying to push my changes, getting a rejected message. Can I pull the new changes without merging the commit (Rebase)?](#19-i-have-created-singlemultiple-commits-when-i-am-trying-to-push-my-changes-getting-a-rejected-message-can-i-pull-the-new-changes-without-merging-the-commit-rebase) 43 | 1. [I have raised a PR (Pull Request). But it is showing conflicts.](#20-i-have-raised-a-pr-pull-request-but-it-is-showing-conflicts) 44 | 1. [I have many commits. How can I transform them into a single commit (squash)?](#21-i-have-many-commits-how-can-i-transform-them-into-a-single-commit-squash) 45 | 1. [I have many commits. How can I transform them into a single commit (squash) with just commands?](#22-i-have-many-commits-how-can-i-transform-them-into-a-single-commit-squash-with-just-commands) 46 | 1. [I am trying to rebase my branch with the same or another branch. As I have many commits, I am getting a lot of conflicts on every commit to rebase.](#23-i-am-trying-to-rebase-my-branch-with-the-same-or-another-branch-as-i-have-many-commits-i-am-getting-a-lot-of-conflicts-on-every-commit-to-rebase) 47 | 1. [I have made changes and committed to a branch. I want to copy the same changes to another branch.](#24-i-have-made-changes-and-committed-to-a-branch-i-want-to-copy-the-same-changes-to-another-branch) 48 | 1. [I have tried to cherry-pick as shown in [24]. But I am getting conflicts.](#25-i-have-tried-to-cherry-pick-as-shown-in-24-but-i-am-getting-conflicts) 49 | 1. [I have multiple commits which I want to move to a different branch.](#26-i-have-multiple-commits-which-i-want-to-move-to-a-different-branch) 50 | 1. [I have pushed my changes and got it merged. I want to revert it immediately.](#27-i-have-pushed-my-changes-and-got-it-merged-i-want-to-revert-it-immediately) 51 | 1. [How do I reset the code of my branch to the code of a different branch?](#28-how-do-i-reset-the-code-of-my-branch-to-the-code-of-a-different-branch) 52 | 1. [I want to delete/undo the previous commit from my branch which I have already pushed. I am not looking for a revert. I just want to delete it.](#29-i-want-to-deleteundo-the-previous-commit-from-my-branch-which-i-have-already-pushed-i-am-not-looking-for-a-revert-i-just-want-to-delete-it) 53 | 54 |
55 | 56 | ### 1. I want to clone a project and start going through the code. 57 | 58 | - Get the repo URL/link 59 | - Open your terminal 60 | - Navigate to the location in the terminal where you want your project to be cloned 61 | - Enter the command `git clone URL` 62 | 63 | The repo URL can be found inside the repo. 64 | 65 | repo clone 66 | 67 | Github repo link example 68 | ![image](https://user-images.githubusercontent.com/12962887/182018086-bea2688f-baf8-47ab-9d27-4c390355464f.png) 69 | 70 | Bitbucket repo link example 71 | ![image](https://user-images.githubusercontent.com/12962887/182018164-7d2f6bcd-517e-4793-925d-afe61c39032c.png) 72 | 73 |
74 |
75 | 76 | --- 77 | 78 |
79 | 80 | ### 2. I have cloned the repo. But I am not able to see the proper code!!! 81 | 82 | By default the main/master branch is active. Ask which branch has the relevant code 83 | 84 | - Navigate inside the cloned folder `cd repo<-name>` 85 | - Enter the command `git checkout ` 86 | 87 | checkout 88 | 89 | --- 90 | 91 |
92 | 93 | ### 3. Someone has made a few changes in the code and asked me to pull those changes. What should I do? 94 | 95 | - Pull the changes `git pull` 96 | 97 | pull 98 |
99 |
100 | 101 | --- 102 | 103 |
104 | 105 | ### 4. I have modified the code and added changes. How do I commit my changes? 106 | 107 | First stage all the files and then commit your changes. You can create multiple commits. 108 | 109 | - Stage the files `git add *` 110 | - Commit the changes `git commit -m 'Your commit message'` 111 | 112 | commit 113 |
114 |
115 | 116 | --- 117 | 118 |
119 | 120 | ### 5. What if I want only some of my files added and pushed instead of all the changes? 121 | 122 | You can add files by mentioning the file/files with relative or full path. You can add files one by one or multiple files at a time using the commands 123 | 124 | - Add single file `git add ` 125 | - Add multiple files `git add ` 126 | 127 | add 128 | 129 | Similarly, to unstage a file use the command `git reset ` 130 | 131 |
132 |
133 | 134 | --- 135 | 136 |
137 | 138 | ### 6. I have modified/formatted some code while going through it. Now I want the code to be back to the state as it was. 139 | 140 | - To reset all the changes `git reset --hard` 141 | - To reset a single file `git checkout HEAD -- ` 142 | 143 | reset 144 | 145 |
146 |
147 | 148 | --- 149 | 150 |
151 | 152 | ### 7. I have committed my changes. How can I undo my change? 153 | 154 | You can undo the commit by resetting the HEAD. If you just want to undo the commit but let the changes be present then use the `soft` attribute else if you do want the commit along with the changes then use the `hard` attribute 155 | 156 | - `git reset --soft HEAD~1` (undo with changes preserved) 157 | - `git reset --hard HEAD~1` (undo with changes removed) 158 | 159 |
160 |
161 | 162 | --- 163 | 164 |
165 | 166 | ### 8. I have made some changes to the branch. Also, I wanted to pull the new changes. But it is not working. 167 | 168 | The command `git pull` may not work if the changes are done by someone else to the same files which you have also modified. 169 | 170 | - Stash the changes `git stash save -u` 171 | - Pull the changes now `git pull` 172 | - Retrieve the changes `git stash apply ` 173 | 174 | where n is the stash number. To get the list of stashes `git stash list` 175 | 176 | stash 177 | 178 | Note: You can stash multiple changes and bring them back as and when you like to 179 | 180 |
181 |
182 | 183 | --- 184 | 185 |
186 | 187 | ### 9. After applying the stash, I am getting a lot of conflicts in the code. 188 | 189 | If there are changes in the code on the region of the stashed code, it is expected to get conflicts. You will have to manually resolve all the conflicts. (Do it carefully) 190 | 191 | stash conflicts 192 | 193 | Below is the view of the VSCode, which helps in easily resolving the conflicts. 194 | ![VSCode conflict](https://user-images.githubusercontent.com/12962887/180612978-fffd4307-0045-4fd3-b52c-394d1d9a0059.png) 195 | 196 |
197 |
198 | 199 | --- 200 | 201 |
202 | 203 | ### 10. I have made some code changes. But I want to commit to a new separate branch. 204 | 205 | You can create a separate branch out of the current branch and commit it. This works both if you have already made changes or are yet to start making changes. 206 | 207 | - Create a new branch `git checkout -b ` 208 | - Stage all the changes `git add *` 209 | - Commit the changes `git commit -m ""` 210 | 211 | To switch between the branches use the command `git checkout ` 212 | 213 | branch out 214 | 215 | Note: `my-branch-name` is your local branch and not available for anyone else unless you push it 216 | 217 |
218 |
219 | 220 | --- 221 | 222 |
223 | 224 | ### 11. I just committed but forgot to add a few files to the commit. Is there a way to update the same commit with some modifications? 225 | 226 | Yes. You can update the commit by amending your changes. 227 | 228 | - To add files `git add ...` 229 | - To update the commit `git commit --amend --no-edit` 230 | 231 | To update with new commit message `git commit --amend -m 'My new commit message'` (This command can also be used to simply update the previous commit message without any code modifications) 232 | 233 | `--no-edit` is used to avoid the prompt to edit the commit message. If you want to modify the commit message as well during the update of the commit, then do not include `-m` along with your commit message 234 | 235 | amend 236 | 237 | Note: The `amend` updates the previous commit without creating a new one on top of the previous. (in reality, Git discards the previous commit and replaces it with a new commit) 238 |
239 |
240 | 241 | --- 242 | 243 |
244 | 245 | ### 12. I am asked to raise a Pull Request (PR) to a branch. What am I supposed to do? 246 | 247 | You can follow the same steps as given in the previous question. Once done you will push the code and raise a PR. It's that simple. Here we assume you are on the 'develop' branch and raising PR to the 'main' branch. 248 | 249 | - Create a new branch `git checkout -b ` 250 | - Stage all the changes `git add *` 251 | - Commit the changes `git commit -m ""` 252 | - Push the changes `git push` (as the branch is not present on the remote, it will show the command to use) 253 | - Enter `git push --set-upstream origin ` 254 | 255 | 256 | 257 | The URL to raise the PR will be automatically available as shown above. Use the link and open it in the browser. 258 | 259 | ![image](https://user-images.githubusercontent.com/12962887/180644608-ad9175df-64cd-4f8e-930c-64333ea2222a.png) 260 | 261 | Now select the base branch to which you want to raise a PR and click on 'Create a PR' 262 | 263 |
264 |
265 | 266 | --- 267 | 268 |
269 | 270 | ### 13. I do not want a branch anymore. How can I delete the branch? 271 | 272 | To delete a branch locally, check out a different branch than the one you want to delete. Here we will delete the branch named 'develop' 273 | 274 | - Switch to other branch `git checkout ` 275 | - Delete branch `git branch -d ` 276 | 277 | To delete the branch from remote as well 278 | 279 | - Delete remote branch `git push -d ` 280 | 281 | image 282 | 283 | Note: If `-d` does not allow to delete a branch, use the `-D`. Example: `git branch -D ` 284 | 285 |
286 |
287 | 288 | --- 289 | 290 |
291 | 292 | ### 14. I want to rename my local and remote branches. How can I do it? 293 | 294 | To rename a branch, checkout to the branch and rename it. 295 | 296 | - Check out other branch `git checkout ` 297 | - Rename your branch `git branch -m ` 298 | - Push to remote `git push : ` 299 | - Set upstream `git push -u ` 300 | 301 | <remote> is usually origin 302 | 303 | image 304 | 305 |
306 |
307 | 308 | --- 309 | 310 |
311 | 312 | ### 15. I have made some changes to the code on the branch on which all of the developers are working. How can I publish my changes? 313 | 314 | To move the changes from your local machine to remote (called origin), follow the below steps in your terminal 315 | 316 | - Stage the files `git add *` 317 | - Commit the changes `git commit -m ""` 318 | - Push the changes `git push` 319 | 320 | image 321 | 322 |
323 |
324 | 325 | --- 326 | 327 |
328 | 329 | ### 16. I created a commit and also pushed it. Is it possible to update that commit now? 330 | 331 | Yes. You can update the commit even after it is pushed. Everything will follow as mentioned in the previous question, but you will have to force push. 332 | 333 | - `git add ...` 334 | - `git commit --amend --no-edit` or `git commit --amend` 335 | - `git push -f` or `git push --force` 336 | 337 | image 338 | 339 | Note: You need to be very careful while pushing forcefully, as it may eliminate other commits if someone has done in between. Make sure you are working on the branch and no one else is simultaneously working on the same or branching out from the branch at your commit. 340 | 341 |
342 |
343 | 344 | --- 345 | 346 |
347 | 348 | ### 17. I have created single/multiple commits. When I am trying to push my changes, getting a rejected message. I am stuck!!! 349 | 350 | The rejection could be because the remote branch might be ahead of the local branch. Different techniques can be used here to achieve sync. 351 | 352 | - Pull the changes `git pull` 353 | - Continue with the merge commit created automatically 354 | - `git push` 355 | 356 | ![image](https://user-images.githubusercontent.com/12962887/180632103-bc118a25-eb58-4032-bc12-128b2c506e0c.png) 357 | 358 | If there are conflicts, then resolve them manually to proceed ahead as shown below. 359 | 360 |
361 |
362 | 363 | --- 364 | 365 |
366 | 367 | ### 18. I followed the above steps but got conflicts after git pull. 368 | 369 | If there are code changes on the same region from multiple commits, conflicts will occur. You need to resolve all the conflicts and proceed. 370 | 371 | - Resolve all the conflicts 372 | - Stage files `git add ` 373 | - Continue the merge `git merge --continue` 374 | - Push the changes `git push` 375 | 376 | ![pull conflict](https://user-images.githubusercontent.com/12962887/180636539-cad6c7ae-ec56-41e0-9ff0-21dd5e95382f.png) 377 | 378 | Note: If something goes wrong, in any of the above steps, then there is nothing to panic about. Just run `git merge --abort` and redo the steps. 379 | 380 |
381 |
382 | 383 | --- 384 | 385 |
386 | 387 | ### 19. I have created single/multiple commits. When I am trying to push my changes, getting a rejected message. Can I pull the new changes without merging the commit (Rebase)? 388 | 389 | Yes. You can pull the changes without a merge. This is called **Rebase**. I know you have heard it a lot. It is very simple though. 390 | 391 | - Pull with rebase `git pull --rebase` 392 | - Push the changes `git push` 393 | 394 | rebase 395 | 396 | If you get conflicts, then solve all the conflicts. Then 397 | 398 | - `git rebase --continue` 399 | - Resolve all conflicts 400 | - Push the changes `git push` 401 | 402 | ![image](https://user-images.githubusercontent.com/12962887/181306723-5d1929bf-2d7f-4648-bb2e-534f26ae4a34.png) 403 | 404 | Note: If something goes wrong, in any of the above steps, then there is nothing to panic about. Just run `git rebase --abort` and redo the steps. 405 | 406 |
407 |
408 | 409 | --- 410 | 411 |
412 | 413 | ### 20. I have raised a PR (Pull Request). But it is showing conflicts. 414 | 415 | pr conflict 416 | 417 | PR will show conflicts if the new changes added to the source branch are conflicting with your changes or your branch is lagging. 418 | 419 | There are 2 main approaches to solve this. 420 | 421 | - [Merge approach](#merge-approach) 422 | - [Rebase approach](#rebase-approach) 423 | 424 | **Follow any one of the approaches.** Don't try both of them. 425 | 426 | Assuming that your branch is `develop` and the source branch is `main` 427 | 428 | #### Merge approach 429 | 430 | - Checkout to main branch `git checkout main` 431 | - Pull changes `git pull` 432 | - Checkout to your branch `git checkout develop` 433 | - Merge the changes `git merge main` 434 | - Resolve all the conflicts and add to staging `git add ` 435 | - If conflicts are present `git merge --continue` 436 | - Push the changes `git push` 437 | 438 | ![merge](https://user-images.githubusercontent.com/12962887/181508483-a0b79ea8-431f-499f-b802-522b2e9fd02f.png) 439 | 440 | #### Rebase approach 441 | 442 | - Checkout to main branch `git checkout main` 443 | - Pull changes `git pull` 444 | - Checkout to your branch `git checkout develop` 445 | - Rebase the branch `git rebase main` 446 | - Resolve all the conflicts and add to staging `git add ` 447 | - Run `git rebase --continue` after resolving the conflicts 448 | - Push the changes `git push -f` 449 | 450 | ![rebase](https://user-images.githubusercontent.com/12962887/181511250-658f490b-0bfe-4d17-bae6-485a1c1d1692.png) 451 | 452 | Note: You may have to run `git rebase --continue` multiple times if there are multiple conflicts on your multiple commits. 453 | 454 |
455 |
456 | 457 | --- 458 | 459 |
460 | 461 | ### 21. I have many commits. How can I transform them into a single commit (squash)? 462 | 463 | Converting multiple commits into one is known as **Squashing**. We will achieve this by rebasing with the help of the interactive feature of the editor (VSCode). This will be both easier and simple. 464 | 465 | - Run `git rebase -i HEAD~` (where n refers to the number of commits to squash) 466 | - Mark all the commits as 'Squash' except the oldest one 467 | - Click on 'Start rebase' 468 | - Enter the commit message 469 | 470 | squash 471 | 472 | ![image](https://user-images.githubusercontent.com/12962887/181514663-9e04cc4a-45f3-41b5-9a4b-bde2f6fda1d5.png) 473 | 474 | ![image](https://user-images.githubusercontent.com/12962887/181516003-21e0a84f-30c5-47ce-9466-f1a68b7dc697.png) 475 | 476 | Note: If you had already pushed the commits, then you will have to use the command `git push -f` to reflect squash on the remote branch as well. 477 | 478 |
479 |
480 | 481 | --- 482 | 483 |
484 | 485 | ### 22. I have many commits. How can I transform them into a single commit (squash) with just commands? 486 | 487 | You can use the technique of undoing to achieve this easily. 488 | 489 | - Undo the number of commits you need to squash `git reset --soft HEAD~` 490 | - Commit them again `git commit -m 'Commit message'` 491 | 492 | Note: If you had already pushed the commits, then you will have to use the command `git push -f` to reflect squash on the remote branch as well. 493 | 494 |
495 |
496 | 497 | --- 498 | 499 |
500 | 501 | ### 23. I am trying to rebase my branch with the same or another branch. As I have many commits, I am getting a lot of conflicts on every commit to rebase. 502 | 503 | - You can first squash all commits to one commit (Refer [21](#21-i-have-many-commits-how-can-i-transform-them-into-a-single-commit-squash)). 504 | - Once done, rebase the branch and resolve all conflicts in a single go. (Refer [20](#rebase-approach)) 505 | 506 |
507 |
508 | 509 | --- 510 | 511 |
512 | 513 | ### 24. I have made changes and committed to a branch. I want to copy the same changes to another branch. 514 | 515 | To copy the changes of a commit from one branch to another, you can use **cherry pick**. First obtain the commit id of the commit, which you want to copy. 516 | 517 | - Checkout the branch `git checkout ` 518 | - Cherry-pick the commit `git cherry-pick ` 519 | 520 | cherry pick 521 | 522 |
523 |
524 | 525 | --- 526 | 527 |
528 | 529 | ### 25. I have tried to cherry-pick as shown in [24](#24-i-have-made-changes-and-committed-to-a-branch-i-want-to-copy-the-same-changes-to-another-branch). But I am getting conflicts. 530 | 531 | If you get any conflicts, resolve the conflicts first. Once all the conflicts are resolved, they continue the cherry-pick. 532 | 533 | - Add resolved files to stage `git add ` 534 | - Continue cherry-pick `git cherry-pick --continue` 535 | 536 | cherry pick conflict 537 | 538 | Note: If something goes wrong in between, reset the process by using the command `git cherry-pick --abort` (You can start the process again) 539 | 540 |
541 |
542 | 543 | --- 544 | 545 |
546 | 547 | ### 26. I have multiple commits which I want to move to a different branch. 548 | 549 | To copy a range of commits from one branch to another, you can note down the older commit id from history and the newer commit id. 550 | 551 | - Checkout the branch `git checkout ` 552 | - Cherry pick the commit `git cherry-pick ..` 553 | 554 | cherry pick multiple 555 | 556 |
557 |
558 | 559 | --- 560 | 561 |
562 | 563 | ### 27. I have pushed my changes and got it merged. I want to revert it immediately. 564 | 565 | Revert creates a reverse commit where the changes made will reverse and is created as a new commit. To revert a particular commit first, obtain its commit id. 566 | 567 | - Revert the commit `git revert ` 568 | - Push the commit `git push` 569 | 570 | revert 571 | 572 |
573 |
574 | 575 | --- 576 | 577 |
578 | 579 | ### 28. How do I reset the code of my branch to the code of a different branch? 580 | 581 | When resetting a branch to a different branch, the codebase on your branch will become the same as the other branch. To achieve this, you can use the 'reset' command. 582 | 583 | - Checkout to the branch `git checkout ` 584 | - Reset with the branch name `git reset --hard ` 585 | 586 | In this case, 'your-branch-name' will match the codebase of 'source-branch' 587 | 588 | reset 589 | 590 |
591 |
592 | 593 | --- 594 | 595 |
596 | 597 | ### 29. I want to delete/undo the previous commit from my branch which I have already pushed. I am not looking for a revert. I just want to delete it. 598 | 599 | Revert will reverse the changes creating a new commit. If you want to remove the previous commit, then you can undo and force push the branch. 600 | 601 | - Reset by undoing `git reset --hard HEAD~` (n is the number of commits to undo) 602 | - Push the changes `git push -f` 603 | 604 | undo 605 | 606 | Note: Force push is needed as the history of the branch is changing in this. If the commit is not pushed then it is not needed. 607 | 608 |
609 |
610 | 611 | --- 612 | 613 | ### License 614 | 615 | This repository is MIT licensed. [Read more](./LICENSE) 616 | --------------------------------------------------------------------------------