Day: February 27, 2017

  • How to Compare Branch in GIT

    COMPARE BRANCH

    • Compare two branches (local) and show how much commits each branch is ahead of the other
    git rev-list --left-right --count <branch1>...<branch2>

    DIFF STATUS

    • Get differences status between local (checked-out) branch and its respective remote (tracked) branch
    git checkout <local-branch>
    git status -sb
    Example Output: ## <local-branch>...<remote-branch> [ahead 2, behind 1] 

    DIFF AHEAD revisions of local branch

    • To see differences of ahead-revisions (in local-branch)
    git diff <local-branch>...<remote-branch>^
    E.g.
    git diff master...origin/master^ 

    DIFF BEHIND revisions of REMOTE branch

    • To see differences of ahead-revisions (in local-branch)
    git diff <remote-branch>...<local-branch>^^
    E.g.
    git diff origin/master...master^^

  • How to List all Remote Branches in GIT

    • List all remote branches
    # Show only remote branches names
    git remote
    # Show remote branches names with links
    git remote -v
    • Update everything on local from remote
    git remote update
    * This command will update the local repository with ALL the content at ALL those remotes at git server. It would not show any changes to your local repository but in fact, it would update and create new remote branches in local .git folder. Now whenever we would create a local branch which is already existing at remote, it would fetch from .git folder straightaway to fetch all the contents.
    • Rename remote branch name
    git push origin :<old_name> <new_name>
    • Delete remote branch
    git push origin --delete <remote-branch-name>
    • Pull remote branch to create local
    git pull <repo-name> <remote-branch-name>:<local-branch-name>
    E.g.
    git pull origin develop:develop
    git pull origin features/test/tasks:features/test/mylocaltasks
    • Clean up stale references i.e. Remove all outdated-references from local machine’s cache (i.e. remote/* kind of heads)
    git remote prune origin
    OR
    git fetch -p

  • How to Rename Branch in GIT

    • Rename currently checked-out local branch
    git branch -m <new-branch-name>
    • Rename local branch
    git branch -m <branch_name_to_rename> <new-branch-name>
    • push new branch name to remote
    git push origin :<old_name> <new_name>
    • Rename branch locally and remotely in 3 steps
    # Rename branch locally
    git branch -m <old-branch-name> <new-branch-name>
    # Delete the old branch
    git push origin :<old-branch-name>
    # Push the new branch, set local branch to track the new remote
    git push --set-upstream origin <new-branch-name>
    OR
    git push -u origin <new-branch-name>
    • Reset the upstream branch for the new-name local branch.
      • Switch to the branch
        • git checkout <new_name>
      • Push new branch-name now
        • git push origin -u new-name

  • How to List Branches in GIT

    • List all local branches with names only
    git branch
    • List all remote branches with names only
    git branch -r
    • List all local and remote branches with names only
    git branch -a
    • List last commit on each branch
    git branch -v
    • List last commit + remote-name on each branch
    git branch -vv
    • List all branches merged to current branch
    git branch --merged
    • List all branches NOT merged to current branch
    git branch --no-merged

  • How to Delete Branch in GIT

    • Switch to some other branch first
    git checkout <some-other-branch-name>
    • Delete remote branch
    git push origin --delete <branch_name>
    • Delete local branch now (This command will fail, if there is any pending work to be committed / merged)
    git branch -d <branch-name-to-delete>
    • Delete local branch forcefully irrespective of merge status
    git branch -D <branch-name-to-delete>
    OR
    git branch --delete --force <branch-name-to-delete>

  • How to Create Branch in GIT

    • Create a blank branch (no-switching)
    git branch <branch-name>
    • Create a blank branch and switch to it
    git checkout -b <branch-name>
    • Create a branch from other branch
    git checkout -b <branch-name> <branch-name-from>

  • How to Merge in GIT

    • register modified and untracked files
    git add <files-to-add> 
    • Commit changes
    git commit -m “commit message”
    • pull changes from origin
    git pull --rebase
    • In case, merge conflict appears
    git mergetool --tool=opendiff
    • Once merge is complete, save file and complete rebasing
    git rebase --continue
    • Push finally
    git push

    resolve behind revisions

    • Checkout to local-branch
    git checkout <local-branch>
    E.g.
    git checkout master
    • See if it has now ahead/behind revisions differences status
    git branch -vv
    • Resolve behind revisions
      • Fetch all to update remote-branch in local cache
       git fetch --all 
      • Merge now remote branch to local branch (fast-forward)
       git merge <remote-branch>
       E.g.
       git merge origin/master
    • pull changes from origin
    git pull --rebase
    • In case, merge conflict appears
    git mergetool --tool=opendiff
    • Once merge is complete, save file and complete rebasing
    git rebase --continue
    • Push finally
    git push

  • How to Commit in GIT

    Commit code (make sure to add files first which need to be committed)

    git commit -m <message>

    Commit code to previous commit

    git commit --amend
    E.g.
    git commit -m 'sample commit'
    git add forgotten-file
    git commit --amend

    Undo Add

    git reset

    Undo Commit

    Undo git commit (changes get unstaged i.e. after modification, we need to re-add, re-commit)
    git reset HEAD~
    Undo git commit (changes remain staged i.e. after modification, we need to re-commit only) -- useful when we need to change only commit message
    git reset --soft HEAD~
    Above commands are like moving commit pointer to 1 back position. In case, we want to undo multiple commits, we can use like below
    git reset HEAD~3 -> * Undoes 3 commits (changes get unstaged)
    git reset --soft HEAD~3 -> * Undoes 3 commits (changes remain staged) 

  • What is Stash in GIT

    Clear stash list

    git stash clear

    Show all stashes

    git stash list

    Drop a stash

    Removes latest stash

    git stash drop 

    Removes given stash name

    git stash drop <name-of-stash-to-drop>

  • How to do Undo things done mistakenly in GIT

    Remove file added to staging for commit

    Undo git add command
    git checkout -- <filename>

    Discard Changes to added file to staging

    git checkout <filename>; i.e. checkout repository's version

    Undo last commit

    git reset --soft HEAD^
    *This command would undo commit, but would keep changes so that we can re-modify and re-commit

    Undoing GIT Merge (Not pushed yet)

    With git log check which commit is one prior the merge. Then you can reset it using:

    1. git log --oneline -n 2
    2. git reset --hard <commit_sha_previous_to_merged_one>
    OR
    git reset --hard HEAD~1