Skip to content

Instantly share code, notes, and snippets.

@riansanderson
Last active October 29, 2018 20:32
Show Gist options
  • Save riansanderson/5924942 to your computer and use it in GitHub Desktop.
Save riansanderson/5924942 to your computer and use it in GitHub Desktop.
cheatsheat of really useful things I don't need to do very often or have stupid syntax
# alias for quickly getting the remote URL for the current repo, very useful to pipe to pbcopy
git config --global alias.url '!git remote -v | awk "{print \$2}" | head -1'
# use local copy of file for all merge conflicts (sub --theirs to take remote copies)
git status -s | cut -f2 -d' ' | xargs git checkout --ours
# create and apply an email ready patch
git format-patch <commit before the series of commits you want>
<get patch onto the other machine>
git am -s 0001.patch
# create a patch you can use across different repos using the same source code (ugly but sometimes useful)
git diff --no-prefix > patchfile
<then copy the file over>
patch -p0 < patchfile
# add all modified files that are currently tracked (much like git commit -a)
git add -u
# log oneline packed with info. useful when cherry picking
git log --pretty=format:'%C(yellow)%h %Creset%ad %Cred%an%Cgreen%d %Creset%s' --date=short
# use your mergetool to manually combine two branches
git difftool --tool=bc3 HEAD...feature-something
# Your branch and 'origin/release_2.5' have diverged,and have 6 and 13 different commits each, respectively.
git reset --hard origin/release_2.5
# rename a local branch
git branch -m <oldname> <newname>
# cherry-pick like action for just one file out of a commit
# its easy to get the order mixed up so do a diff without the patch first!
git diff release_2_2..master src/Algorithm.c | patch -p1
# search for a string across all branches
git grep "string/regexp" $(git rev-list --all)
# what commits has this person made across all branches in the last 2 months?
git log --oneline --decorate --all --since=2.months.ago --author=somebody
# what branch is this commit on?
git branch -a --contains <hash>
# when did the line with this text get deleted?
git log -p --cc -S'search-string' path/to/file.c
# Your branch and 'origin/master' have diverged: figure out what commints are on
git cherry-pick --left-right --oneline master...origin/master
# someone put a whole bunch of related changes on master that all need to be reverted
git checkout -b verify-reverts
git log --oneline --author=somebody 925f283...master | awk '{print $1}' | tac | awk '{system "git revert --no-edit " $1}'
# create a new repository and replicate it on a server somewhere
# thanks http://crashingdaily.wordpress.com/2009/09/02/initing-a-new-remote-git-repository-with-existing-files/
[11:44 20090902 crashing@server ~]
$ mkdir -p /var/local/git/repos/CrashTesting
Intialize the empty repository
[11:44 20090902 crashing@server /var/local/git/repos/CrashTesting]
$ git --bare init
Initialized empty Git repository in /var/local/git/repos/CrashTesting/
Now I have the necessary repository components.
[11:44 20090902 crashing@server /var/local/git/repos/CrashTesting]
$ ls
branches config description HEAD hooks info objects refs
On my desktop, in the existing directory of files, init the directory
[11:30 20090902 crashing@desktop ~/Desktop/testws]
$ git init
Initialized empty Git repository in /Users/crashing/Desktop/testws/.git/
This created a .git directory with git control files.
Next, I tell my local desktop repository about the remote repo. The remote repo is given the short name origin.
[11:46 20090902 crashing@desktop ~/Desktop/testws]
$ git remote add origin ssh://server.crashingdaily.com/var/local/git/repos/CrashTesting
Then, I place my local files under version control.
[11:42 20090902 crashing@desktop ~/Desktop/testws]
$ git add .
Now I can commit the local files to the local repository.
[11:45 20090902 crashing@desktop ~/Desktop/testws]
$ git commit -a -m 'initialize repo'
[master (root-commit) 7871087] initialize repo
23 files changed, 500 insertions(+), 0 deletions(-)
create mode 100755 build.properties
create mode 100755 build.xml
Finally, push the master branch to the remote repository named origin.
[11:46 20090902 crashing@desktop ~/Desktop/testws]
$ git push origin master
#change the remote origin URL
git remote set-url origin git://new.url.here
# migrate remote from one server to another
## on new server
cd /opt/git_repos
sudo mkdir new_repo
sudo chmod g+rwx new_repo
sudo chgrp dev new_repo
cd new_repo
git init --bare
## on laptop, co all branches and push to new origin
git fetch -a
for remote in `git branch -r | grep -v master `; do git checkout --track $remote ; done
git remote rename origin old_origin
git remote add origin ssh://somehost/opt/git_repos/new_repo
git push --all
#push an amended commit to origin (only if you *know for sure* no-one else has pulled it down
git commit --amend
git push --force origin master
# color git status and friends in the terminal (this should be in a config file)
git config --global color.ui auto
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment