From charlesreid1

 
(9 intermediate revisions by the same user not shown)
Line 3: Line 3:
==Stop Github Asking for Username and Password==
==Stop Github Asking for Username and Password==


If git keeps pestering you for your github username and password, the problem is that all of your repositories are being checked out using their HTTPS url to github. This '''always''' requires you to enter your password.  
If git keeps pestering you for your github username and password, the problem is that the remote URL is an HTTPS URL, which always requires a username/password. Here are notes on how to fix that: [[Github/Stop Asking for Username and Password]]


Instead, you should set up your computer to be identified by Github, which will allow you to check out repositories using their GIT url to github. This will be an identical repository, but accessed using a different authentication system (public/private keys over SSH). This is a much more efficient and safe method for authenticating.
==Update/Sync a Fork==
 
If you have a fork that has fallen behind the upstream by several commits, here are instructions for how to update it: [[Git/Sync a Fork]]


The steps:
==Delete Remote Branch==
* Create your public private key pair
* Tell Github your public key
* Change your github repo to use the GIT url and not the HTTPS url


===Step 1: Set up Github SSH Keys===
If you're making multilpe fixes to a remote repository, you might end up creating multiple branches, one for each PR you're submitting. This leaves lots of remote branches laying around. If you want to delete these, here are instructions: [[Git/Delete Remote Branch]]


Useful instructions: https://help.github.com/articles/generating-ssh-keys
==Move Last N Commits Off Of Master Branch==


If you don't have a public key, you will need to make one.
Ever get in the zone and make a bunch of commits to your repo, only to realize you were inadvertently working on the master branch?


If you have one already, your public key is in <code>~/.ssh/id_rsa.pub</code> so give Github the contents of that file. Log into your Github account, click account settings, and find the SSH Keys option. Add a new SSH key, and copy and paste the contents of <code>~/.ssh/id_rsa.pub</code> to the box and save the key.
Here's how you can move the last N commits off of the master branch and onto a new, separate branch (and rewind master so it points at the original commit where you started to make changes):
 
Step 1: Create a new branch, which will branch from the repository's current state, and will contain all of the changes you've made up to this point. This command will leave you on the master branch.


<pre>
<pre>
$ cat ~/.ssh/id_rsa.pub
git branch new-feature
</pre>
 
Step 2: You are still on the master branch. Rewind the master branch to the appropriate commit, or rewind it N commits. Here's the syntax for both:
 
<pre>
git reset --hard a1b2c3d4    # revert to particular commit
git reset --hard HEAD~5      # revert back 5 commits
</pre>
 
Step 3: You've fixed master, and you've saved your changes to a new branch, so now all you have to do is check out your new branch and you'll be back where you started:
 
<pre>
git checkout new-feature
</pre>
 
==Delete Latest Commits From (Remote) Master Branch==
 
If you royally fuck things up and commit to the master branch when you did not intend to, git purists will relegate you to the seventh layer of hell.
 
Don't worry, hope is not lost, here's how you can fix it (quick, before anybody notices!): [[Git/Delete Commits from Master]]
 
Note that if anyone pulls your commit and then pushes changes to the repo, they will be committing changes on top of the old commit that you didn't delete. Meanwhile, anyone who never sees the commit you deleted will be committing changes on top of the new commit that you wanted.
 
==Resolving Git Push Conflicts==
 
Notes on how to resolve conflicts that occur when running <code>git push</code>. See [[Git/Resolving Push Conflicts]]


........blah..........
==Diff with Prior Commit==


If you want to compare a commit to the prior commit, use the notation <code>hash^</code> to refer to the previous commit:
<pre>
git diff 572ecd1^ 572ecd1
</pre>
</pre>


Make sure you don't give Github your private key, which would be a bad idea. Your private key is in <code>~/.ssh/id_rsa</code> (no pub extension means it is private).
==Discard Changes==


===Step 2: Switch Repos from HTTPS to SSH===
===Discard Changes Not Yet Added===


You can switch your repos from HTTPS to SSL without opening the browser. First check the repo url using the git remote command:
If changes to a file have not been added to the staging area (i.e., you modified the file but did not run git add), restore the file like this:


<pre>
<pre>
$ git remote -v
git checkout -- <filename>
origin  https://github.com/user/repo.git (fetch)
origin  https://github.com/user/repo.git (push)
</pre>
</pre>


Find the addresses of the github remotes, and switch the protocol from HTTPS to GIT (note the syntax):
===Discard Changes Already Added===
 
On the other hand, if you made changes to a file and ran git add, but you no longer want those changes, you should run a slightly different command:


<pre>
<pre>
$ git remote set-url origin git@github.com:user/repo.git
git reset -- HEAD <file>
</pre>
</pre>


==Create Orphan Branch==


==Update/Sync a Fork==
Before you do this, make sure you have stashed or committed all changes. This will throw away the current contents of your repository, but will not require you to check out an entirely new copy of your git repo - it does not touch the .git directory.


