Skip to content

Instantly share code, notes, and snippets.

@grodrigo
Last active April 28, 2019 22:01
Show Gist options
  • Save grodrigo/44db64745890880424bf8c9aaf6f088e to your computer and use it in GitHub Desktop.
Save grodrigo/44db64745890880424bf8c9aaf6f088e to your computer and use it in GitHub Desktop.
git tips

Git Tips

Git Flow

git flow init # setup project to use git-flow

git flow feature start <feature_name> # creates a new feature branch called <feature_name>

git flow feature finish <feature_name> # merge feature back into develop branch

git flow release start <version> # merge develop to release

https://danielkummer.github.io/git-flow-cheatsheet/
https://skoch.github.io/Git-Workflow/

Git rebase

https://git-scm.com/book/es/v1/Ramificaciones-en-Git-Reorganizando-el-trabajo-realizado
https://es.atlassian.com/git/tutorials/rewriting-history/git-rebase

Update branch

git checkout git rebase master

Displaying commit logs

  • Minimal prettified output
git log   --oneline --graph
  • Show files modified in log messages:
git log --name-only
  • Show what changed in file in each commit
git log -p filename
  • Show evolution of a file
git log -- filename
  • Other useful log options

    • --author="..." Only show commits made by that author
    • --reverse Show commits in reverse order (Oldest commit first)
    • --after="..." Show all commits that happened after certain date
    • --before="..." Show all commits that happened before certain data
  • Display commits in one branch not present in the other.

    • Example: display commits develop branch that are not present in master
git log master..develop
  • conversely, git log develop..master shows commits in master not merged in develop.
  • This is useful to show what you're about to push to a remote:
git fetch origin
git log origin/master..HEAD
  • Show what happened in a commit
git show <commit>
  • Example: To see the commit object two revs before the latest revision
git show HEAD~2
# or git show HEAD@{2}

Undo changes

  • undo add with git reset to unstage added files
  • revert changed to edited files using git reset --hard HEAD or alternately git checkout .
  • return to a particular point in history using git reset --hard <commit-hash>. All changes made after this commit are discarded.
  • return to a particular point in history using git reset <commit-hash>. Run git add . and git commit to add the changes back to git.
  • return to a particular point in history using git reset --soft <commit-hash>. All changes made after this commit are automatically added, i.e. staged for commit, so you only need to run git commit to add them back in.
  • To revert specific files, use git checkout <commit-hash> <filename> or git checkout <branchname> <filename>

Compare changes

  • Use git diff or git difftool for a more visual diff
  • git difftool -d when used with tools like meld or winmerge which allow full directory diffs
  • To diff current changes:
git diff HEAD
  • To diff against a branch
git diff branchname
  • To view only changed filenames
git diff --stat

or

git diff --no-commit-id --name-only
  • To run git diff after a git add: git diff --staged

Common branch ops

  • Show file in another branch git show branchname:filename
  • To remove tracking branches that are not present on the remote repo
git remote prune origin
  • Remove local branches that have been merged
# list merged branches
git branch --merged
...
# Remove these branches
git branch -d ...
  • List unmerged branches git branch --no-merged

  • Show tracking branches git branch -vv

Determining a recent problem commit

  • Use git revert -n <commit>
  • Commit can be HEAD, HEAD1, HEAD2, ... to shift to previous revisions
  • This will undo a previous commit or two, allow you to look at the changes, and see which change might have caused a problem.
  • git revert without -n will automatically re-commit reverted files, prompting you to write a new commit message. The -n flag tells git to not commit, since all we want to do is look.

Selective patching/adding

  • git add -i allows interactive staging
  • git add -p allows interactive patching for added files
  • git stash -p allows interactive stashing

Stashing to a new branch

git stash
git stash branch branchname

Fix commits to the wrong branch

  • Undo last commit, but only from the repo, not the local working copy, so your latest changes are still preserved using:
git reset HEAD~ --soft
  • Now stash to a new branch:
git stash
git stash branch branchname
  • If the branch already exists:
git stash
git checkout branchname
git stash pop

Fix commit to master instead of a new branch

git branch newbranchname
git reset HEAD~n --hard # n is the number of commits to undo
git checkout newbranchname # this contains the previous commits from master

Fix commit to master instead of existing branch

