Skip to content

Instantly share code, notes, and snippets.

@AlisterH
Last active January 19, 2024 10:50
Show Gist options
  • Save AlisterH/f229f268d4513737aabcaea785359f1c to your computer and use it in GitHub Desktop.
Save AlisterH/f229f268d4513737aabcaea785359f1c to your computer and use it in GitHub Desktop.
useful git stuff
# ignore file permission changes
git config core.filemode false
http://joaquin.windmuller.ca/2011/11/16/selectively-select-changes-to-commit-with-git-or-imma-edit-your-hunk
https://web.archive.org/web/20170630184446/http://www.naleid.com/blog/2012/01/12/how-to-use-kdiff3-as-a-3-way-merge-tool-with-mercurial-git-and-tower-app/
http://blog.wuwon.id.au/2010/09/painless-merge-conflict-resolution-in.html
https://marcin-chwedczuk.github.io/use-meld-as-git-merge-diff-tool
https://martin-thoma.com/software-versioning-cheat-sheet/
https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History
https://stackoverflow.com/questions/2428137/how-to-rebase-one-git-repository-onto-another-one
https://robots.thoughtbot.com/git-interactive-rebase-squash-amend-rewriting-history
https://stackoverflow.com/questions/11133290/which-version-of-the-git-file-will-be-finally-used-local-base-or-remote
https://gist.github.com/katylava/564416
@AlisterH
Copy link
Author

AlisterH commented Nov 29, 2018

# just get the minimum from git
# this is a bad idea if you need to rebase e.g. update your fork to match master
git clone --no-checkout --depth 1 --single-branch https://github.com/AlisterH/Quantum-GIS.git
cd Qu*
git config core.sparseCheckout true

# checkout and update your own fork
git checkout master
git remote add upstream  https://github.com/qgis/QGIS.git
git fetch upstream

git rebase upstream/master

# push the rebase to forked repo

git push -f

git stash
# create and checkout a branch
git checkout -b ticket20665
git stash pop

# updating a .git/info/sparse-checkout (to checkout and track additional files) can be achieved with
git read-tree --dry-run HEAD

git checkout master && git remote add upstream  https://github.com/qgis/QGIS.git && git fetch upstream && git rebase upstream/master

# push branch to github
git push --set-upstream origin ticket20665

# How do I simply create a patch from my latest git commit?

# In general,
git format-patch -n HEAD^

# For a single commit just
git show HEAD > some-patch0001.patch
# will give you a useable patch.

#EDIT: apply with `git apply`
# the internet says "be carefull with "git show HEAD > some-patch0001.patch", if it'S called in colored terminal it dups also color escape sequences into file.", but I haven't had a problem.

@AlisterH
Copy link
Author

AlisterH commented Jan 20, 2021

# How do I discard changes in my working copy that are not in the index?
git stash save --keep-index --include-untracked
# After that, you can drop that stash with a git stash drop command if you like

@AlisterH
Copy link
Author

AlisterH commented Jan 21, 2021

# Create a new branch and commit the current changes to it
git checkout -b new_name
git add files
git commit
git push --set-upstream origin new_name

# If you give it the wrong name, you can rename it like this:
git checkout -b new-name

@AlisterH
Copy link
Author

# Throw away changes to a file
git checkout -- [file]

@AlisterH
Copy link
Author

# diff two files
 git diff --no-index gwc.c gwc.c_changed > segfault.patch
# to apply the diff first edit it so that it isn't talking about two files; change this:
--- a/gwc.c
+++ b/gwc.c_changed
# to this:
--- a/gwc.c
+++ b/gwc.c
# then apply:
 git apply segfault.patch

@AlisterH
Copy link
Author

How to update stale patch with GIT (also known as rerolling) https://gist.github.com/miloskroulik/59493277fa0f71c5566b

@AlisterH
Copy link
Author

@AlisterH
Copy link
Author

AlisterH commented Apr 9, 2021

@AlisterH
Copy link
Author

Cherry pick most recent commit: git cherry-pick master

@AlisterH
Copy link
Author

git pull = git fetch + git merge
To get new upstream branches do git fetch --all or git pull --all

@AlisterH
Copy link
Author

https://superuser.com/questions/229290/how-to-amend-the-last-commit-to-un-add-a-file

An alternative that doesn't require index hackery, but nonetheless preserves the old commit message:

$ git reset HEAD^
$ git add <all the files you want, excluding the one you don't want>
$ git commit -C HEAD@{1}

Alternatively if you are using git gui, you just select the "Amend last commit" option, the added file appears in the "Staged" list, click on it's icon to move it to the "Unstaged" list and commit.

And other answers might be useful...

@AlisterH
Copy link
Author

AlisterH commented Dec 8, 2023

Rename master branch:

git branch -m master crap_work
git branch -m previous_master master
git push -uf origin master and git push -u origin crap_work```

@AlisterH
Copy link
Author

AlisterH commented Jan 17, 2024

By default on Windows git uses autocrlf, which makes a working folder incompatible with linux git (i.e. WSL).
To fix:

#In WSL
git rm --cached -r .
git reset --hard HEAD
git config --global core.autocrlf false
#Or
git config --local core.autocrlf false
#This is also useful
git config --local core.filemode false

Also, to reset file times to allow another diff program to work:

apt install git-restore-mtime
git restore-mtime

@AlisterH
Copy link
Author

Warning: the dos2unix command that comes packed with msysgit (as of 1.4.8) has a non-standard (IMO dangeorous and idiotic) behaviour: it does not force UNIX newlines, but instead toggles DOS <-> UNIX . To force UNIX you must use the (non standard) --d2u option.

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