If you have a fork that has fallen behind the upstream by several commits, here are instructions for how to update it: [[Git/Sync a Fork]]
(Alternatively, you can clone a second copy of your repo, and create the orphan branch from there.)


==Delete Remote Branch==
To create an orphan branch (i.e., an empty branch that shares no history with any other branches):


If you're making multilpe fixes to a remote repository, you might end up creating multiple branches, one for each PR you're submitting. This leaves lots of remote branches laying around. If you want to delete these, here are instructions: [[Git/Delete Remote Branch]]
<pre>
git checkout --orphan gh-pages
</pre>


==Delete Latest Commits From Master Branch==
Now, all of the files that were previously in your repository will show up as uncommitted/untracked files. You can remove everything in your current directory with this (BE CAREFUL):


If you royally fuck things up and commit to the master branch when you did not intend to, git purists will relegate you to the seventh layer of hell.
<pre>
rm -rf *
</pre>


Don't worry, hope is not lost, here's how you can fix it (quick, before anybody notices!): [[Git/Delete Commits from Master]]
Now test out your new branch by adding a single test file.


==Resolving Git Push Conflicts==
<pre>
echo "<h1>hello world</h1>" > index.html
git add index.html
git commit index.html -m 'initial commit of gh-pages branch'
git push origin gh-pages
</pre>


Notes on how to resolve conflicts that occur when running <code>git push</code>. See [[Git/Resolving Push Conflicts]]
(This results in a bunch of weird stuff.)


=Flags=


[[Category:Git]]
[[Category:Git]]

Latest revision as of 21:12, 18 April 2018

Routine Git Operations

Stop Github Asking for Username and Password

If git keeps pestering you for your github username and password, the problem is that the remote URL is an HTTPS URL, which always requires a username/password. Here are notes on how to fix that: Github/Stop Asking for Username and Password

Update/Sync a Fork

If you have a fork that has fallen behind the upstream by several commits, here are instructions for how to update it: Git/Sync a Fork

Delete Remote Branch

If you're making multilpe fixes to a remote repository, you might end up creating multiple branches, one for each PR you're submitting. This leaves lots of remote branches laying around. If you want to delete these, here are instructions: Git/Delete Remote Branch

Move Last N Commits Off Of Master Branch

Ever get in the zone and make a bunch of commits to your repo, only to realize you were inadvertently working on the master branch?

Here's how you can move the last N commits off of the master branch and onto a new, separate branch (and rewind master so it points at the original commit where you started to make changes):

Step 1: Create a new branch, which will branch from the repository's current state, and will contain all of the changes you've made up to this point. This command will leave you on the master branch.

git branch new-feature

Step 2: You are still on the master branch. Rewind the master branch to the appropriate commit, or rewind it N commits. Here's the syntax for both:

git reset --hard a1b2c3d4    # revert to particular commit
git reset --hard HEAD~5      # revert back 5 commits

Step 3: You've fixed master, and you've saved your changes to a new branch, so now all you have to do is check out your new branch and you'll be back where you started:

git checkout new-feature

Delete Latest Commits From (Remote) Master Branch

If you royally fuck things up and commit to the master branch when you did not intend to, git purists will relegate you to the seventh layer of hell.

Don't worry, hope is not lost, here's how you can fix it (quick, before anybody notices!): Git/Delete Commits from Master

Note that if anyone pulls your commit and then pushes changes to the repo, they will be committing changes on top of the old commit that you didn't delete. Meanwhile, anyone who never sees the commit you deleted will be committing changes on top of the new commit that you wanted.

Resolving Git Push Conflicts

Notes on how to resolve conflicts that occur when running git push. See Git/Resolving Push Conflicts

Diff with Prior Commit

If you want to compare a commit to the prior commit, use the notation hash^ to refer to the previous commit:

git diff 572ecd1^ 572ecd1

Discard Changes

Discard Changes Not Yet Added

If changes to a file have not been added to the staging area (i.e., you modified the file but did not run git add), restore the file like this:

git checkout -- <filename>

Discard Changes Already Added

On the other hand, if you made changes to a file and ran git add, but you no longer want those changes, you should run a slightly different command:

git reset -- HEAD <file>

Create Orphan Branch

Before you do this, make sure you have stashed or committed all changes. This will throw away the current contents of your repository, but will not require you to check out an entirely new copy of your git repo - it does not touch the .git directory.

(Alternatively, you can clone a second copy of your repo, and create the orphan branch from there.)

To create an orphan branch (i.e., an empty branch that shares no history with any other branches):

git checkout --orphan gh-pages

Now, all of the files that were previously in your repository will show up as uncommitted/untracked files. You can remove everything in your current directory with this (BE CAREFUL):

rm -rf *

Now test out your new branch by adding a single test file.

echo "<h1>hello world</h1>" > index.html
git add index.html
git commit index.html -m 'initial commit of gh-pages branch'
git push origin gh-pages

(This results in a bunch of weird stuff.)

Flags