Skip to content

Instantly share code, notes, and snippets.

@glennzw
Last active December 15, 2015 09:29
Show Gist options
  • Save glennzw/5238803 to your computer and use it in GitHub Desktop.
Save glennzw/5238803 to your computer and use it in GitHub Desktop.
Git tips
#Setup
git config --global user.name "My Name"
git config --global user.email "[email protected]"
#Initial commit
cd my_project
git init
git commit -m "My initial commit message"
git remote add origin [email protected]:my_project.git
git push -u origin master
#Grab clone
git clone [email protected]:my_project.git
cd my_project
#Add files
git add .
#Remove files
git rm somefile
# Caution:
# `add` and `remove` are not opposites
# `git add` adds to the git *index*. `git rm` will rm the files from your hdd too
# `git status` is your friend: 'use "git reset HEAD <file>..." to unstage'
# "staging" means "adding stuff to the git index"
# it is then the contents of the index that gets committed when you do `git commit`
# use `git gui` for committing, since it allows you very fine grained control over what is staged
# from git gui you can stage a whole file/files, individual hunks (a block of contiguous changed lines), or even individual lines. *very* useful for splitting up big changes into smaller commits
git gui
#you can even go further than that with the fugitive vim addon and edit the exact stuff that will get committed
#git is flexible and wants you play around with it. it's ok, you won't lose your work... as long as you committed your work, and you don't gomessing about with the history.
# your head is always there, don't freak out if you lose it
# if you don't have the hash anymore, git reflog is your friend
#Status
git status
#List branches
git branch -a
#To check out a remote branch
git remote show origin
git remote update
git fetch
git checkout -tb review origin/remote-name-of-repo
#After fix is pushed
git pull --rebase
#Push changes
git pull --rebase
git commit
git push
#Merge
#the idea is that you merge the commits from the review branch that you want, into the master branch
#so that master is always the "latest" version
git checkout master
git merge review
#this will be seen as a new commit on the master branch
#so doing `git status` should tell you something about a merge being staged
git commit -m 'Merged review branch'
git pull --rebase
git push
#Merge trouble
#Please don't use this recipe if your situation is not the one described in the question. This recipe is for fixing a bad merge, and replaying your good commits onto a fixed merge.
# create and check out a temporary branch at the location of the bad merge
git checkout -b tmpfix <sha1-of-merge>
# remove the incorrectly added file
git rm somefile.orig
# commit the amended merge
git commit --amend
# go back to the master branch
git checkout master
# replant the master branch onto the corrected merge
git rebase tmpfix
# delete the temporary branch
git branch -d tmpfix
#'Rollback' hackery
git reflog # and look for a line that looks like the commit you did that you want to keep
git checkout -b tmp <hash>
git checkout master
git reset --hard tmp
git push --force
#Gitosis server only accessible from laptop via SSH Tunnel
$ cat ~/.ssh/config
host git.server.com
port 2222
IdentityFile=~/.ssh/id_rsa
$ cat /etc/hosts
127.0.0.1 internal.server.com
$ ssh [email protected] -L 2222:git.server.com:22 -g
$ git clone [email protected]:my_repo.git
# Because we used -g, we can connect to the repo from other machines on our LAN
me@other $ cat /etc/hosts
192.168.100.48 git.server.com
(use same git config file)
# If we want to access our code on a remote server, we do the tunnel the other way:
ssh [email protected] -R 2222:localhost:2222
# Did a commit and push with bad timestamp?
date --set="12 June 2013 13:37:37"
git commit --amend --date="$(date -R)"
git push -f
#Useful links:
http://gitlab.org/
http://www.shakthimaan.com/installs/gitosis.html
http://www.shakthimaan.com/installs/gitosis.html#_create_local_directory_and_push_to_server
#Useful tools:
tig is a nice cmd-line tool
otherwise there's the classic gitk
giggle or gitg
#
git log --pretty=format:'%Cred%h %Cgreen%ad %Cblue%aN %Creset%s' --date=iso
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment