Huy Minh Ha

Software development, Tech and other stuff

Fri 05 September 2014

Git cheat sheet

Posted by Ha.Minh in Programming   

Git Manual

Git manual and related topics such as branching process.

TOC

Installation

Branching model

    origin/feature/update_layout
    origin/hotfix/r20130305-1.1
    origin/master
    origin/develop

Basics

    # pull rebase create nicer merge tree, but be careful with it
    git stash # stash any change
    git pull --rebase
    git stash pop # stash pop any change

    git fetch -p # Bring the repository up to date without executing merge on the current branch

    git add -u # add modified files but not new files
    git add -A # add all untracked files

    # Diff with remote
    git diff master origin/master

Tagging

    git tag -a <tagname> -m "Tag message"
    
    # delete tag on remote
    git tag --delete <tagname>
    git push origin :<tagname>

Pull from all branches

git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all

Info

Find commits where files were deleted

    git log --diff-filter=D --summary --stat

Checkout deleted file in the working tree

    git checkout <sha1>^ <file>

Search for a file in project commit history

    git log --all --full-history -- **/<filename>.*

Only show the content of a file from a specific revision

    git show <sha1>:<file>

Diff 2 files at specific revisions or branches

    git diff <revision_1>..<revision_2> -- <file>
    git diff <branch_1> <branch_2> -- <file>
    git diff <branch_2> -- <file>

Show changes on a branch that is not merged upstream

    git cherry <upstream_branch> <new_branch>
    git log <upstream_branch>..<new_branch>

Show log with changed files

    git log --name-only
    git log --name-status
    git log --stat
    git log --decorate --graph --oneline --date-order # better visual

Get latest tag in the current branch

    git describe --exact-match --abbrev=0
    git describe --abbrev=0 --tags

Get tag with messages

    git tag -l -n9

Show log graph

    git log origin master # Show log of a specific branch on remote
    git log origin master --graph --decorate # to show colorful text and graph and the branch name of each log.

    # A beautiful version
    git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

    # Show git log then exit
    git log | cat -

    # Show log of a particular hash
    git show <hash>
    git show <hash> --stat # show only oneline for each change

Get current URL

    git remote show origin

Get brief info about branches

    git branch -lvv

Finding what branch a commit came from

    git branch --contains <commit>

Finding who committed which line

    git blame <file>

Applying a range of commits

    # A should be older than B, this DOES NOT include A
    git cherry-pick A..B

    # Include A
    git cherry-pick A^..B

Exports

    # From a repository
    git checkout-index -a -f --prefix=/destination/path/

    # Export remote
    git archive --format=tar --remote=ssh://remote_server/remote_repository master | tar -xf -

TOC

Branching

Branch and Create new branch

    git checkout -b experimental

Delete unused branch

    git branch -d experimental
    git push origin --delete newfeature

Rename a local branch

    git branch -m <oldname> <newname>

TOC

Undoing

Undo a merge or pull

Check out git reset for great explanation and examples .

    git reset --hard

Undo a merge or pull inside a dirty work tree

Check out git reset for great explanation and examples .

    git reset --merge ORIG_HEAD

Revert a bad commit

    git revert <sha1>

    # Revert single file
    git checkout -- filename

    # Revert all files in current folder
    git checkout .

    # Remove all new files or folder
    git clean -df

    # Revert to a commit with a new commit
    git revert --no-commit xxxxxx..HEAD
    git commit -m "revert to xxxxxx"
    git diff HEAD xxxxxx

Checkout a deleted file into the work tree

    git checkout <sha1>^ -- <file>

Rewrite author/commiter name and email

    git filter-branch --commit-filter '
            if [ "$GIT_COMMITTER_NAME" = "Ha.Minh" ];
            then
                    GIT_COMMITTER_NAME="Ha.Minh";
                    GIT_AUTHOR_NAME="Ha.Minh";
                    GIT_COMMITTER_EMAIL="minhhh@minhhuyha.info";
                    GIT_AUTHOR_EMAIL="minhhh@minhhuyha.info";
                    git commit-tree "$@";
            else
                    git commit-tree "$@";
            fi' HEAD

TOC

Remotes

Create local branch then push to the remote (without tracking !!!)

    git checkout -b <branch_name>
    git push origin <branch_name>

Crete a new local branch by pulling a remote branch

    git pull origin <branch_name>                                 # without tracking
    git checkout --track -b <branch_name> origin/<branch_name>    # with tracking

Track a remote branch with an existing local

    git branch --set-upstream <branch_name> origin/<branch_name>

Delete remote branch

    git push origin :heads/<branch_name>

or

    git push origin :<branch_name>

Prune remote-tracking branches that are deleted from a remote repo

    git remote prune origin

Change remote URL

    git remote set-url origin http://new-example.com/repo.git

Merge upstream from fork repo

    git checkout master
    git pull https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git BRANCH_NAME

Push to upstream using specified ssh-key

    GIT_SSH_COMMAND='ssh -i ~/.ssh/yourprivatekey' git push --set-upstream origin develop

TOC

Submodules

    # Add submodule to subdirectory
    git submodule add <git@github ...> snipmate-snippets/snippets/

    # update submodule
    git submodule update --recursive

    # Update submodules

    git submodule foreach 'git checkout master && git pull origin master'

    # Update submodule's URL
    # Edit the *.gitmodules* file, then run:
    git submodule sync

    # Delete submodule
    git submodule deinit asubmodule
    git rm asubmodule

    # Note: asubmodule (no trailing slash)
    # or, if you want to leave it in your working tree
    git rm --cached asubmodule

    #Get submodule hash
    git ls-tree a9a796a [submodule_dir]

TOC

Migrate from bitbucket to github

    cd <path_to_repo>
    git remote rename origin bitbucket
    git remote add origin <new_repo_url> # add the new origin
    git push -u origin --all # pushes up the repo and its refs for the first time
    git push -u origin --tags # pushes up any tags

Rename github repo

Create a new git repo with the new name

    cd <path_to_repo>
    git remote rename origin old
    git remote add origin <new_repo_url>
    git push -u origin --all # pushes up the repo and its refs for the first time
    git push -u origin --tags # pushes up any tags

Additional resources

TOC

Brought to you by pelican_git. view original README.md


    
 
 

Comments