Skip to content

Instantly share code, notes, and snippets.

@pulcheri
Last active December 11, 2015 06:28
Show Gist options
  • Save pulcheri/4559121 to your computer and use it in GitHub Desktop.
Save pulcheri/4559121 to your computer and use it in GitHub Desktop.
git create/push/delete branch

How do I revert one file to the last commit in git?

git checkout -- filename You can do it without the -- (as suggested by nimrodm), but if the filename looks like a branch or tag (or other revision identifier), it may get confused, so using -- is best.

You can also check out a particular version of a file:

git checkout v1.2.3 -- filename # tag v1.2.3

git checkout stable -- filename # stable branch

git checkout origin/master -- filename # upstream master

git checkout HEAD -- filename # the version from the most recent commit

git checkout HEAD^ -- filename # the version before the most recent commit

Branch

  1. Create branch git branch BRANCHNAME git push -u origin BRANCHNAME
  2. Get it on another machine git fetch origin git checkout --track origin/BRANCHNAME
  3. Delete git push origin --delete BRANCHNAME
  4. Create Branch while preserving current changes git checkout -b BRANCHNAME

How to migrate GIT repository from one server to a new one

http://stackoverflow.com/questions/1484648/how-to-migrate-git-repository-from-one-server-to-a-new-one

cd /path/to/my/repo
git remote add origin_new https://new_repo_url/repo.git
git push -u origin_new --all   # to push up the repo for the first time
git remote rm origin vim .git/config # edit the.git/config file to change the origin_new to origin.
git push origin_new master # Then push the content to the new location

What is the difference between “git branch” and “git checkout -b”?

git checkout -b BRANCH_NAME creates a new branch and goes to the new branch while git branch BRANCH_NAME creates a new branch but leaves you on the same branch. git checkout -b creates a branch and checks it out. It is the short for: 

git branch name

git checkout name

Stashing

http://www.gitguys.com/topics/temporarily-stashing-your-work/

git stash
git stash pop

How do I revert all local changes in a GIT managed project to previous state?

http://stackoverflow.com/questions/1146973/how-do-i-revert-all-local-changes-in-a-git-managed-project-to-previous-state

If you want to revert changes made to your working copy, do this:

git checkout .

If you want to revert changes made to the index (i.e., that you have added), do this:

git reset

If you want to revert a change that you have committed, do this:

git revert ...

How do I remove local (untracked) files from my current git branch?

http://stackoverflow.com/questions/61212/how-do-i-remove-local-untracked-files-from-my-current-git-branch

git clean -f But beware... there's no going back. Use -n or --dry-run to preview the damage you'll do.

If you want to also remove directories, run git clean -f -d

If you just want to remove ignored files, run git clean -f -X

If you want to remove ignored as well as non-ignored files, run git clean -f -x

Note the case difference on the X for the two latter commands.

If clean.requireForce is set to "true" (the default) in your configuration, then unless you specify -f nothing will actually happen, with a recent enough version of git.

Note that as of git-1.6.0, the dashed style of writing git commands (ie, git-clean instead of git clean) is obsoleted.

How do I revert all local changes in Git

http://stackoverflow.com/questions/1146973/how-do-i-revert-all-local-changes-in-git-managed-project-to-previous-state

DANGER AHEAD: (please read the comments. Executing the command proposed in my answer might delete more than you want)

to completely remove all files including directories I had to run

git clean -f -d

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