Git Operations
From charlesreid1
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
Delete Latest Commits From 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
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