Skip to content

Instantly share code, notes, and snippets.

@kranthilakum
Last active December 11, 2015 02:49
Show Gist options
  • Save kranthilakum/4533782 to your computer and use it in GitHub Desktop.
Save kranthilakum/4533782 to your computer and use it in GitHub Desktop.
Tips and Tricks on Git SCM

Clone existing repository

git clone <link to repository>

Changes in working directory

git status -s

History of changes

git log

Who committed what

git blame

References

// List references in a local repository
git show-ref
// List references in a remote repository
git ls-remote

Branching and Merging

// Create new branch
git branch <branch-name>
// List all branches
git branch
// Switch between branches
git checkout <branch-name>
// Push to branch
git push origin <branch-name>
// Merge branch b1 into branch b2
git checkout <b2>
git merge <b1>
// Delete branch on remote repository
git push <remote-name> :<branch-name>
// Delete branch on local repository
git branch -d <branch-name>

Add Tag

git tag -a <tag-name> -m <message>
git push --tags

Remove Tag

git tag -d <tag-name>
git push origin:<tag-name>

A Model of Git Commit Message by Tim Pope:

Capitalized, short (50 chars or less) summary

More detailed explanatory text, if necessary.  Wrap it to about 72
characters or so.  In some contexts, the first line is treated as the
subject of an email and the rest of the text as the body.  The blank
line separating the summary from the body is critical (unless you omit
the body entirely); tools like rebase can get confused if you run the
two together.

Write your commit message in the imperative: "Fix bug" and not "Fixed bug"
or "Fixes bug."  This convention matches up with commit messages generated
by commands like git merge and git revert.

Further paragraphs come after blank lines.

* Bullet points are okay, too
* Typically a hyphen or asterisk is used for the bullet, preceded by a
  single space, with blank lines in between, but conventions vary here
* Use a hanging indent

Check for white-space errors

git diff --check

Untrack files Specify files in .gitignore file to intentionally make git ignore files. Files already tracked by git are not affected.

Forget files that are already tracked

git rm --cached <file-name>

Split your work Use staging area to split your work into at least one commit per issue

Patching If some of the changes modify the same file try to use:

git add --patch

Nicely formatted project-commit history

git log --no-merges

Colorful commit history and differences Add the following aliases to .gitconfig:

g1 = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --
g2 = log --graph --decorate --oneline --all
df = diff --color --color-words --abbrev

Delete the last commit

git push <remote-repository> +<SHA-1 hash>^:master
OR
git reset HEAD^ --hard
git push <remote-repository> -f 

Revert

// Revert last commit
git revert HEAD
// Revert a specific commit
git revert <SHA-1 hash>

Generating SSH Keys

// Check for existing ssh keys on your computer.
cd ~/.ssh
ls
mkdir key_backup
cp id_rsa* key_backup
rm id_rsa*
// Generate a new SSH key
ssh-keygen -t rsa -C "Your-Email-Address"
// Enter passphrase
// Repeat the passphrase
// To copy the SSH key to your clipboard
clip < ~/.ssh/id_rsa.pub

Git initial configuration

git config --global user.name "Your-Name"
git config --global user.email "Your-Email-Address"
git config --global color.ui auto
git config --global diff.mnemonicprefix true

References

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