Skip to content

Commit

Permalink
feat-krishnaacharyaa#378: created readme for git conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
rushil-b-patel committed Jun 22, 2024
1 parent 7bbe009 commit c08f38c
Showing 1 changed file with 23 additions and 64 deletions.
87 changes: 23 additions & 64 deletions GIT_CONFLICTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,97 +2,56 @@

<details>
<summary>
<em>How can I know if I’m in a bad situation?</em>
<em>How to Hard Sync local Master with Original Upstream</em>
</summary>

- Run `gitk` or `gitk --all` to visualize the commit tree.
- Check if `git log` shows any merge commits, indicating a lack of proper synchronization with the upstream branch.
</details>

<details>
<summary>
<em>How to Hard Sync Master with Upstream</em>
</summary>

To hard sync your local master branch with the upstream master branch, follow these steps in your main branch:

- `git remote add upstream /url-to-original-repo`
- `git fetch upstream`
Follow these steps in your main branch
(upstream" is the original repository and "origin" is the fork one) :
- `git checkout main`
- `git pull upstream main`
- `git reset --hard upstream/main`
- `git push origin main --force`
</details>

## Better commits

- Avoid committing unnecessary files:
- Avoid using `git commit -a`, which commits everything.
- Use targeted commit commands:
- Use `git add` to stage specific files.
- Use `git add -p` for a patch mode to review changes before staging.
- Ensure changes are correct before committing:
- Use `git diff --cached` to display staged changes.
- Use `git commit -v` to view diffs while writing the commit message.

If you want to make sure about what you’re committing, use `git diff --cached` to display the ready-to-commit changes. Also, I like to use `git commit -v`, which displays the diff as a comment, so that I can view it while writing the commit message.

<details>
<summary>
<em>Edit a commit</em>
</summary>

- `git reset --soft HEAD^`: Reset to the previous commit without changing files.
- Edit the necessary changes.
- `git commit -a -v -c ORIG_HEAD`: Recommit with the same message and verify changes.
- After pushing, use force push: `git push -f`.
</details>

<details>
<summary>
<em>What should I do if I’m in a bad situation?</em>
</summary>
- `git reset --soft HEAD~1` gets back to the last commit(for more information refer this [blog](https://www.datacamp.com/tutorial/git-reset-revert-tutorial) )
- or
- `git commit --amend`: ammend the most recent commit.
- `git commit --amend -m "New commit message"` : you can set the commit message directly

- **Rebase:**
- Fetch the latest changes: `git fetch main`
- Rebase onto the upstream branch: `git rebase upstream/main`
- **Remove merge commits:**
- Fetch latest changes: `git fetch upstream`
- Discard all changes and reset: `git reset --hard upstream/main`
- **Keep your changes:**
- Create a backup branch: `git branch branchname`
- Safely reset your branch
- Pull changes back to master: `git cherry-pick branchname 1234567890abcdef1234567890abcdef12345678`
</details>


<details>
<summary>
<em>Recovering from a bad merge or accidental deletion</em>
<em>To Check the commit Tree:</em>
</summary>

- `git reflog`
- `git reset HEAD@{index}`
- Run `gitk` or `gitk --all` to visualize the commit tree.
- `git log` show commit logs.
</details>

<details>
<summary>
<em>Amending the last commit</em>
<em>Undoing a commit from several commits ago</em>
</summary>

- `git commit --amend --no-edit`
- `git log`
- `git revert [hash of the commit]`
</details>

<details>
<summary>
<em>Changing the last commit message</em>
</summary>

- `git commit --amend`
- **Rebase:**
- `git fetch main`: Fetch the latest changes:
- `git rebase upstream/main`: Rebase onto the upstream branch
</details>

<details>
<summary>
<em>Undoing a commit from several commits ago</em>
</summary>

- `git log`
- `git revert [saved hash]`
</details>
- Avoid committing unnecessary files:
- Avoid using `git commit -a`, which commits everything.
- Use targeted commit commands:
- Use `git add` to stage specific files.

0 comments on commit c08f38c

Please sign in to comment.