|
7 | 7 |
|
8 | 8 | Follow these steps in your main branch
|
9 | 9 | (upstream" is the original repository and "origin" is the fork one) :
|
10 |
| -- `git checkout main` |
11 |
| -- `git pull upstream main` |
12 |
| -- `git reset --hard upstream/main` |
13 |
| -- `git push origin main --force` |
| 10 | +- ```sh |
| 11 | + git checkout main |
| 12 | + git pull upstream main |
| 13 | + git reset --hard upstream/main |
| 14 | + git push origin main --force |
| 15 | + ``` |
14 | 16 | </details>
|
15 | 17 |
|
16 | 18 | <details>
|
17 | 19 | <summary>
|
18 | 20 | <em>Why the previous commits of another branch are showing in the new branch</em>
|
19 | 21 | </summary>
|
20 | 22 |
|
21 |
| -When you create a new branch from a particular branch, you'll start from the point where that branch currently is. So all commit history will be there in the new branch as well. |
| 23 | +Let's say you have this repository with branch 'prev_branch' |
22 | 24 |
|
23 |
| - |
| 25 | +```sh |
| 26 | +---o---o---A prev_branch |
| 27 | +``` |
24 | 28 |
|
| 29 | +Let's say you commit something A in prev_branch and created new branch 'new_branch'1, |
| 30 | +it won't differ until you start commiting to new branch |
| 31 | + |
| 32 | +```sh |
| 33 | +---o---o---A prev_branch + new_branch |
| 34 | +``` |
| 35 | + |
| 36 | +Then you commit B in newbranch |
| 37 | + |
| 38 | +```sh |
| 39 | +---o---o---A prev_branch |
| 40 | + \ |
| 41 | + B new_branch |
| 42 | +``` |
| 43 | + |
| 44 | +The new commit B has A as its parent commit. Now prev_branch and new_branch differ in some way. |
| 45 | + |
| 46 | +```sh |
| 47 | +---o---o---A---X---Y prev_branch |
| 48 | + \ |
| 49 | + B---Z new_branch |
| 50 | +``` |
| 51 | +## Summary |
| 52 | + |
| 53 | +When you create a new branch from a particular branch, you'll start from the point where that branch currently is. So all commit history will be there in the new branch as well.</br> |
25 | 54 | A good rule of thumb is to always create a new branch from the branch that you intend to eventually merge the new branch into (main branch). So if D is intended to be merged into main at some future point in time, create it from the current tip of main.
|
26 | 55 |
|
27 |
| -for more details refer this [blog](https://www.reddit.com/r/git/comments/l7epj0/why_does_my_new_branch_contain_commits_from/) |
| 56 | +For more details refer [blog](https://www.reddit.com/r/git/comments/l7epj0/why_does_my_new_branch_contain_commits_from/) / [blog](https://stackoverflow.com/questions/37010110/git-pushes-old-commit-in-different-branch-to-new-branch/78666984#78666984) |
| 57 | + |
| 58 | +Follow this while creating new branch and commiting changes: |
| 59 | +```sh |
| 60 | +- git checkout main/master |
| 61 | +- git branch -b new_branch |
| 62 | +- git add . |
| 63 | +- git commit -m "message" |
| 64 | +``` |
| 65 | + |
28 | 66 | </details>
|
29 | 67 |
|
30 | 68 | <details>
|
31 | 69 | <summary>
|
32 | 70 | <em>Edit a commit</em>
|
33 | 71 | </summary>
|
34 | 72 |
|
35 |
| - 1. `git add `: Add new changes |
36 |
| - 2. `git commit --amend --no-edit`: it will combine the new staged files to the last commit. |
37 |
| - |
38 |
| -**or** |
39 |
| - |
40 |
| -When calling git reset, you need to specify the commit to reset to or you can specify an ancestor of HEAD, the current commit, using the tilde (~) suffix. |
41 |
| -- `git add .` |
42 |
| -- `git commit -m "This commit is a mistake"` |
43 |
| -- `git reset HEAD~` |
44 |
| -- `git add main.py # need to re-add files after reset` |
45 |
| -- `git commit -m "This commit corrects the mistake"` |
46 |
| - |
47 |
| -To undo the last two commits, use the commands: |
48 |
| -- `git add .` |
49 |
| -- `git commit -m "This commit is a mistake"` |
50 |
| -- `// make changes` |
51 |
| -- `git add .` |
52 |
| -- `git commit -m "This commit is another mistake"` |
53 |
| -- `git reset HEAD~2` |
54 |
| -- `git add .` |
55 |
| -- `git commit -m "this commit corrects both mistakes"` |
56 |
| - |
57 |
| -for more info refer this [blog](https://sentry.io/answers/undo-the-most-recent-local-git-commits/) |
58 |
| - |
59 |
| -**or** |
60 |
| - |
61 |
| -- `git commit --amend -m "New commit message"`: it will chnage the last commit message |
62 |
| -  |
63 |
| -for more info, watch this [video](https://www.youtube.com/watch?v=q53umU5vMkk) |
| 73 | + |
| 74 | +1. Convenient way to modify the most recent commit |
| 75 | + ```sh |
| 76 | + # Edit src.js and main.js |
| 77 | + git add src.js |
| 78 | + git commit |
| 79 | + # Realize you forgot to add the changes from main.js |
| 80 | + git add main.js |
| 81 | + git commit --amend --no-edit |
| 82 | + ``` |
| 83 | +2. Using git reset |
| 84 | + ```sh |
| 85 | + git add . |
| 86 | + git commit -m "This commit is a mistake" |
| 87 | + git reset HEAD~ |
| 88 | + git add main.js |
| 89 | + git commit -m "This commit corrects the mistake" |
| 90 | + ``` |
| 91 | + |
| 92 | +3. To undo the last two commits, use the commands: |
| 93 | + ```sh |
| 94 | + git add |
| 95 | + git commit -m "This commit is a mistake" |
| 96 | + # make changes to files |
| 97 | + git add . |
| 98 | + git commit -m "This commit is another mistake" |
| 99 | + # want to go back to 2nd last commit to make changes |
| 100 | + git reset HEAD~2 |
| 101 | + # make changes |
| 102 | + git add . |
| 103 | + git commit -m "this commit corrects both mistakes" |
| 104 | + ``` |
| 105 | + for more info refer this [blog](https://sentry.io/answers/undo-the-most-recent-local-git-commits/) |
| 106 | + |
| 107 | + 4. Change the last commit message |
| 108 | + ```sh |
| 109 | + # it will change the last commit's message |
| 110 | + git commit --amend -m "New commit message" |
| 111 | + ``` |
| 112 | +  |
| 113 | + for more info, watch this [video](https://www.youtube.com/watch?v=q53umU5vMkk) |
64 | 114 | </details>
|
65 | 115 |
|
66 | 116 |
|
67 | 117 | <details>
|
68 | 118 | <summary>
|
69 | 119 | <em>To Check the commit Tree:</em>
|
70 | 120 | </summary>
|
71 |
| - |
72 |
| -- Run `gitk` or `gitk --all` to visualize the commit tree. |
73 |
| -- `git log` show commit logs. |
| 121 | + |
| 122 | + ```sh |
| 123 | + #Run below to visualize the commit tree. |
| 124 | + gitk |
| 125 | + git log show commit logs. |
| 126 | + ``` |
74 | 127 | </details>
|
75 | 128 |
|
76 |
| -- **Rebase:** |
77 |
| - - `git fetch main`: Fetch the latest changes: |
78 |
| - - `git rebase upstream/main`: Rebase onto the upstream branch |
| 129 | +<details> |
| 130 | +<summary> |
| 131 | +<em>Rebase</em> |
| 132 | +</summary> |
| 133 | + |
| 134 | + ```sh |
| 135 | + #Fetch the latest changes |
| 136 | + git fetch main |
| 137 | + #Rebase onto the upstream branch |
| 138 | + git rebase upstream/main` |
| 139 | + ``` |
79 | 140 | </details>
|
80 | 141 |
|
81 |
| - |
82 |
| -- Avoid committing unnecessary files: |
83 |
| - - Avoid using `git commit -a`, which commits everything. |
84 |
| -- Use targeted commit commands: |
85 |
| - - Use `git add` to stage specific files. |
| 142 | +<details> |
| 143 | +<summary> |
| 144 | +<em>Good Practice</em> |
| 145 | +</summary> |
| 146 | + |
| 147 | + 1. Avoid committing unnecessary files: |
| 148 | + |
| 149 | + ```sh |
| 150 | + #Avoid using which commits everything. |
| 151 | + git commit -a |
| 152 | + ``` |
| 153 | + |
| 154 | + 2. Use targeted commit commands: |
| 155 | + ```sh |
| 156 | + git add specific file |
| 157 | + ``` |
| 158 | +</details> |
0 commit comments