Git snippets and tricks

Read this:

Git from the inside out

Look at this workflow:

http://justinhileman.info/git-pretty/

git-pretty

1. Ignore files that are already tracked

  1. First commit any outstanding code changes,
  2. and then, run this command:
    git rm -r --cached .
    This removes everything from the index
  3. then just run:
    git add .
  4. Commit it:
    git commit -m ".gitignore is now working"

2. Edit an incorrect commit message in Git

git commit --amend -m "New commit message"

If you’ve already pushed, just force push again:

git push -f origin branchname

Note: like rebasing, this is changing the history. If someone has cloned/pulled from your repo between the original and rewritten history then they won’t be able to pull after the rewrite (for that branch).

3. Undo the last commit

$ git commit ...              #(1)
$ git reset --soft 'HEAD^'    #(2)
$ edit                        #(3)
$ git add ....                #(4)
$ git commit -c ORIG_HEAD     #(5)

(1) This is what you want to undo
(2)This is most often done when you remembered what you just committed is incomplete, or you misspelled your commit message, or both. Leaves working tree as it was before “reset”. (The quotes may or may not be required in your shell)
(3)Make corrections to working tree files.
(4) Stage changes for commit.
(5) “reset” copies the old head to .git/ORIG_HEAD; redo the commit by starting with its log message. If you do not need to edit the message further, you can give -C option instead.

See this answer on StackOverflow for a more elaborate explanation

4. Delete all changes from working directory including new untracked files.

git reset --hard # removes staged and working directory changes
git clean -f -d # remove untracked files
 
 
( git clean -f -x -d # CAUTION: as above but removes ignored files like config. )

5. Force pull

Force an overwrite of local files on a Git pull?

I did this succesfully on my server where I force pulled from a bare repository (on the same server).  Source: http://stackoverflow.com/a/8888015

git fetch --all
git reset --hard origin/master

Explanation:

git fetch downloads the latest from remote without trying to merge or rebase anything.

Then the git reset resets the master branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/master, so if you have any local changes, they will be lost. With or without --hard, any local commits that haven’t been pushed will be lost.

If you have any files that are not tracked by Git (e.g. uploaded user content), these files will not be affected.

6. Force Push

You can completely specify specific branches and a remote:

git push   -f
git push origin master -f # Example

When the branch to push branch is omitted, Git will figure it out based on your config settings. In Git versions after 2.0, a new repo will have default settings to push the currently checked-out branch:

git push  -f
git push origin -f # Example

When both the remote and the branch are omitted, the behavior of just git push –force is determined by your push.default Git config settings:

git push --force

As of Git 2.0, the default setting, simple, will basically just push your current branch to its upstream remote counter-part. The remote is determined by the branch’s branch..remote setting, and defaults to the origin repo otherwise.

7. Change remote origin

git remote set-url origin [URL]

8. Remove submodule

  1. Delete the relevant section from the .gitmodules file.  The section would look similar to:
    [submodule "vendor"]
    	path = vendor
    	url = git://github.com/some-user/some-repo.git
  2. Stage the .gitmodules changes via command line using:git add .gitmodules
  3. Delete the relevant section from .git/config, which will look like:
    [submodule "vendor"]
    	url = git://github.com/some-user/some-repo.git
  4. Run git rm --cached path/to/submodule .  Don’t include a trailing slash — that will lead to an error.
  5. Run rm -rf .git/modules/submodule_name
  6. Commit the change
  7. If you want to keep the submodule files in your repo:
    1. rm -rf path/to/submodule/.git
    2. git add .
  8. Otherwise:
    Delete the now untracked submodule files rm -rf path/to/submodule

9. Push a particular commit

source

git push <remotename> <commit SHA>:<remotebranchname>

should do the trick. Example:

git push origin 712acff81033eddc90bb2b45e1e4cd031fefc50f:master

Note that this pushes all commits up to and including the commit you choose. If you don’t want that to happen, you should first use git rebase -i to re-order the commits.

10. Clone a particular release/version

“reset” your repository to any commit you want (e.g. 1 month ago).
source

Use git-reset for that:

git clone [remote_address_here] my_repo
cd my_repo
git reset --hard [ENTER HERE THE COMMIT HASH YOU WANT]

why would you not do a simple checkout of the wanted commit? – nemoo May 23 ’13 at 10:44
Because you will be on the “detached HEAD” state after checkout to a particular commit. – Rui CarneiroMay 23 ’13 at 16:06
 

11. Use Sublime Text 3 as core editor

Source

Sublime Text 3 (Build 3065) added the subl.exe command line helper. Use subl.exe -h for the options available to you. I have hot_exit: true and remember_open_files: true set in my Sublime Text user settings. I have found the following git config to work well for me.

git config --global core.editor "'c:/program files/sublime text 3/subl.exe' -w"

With this git config the a new tab is opened in my editor. I edit my commit message, save the tab and close it (CTRL+w). Git will wait until the tab is closed to continue its work.

12. Undo git add for a file

 git reset HEAD <file>

13. Delete the history of a repo

http://stackoverflow.com/a/26000395
If you want to delete all your commit history but keep the code in its current state, it is very safe to do it as in the following:

  1. Checkoutgit checkout --orphan latest_branch
  2. Add all the filesgit add -A
  3. Commit the changesgit commit -am "commit message"
  4. Delete the branchgit branch -D master
  5. Rename the current branch to mastergit branch -m master
  6. Finally, force update your repositorygit push -f origin master

14. Diff the same file between two different commits on the same branch

https://stackoverflow.com/a/3338145

To see the difference for a file “main.c” between now and two commits back, here are three equivalent commands:

git diff HEAD^^ HEAD main.c
git diff HEAD^^..HEAD -- main.c
git diff HEAD~2 HEAD -- main.c

15. Error when pushing to bare repository

When you get something like this:

error: failed to push some refs to 'git@github.com:519ebayproject/519ebayproject.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

The simplest solution is to force push the repo as described before:

git push origin master -f