git checkout your-branch-name
git cherry-pick master
git checkout master
git reset HEAD~n --hard   # n is the number of commits to undo

Commit all tracked files without staging

git commit -a 

NOTE Files not tracked will not be added

Modifying a commit

  • Fix an incorrect commit message
git commit --amend
  • Add files that should've been there in the previous commit:
git add ...
git commit --amend --no-edit # reuse previous commit msg

Fixup and autosquash to cleanup bad commits

  • If you make a new commit to fix a previous bad commit, you can use
# git add/rm/mv ...
git commit --fixup <badcommit>
git rebase -i --autosquash <badcommit>^

Search for a string in all commits

  • git log -S <whatever> --source --all

git grep - Search a git tree

  • Use git grep instead of grep -R to search a git tree.
  • To show function names containing a pattern, use -p
  • Eg: git grep -p basename *.sh
  • Use --break and --heading to make the output more readable
  • To search for more than one pattern: -e pattern1 --and -e pattern2
  • To search for more than one pattern ('--or' implied): -e pattern1 -e pattern2
  • To negate a pattern: --not -e pattern1

git push syntax

  • git push [remote-name] [branch-or-tag-to-push]
  • Eg: git push origin master
  • Eg: git push origin v1.0.2

Show remote info

  • git remote show <remotename>
  • Eg: git remote show origin

Show tag info

git show <tagname>

Branches

  • Checkout branch and track remote git checkout -b feature/project --track origin/feature/project

  • Show current branch name git rev-parse --abbrev-ref HEAD

  • Show all local branches with your starred: git branch

  • Are we up to date with remote git status -uno

  • Add submodule git submodule add <PATH>

  • Create and push branch

git checkout -b develop
git push -u origin develop

Others

  • Pull a repo git pull --recurse-submodules --no-commit --rebase

  • Recursive update of submodules git submodule update --init --recursive

  • Delete a tag from origin. git tag -d 0.1.3 git push origin :refs/tags/0.1.3

  • Replace entire folder with one from another branch. git rm -r /path/to/folder git checkout OTHER_BRANCH -- /path/to/folder

  • Move a submodule git mv old/submod new/submod

  • List submodules grep path .gitmodules | sed 's/.*= //'

  • Show contents of a stash git stash show -p

  • Show file from commit/branch git show 8bbd06c:app/models/user.rb

git fetch git branch --all

Remove deleted branches

for branch in $(git branch -vv | grep ‘: gone]’ | awk ‘{print $1}’); do git branch -D “$branch”; done

  • Change branch parent git rebase --onto

creating a zip from a git archive

git archive HEAD --format=zip > archive.zip

List all modified file names

git whatchanged --since '12/01/2016' --oneline --name-only --pretty=format: | sort | uniq
git log --since="4 day ago" --name-only --pretty=format: | sort -u

Get a list of git branches, ordered by most recent commit

git branch -av

remove local untracked files from the current Git branch

f files, d directories, X ignored, x ignored and non-ignored

git clean -f
git clean -fd
git clean -fx

creating a zip from a git archive

git archive HEAD --format=zip > archive.zip

Set up Username/id

git config --global user.email "your@email.id"```

Set up core editor

    for Atom, use ```git config --global core.editor "atom --wait"```
    for SublimeText use ```git config --global core.editor "/path/to/subl --wait --new-window"```
    for VSCode use ```git config --global core.editor "/path/to/code --wait"```

```git config --global diff.tool meld ```#used by git difftool, use vimdiff if meld is not available

## crlf policy
###Linux:
```git config --global core.autocrlf input```
### Windows
```git config --global core.autocrlf true```

Long filenames on Windows
  To overcome the 260 char path limit on Windows
```git config --system core.longpaths true```
or alternately
```git config --global core.longpaths true```

## Alias
```git config --global alias.ci commit```
```git config --global alias.st "status --short"```

## Display commits in one branch not present in the other.
Example: display commits develop branch that are not present in master
```git log master..develop```
    conversely, git log develop..master shows commits in master not merged in develop.

## Show what happened in a commit
```git show <commit>```


Note

git config --global modifies $HOME/.gitconfig

git config --system modifies the systemwide /etc/gitconfig

git config --file <filename> creates/modifies the named config file

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