From charlesreid1

(Created page with "=Routine Git Operations= ==Stop Github Asking for Username and Password== ===Step 1: Set up Github SSH Keys=== https://help.github.com/articles/generating-ssh-keys ===Step...")
 
 
(22 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==


===Step 1: Set up Github SSH Keys===
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]]


https://help.github.com/articles/generating-ssh-keys
==Update/Sync a Fork==


===Step 2: Switch Repos from HTTPS to SSH===
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]]


http://stackoverflow.com/questions/10126381/why-does-github-keep-asking-me-for-repo-credentials
==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.


<pre>
<pre>
git remote set-url origin git@github.com:user/repo.git
git branch new-feature
</pre>
</pre>


=Scenarios=
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:


This section covers some scenarios I often run into, but sometimes forget how to resolve.
<pre>
git reset --hard a1b2c3d4    # revert to particular commit
git reset --hard HEAD~5      # revert back 5 commits
</pre>


==Git Conflicts==
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:
 
Sometimes, when you try and push your commits with <code>git push</code>, you'll see a message like this:


<pre>
<pre>
$ git push
git checkout new-feature
To https://github.com/siluria/transport.git
Merge branch 'master' of https://github.com/siluria/transport
! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/siluria/transport.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
</pre>
</pre>


Then, if you try and pull the latest changes with <code>git pull</code>, you might see a message like this:
==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]]
 
==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>
<pre>
$ git pull
git diff 572ecd1^ 572ecd1
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 8 (delta 0), reused 2 (delta 0)
Unpacking objects: 100% (8/8), done.
From https://github.com/siluria/transport
  153782f..10e0b68  master    -> origin/master
Auto-merging twodomain/SubdomainCatalyst.py
Auto-merging twodomain/Interface.py
Auto-merging twodomain/Driver.py
CONFLICT (content): Merge conflict in twodomain/Driver.py
Automatic merge failed; fix conflicts and then commit the result.
</pre>
</pre>


In this case, you can edit the file with the conflict, in this case, <code>Driver.py</code>. You'll see some lines like this:
==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:


<pre>
<pre>
<<<<<<< HEAD
git checkout -- <filename>
old code
=======
new code
>>>>>>> iss53
</pre>
</pre>


Edit this file to merge the code however you decide. Once you do that, you'll run <code>git add</code> on the code with the file, which tells git you have resolved the conflict.
===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 add Driver.py
git reset -- HEAD <file>
</pre>
</pre>


Then - this is key - you run <code>git commit</code> with NO OTHER ARGUMENTS.
==Create Orphan Branch==


If you try and add arguments, i.e., specifying which file to commit, or adding a commit message, you'll see an error like this:
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):


<pre>
<pre>
$ git commit Driver.py -m 'resolving conflict.'
git checkout --orphan gh-pages
fatal: cannot do a partial commit during a merge.
</pre>
</pre>


Just run <code>git commit</code> with no arguments:
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):


<pre>
<pre>
$ git commit
rm -rf *
[master d1d98ed] Merge branch 'master' of https://github.com/siluria/transport
</pre>
</pre>


Now you should be able to run <code>git push</code> with no problems:
Now test out your new branch by adding a single test file.


<pre>
<pre>
$ git push
echo "<h1>hello world</h1>" > index.html
Counting objects: 23, done.
git add index.html
Delta compression using up to 8 threads.
git commit index.html -m 'initial commit of gh-pages branch'
Compressing objects: 100% (12/12), done.
git push origin gh-pages
Writing objects: 100% (12/12), 1.48 KiB | 0 bytes/s, done.
Total 12 (delta 10), reused 0 (delta 0)
To https://github.com/siluria/transport.git
  10e0b68..d1d98ed  master -> master
</pre>
</pre>
(This results in a bunch of weird stuff.)
=Flags=
[[Category:Git]]
[[Category:SSH]]
[[Category:Github]]
[[Category:Version Control]]

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