Skip to content

Instantly share code, notes, and snippets.

@snreid
Last active August 29, 2015 14:06
Show Gist options
  • Save snreid/5e7a48d43c09977a966d to your computer and use it in GitHub Desktop.
Save snreid/5e7a48d43c09977a966d to your computer and use it in GitHub Desktop.
Handy Git Commands

##Create, Merge, Delete Branch

Create new branch:

git checkout -b new_branch

Push new branch:

git push origin new_branch

Merge new branch:

Switch to branch you want to merge new_branch into (e.g. git checkout master)

Then: git merge new_branch

Delete new branch:

git branch -d new_branch

Delete in origin:

git push origin :new_branch

Merge new, more up-to-date, branch into an outdated master

Found here: http://stackoverflow.com/questions/2862590/how-to-replace-master-branch-in-git-entirely-from-another-branch

You should be able to use the "ours" merge strategy to overwrite master with seotweaks like this:

git checkout seotweaks
git merge -s ours master
git push origin seotweaks
git checkout master
git merge seotweaks

The result should be your master is now essentially seotweaks.

##Managing Remote URLs

View Remote URL:

git remote -v

Adding a Remote URL (To a new local project not yet on git): (if git isn’t already in the project: git init )

git remote add origin git@github.com:project/project.git

Change Remote URL:

git remote set-url origin git@github.com:project/project.git

##Clean out untracked/newly-created-uncommited files http://gitready.com/beginner/2009/01/16/cleaning-up-untracked-files.html

##In Case you Pushed Files you Shouldn’t Have (Ref: http://stackoverflow.com/questions/2047465/how-can-i-delete-a-file-from-git-repo) To remove files that you need locally, but shouldn’t have pushed to git, run the following command:

git rm --cached filename.txt

If entire directory:

git rm -R --cached directory-name/

Note: This will remove the file from git, but keep your local copy. You’ll probably want to add this file to the .gitignore.

##DELETE Files Accidentally Committed to git. (ref: http://stackoverflow.com/questions/12481639/remove-files-from-git-commit) This is actually to DELETE this files from both local copies and repo copies. May not be the preferred method. As a rookie, I accidentally did this instead of the above method. Glad I had copies elsewhere!

git reset --soft HEAD^

OR git reset --soft HEAD~1

git reset HEAD path/to/unwanted_file

##Ignore local File changes git update-index --assume-unchanged <file>

Then when you want to track them again git update-index --no-assume-unchanged <file>

##Reset local File changes git checkout -- path/to/file

##Stashing Source: http://stackoverflow.com/questions/11269256/how-to-name-a-stash-in-git This is how you do it:

git stash save "my_stash"

where "my_stash" is the stash name...

Some more useful things to know: All the stashes are stored in a stack. Type in :

git stash list

This will list down all your stashes.

To apply a stash and remove it from the stash stack, You can give,

git stash pop stash@{n}

To apply a stash and keep it in the stash stack, type:

git stash apply stash@{n}

Where n in the index of the stashed change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment