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 10, 2024
1 parent a8434d3 commit 54abb70
Showing 1 changed file with 75 additions and 52 deletions.
127 changes: 75 additions & 52 deletions GIT_CONFLICTS.md
Original file line number Diff line number Diff line change
@@ -1,77 +1,100 @@
# Wanderlust Project

## Overview
Welcome to the Wanderlust project! This is a collaborative effort to build an amazing application. We appreciate your contributions and aim to make the process as smooth as possible.
## Welcome to the Wanderlust project! This is a collaborative effort to build an amazing application. We appreciate your contributions and aim to make the process as smooth as possible.

## Common QnA for Git and Open Source Contributions

<details>
<summary>

### How can I know if I’m in a bad situation?
<em>How can I know if I’m in a bad situation?</em>
</summary>
I suggest you to run **`gitk`** or **`gitk --all`**. The commit tree should be self-explainatory.

Also, if **`git log`** shows you any merge commits, this means that it isn’t properly synced with the upstream branch
- 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>

### How to Hard Sync Master with Upstream
<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
git reset --hard upstream/main
git push origin main --force
```

- `git remote add upstream /url-to-original-repo`
- `git fetch upstream`
- `git reset --hard upstream/main`
- `git push origin main --force`
</details>

## Better commits

You should not push what is not needed. Doing **`git commit -a`**, instead, commits just everything. This, often, is bad.
You’d better use **`git add`** and, most of all, **`git add -p`**
**`-p`** stays for “patch”: it will ask, for each “block” of code which you could commit, if you really want to commit it.
It is very simple to use, and will make you more responsible about what you’re commiting.
- 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 commiting, use **`git diff --cached`**, it will 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.
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.

## Edit a commit
Sometimes you committed something, but someone asks you to fix something, or you want to fix something yourself. Instead of making a new commit, you can also edit the previous commit(s).
<details>
<summary>
<em>Edit a commit</em>
</summary>

**`git reset --soft HEAD^`** (will reset the commit history to the previous commit. The —soft option makes sure the files don’t get reset too)
Then edit whatever you want to edit
**`git commit -a -v -c ORIG_HEAD`** (recommits with the same commit message. Because of the -v option, you can check if everything goes well)
- `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>

If you do this after pushing, you’ll have to use **`git push -f`** next time you try to push.
<details>
<summary>
<em>What should I do if I’m in a bad situation?</em>
</summary>

## What should I do if I’m in a bad situation?
- **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>

[](https://github.com/emesene/emesene/wiki/GitHowTo#what-should-i-do-if-im-in-a-bad-situation)
<details>
<summary>
<em>Recovering from a bad merge or accidental deletion</em>
</summary>

rebase.
Just do **`git fetch main`** (fetch the latest changes from the upstream branch)
**`git rebase upstream/main`** (rebase upon the upstream branch)
- `git reflog`
- `git reset HEAD@{index}`
</details>

Sometimes, if you have any merge commits in your branch, this won’t work. The best way to get rid of them is using **`git fetch upstream`** (this will fetch the latest changes of the upstream branch) and then do **`git reset --hard upstream/main`**. BE AWARE THAT THIS WILL THROW AWAY ALL YOUR CHANGES.
<details>
<summary>
<em>Amending the last commit</em>
</summary>

If you want to keep your changes, you can put them in a different branch first **`git branch branchname`**. Now you’ll have a branch with your committed changes backed up. It’s now safe to reset. If you want to pull the changes back to your master branch, you could use **`git cherry-pick branchname 1234567890abcdef1234567890abcdef12345678`**, where 1234567890abcdef1234567890abcdef12345678 is the commit you want in your master branch.
- `git commit --amend --no-edit`
</details>

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

### Recovering from a bad merge or accidental deletion
```
git reflog
git reset HEAD@{index}
```
### Amending the last commit
```
git commit --amend --no-edit
```
- `git commit --amend`
</details>

### Changing the last commit message
```
git commit --amend
```
<details>
<summary>
<em>Undoing a commit from several commits ago</em>
</summary>

### Undoing a commit from several commits ago
```
git log
git revert [saved hash]
```
- `git log`
- `git revert [saved hash]`
</details>

0 comments on commit 54abb70

Please sign in to comment.