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

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