[[git-tips]]
= Git tips

== Delete branches

To delete all of your branches except the branch you are on:

[source]
----
$ git checkout main
$ for br in `git branch` ; do git branch -D $br ; done
----

To delete one branch:

[source,options="nowrap",subs="+quotes"]
----
$ git checkout main
$ git branch -D <branch-name>
----

== Resolve conflicts

To resolve a merge conflict in an existing pull request:

[source,options="nowrap",subs="+quotes"]
----
$ git checkout <branch-name>
$ git branch -u origin <branch-name>
$ git pull --rebase upstream main
$ git push -f origin HEAD
----

== Reset your fork

If your fork is both ahead of and behind the origin you can reset your fork to match the origin and start with a clean slate.

[source]
----
$ git checkout main
$ git reset --hard upstream/main
$ git push origin main --force
$ git pull upstream main
$ git push origin main --force
----

== Using `ssh-agent` to save your SSH key's passphrase

If you have to enter your SSH key's passphrase whenever working with the repository from the command line, you might want to use the ssh-agent to remember the passphrase for you.

Before using the `ssh-agent` you will see a prompt to enter your passphrase after each git command.

[source]
----
[amq-repo]$ git pull --rebase upstream main
Enter passphrase for key '/home/<username>/.ssh/id_rsa':
----

To add your passphrase to the `ssh-agent`:

[source]
----
[amq-repo]$ ssh-add
Enter passphrase for /home/<username>/.ssh/id_rsa:
----

After entering your passphrase you will see confirmation that your passphrase has been saved:

[source]
----
Identity added: /home/<username>/.ssh/id_rsa (/home/<username>/.ssh/id_rsa)
----

== Access another writer’s unmerged commits

This is the process you can use if you need commits another writer has submitted in a merge request that is not yet merged.

. Check out a new topic branch from upstream/main as you normally do.
+
[source,options="nowrap",subs="+quotes"]
----
$ git fetch upstream
$ git checkout -b <new-topic-branch> upstream/main
----
. If you have not yet added that writer’s remote repository, add it now.
+
[source,options="nowrap",subs="+quotes"]
----
$ git remote add -f <user> git@github.com:<user>/strimzi-kafka-operator.git
----
. Rebase to bring in the changes that are in that user’s outstanding
 `origin/<merge-request-branch>` branch.
+
[source,options="nowrap",subs="+quotes"]
----
$ git rebase <user>/<merge-request-branch>
----
+
(you'll see the following response)
+
[source,options="nowrap",subs="+quotes"]
----
First, rewinding head to replay your work on top of it...
Fast-forwarded <new-topic-branch> to <user>/<merge-request-branch>
----
