GIT - mosinn/DOCS-n-Snippets-n-Steps GitHub Wiki

Current Branch Name

git branch --show-current

Get FILE-NAMES changed LIST , between feature AND develop

// File Names List DEVELOP vs CURRENT - WITH Line STATS
git diff --stat --color develop..feature\new

// File Names List - CURRENT vs DEVELOP
git diff --name-status develop

// File Names List - anyBranch1 vs anyBranch2Current
git diff --name-status anyBranch1..anyBranch2Current

NOTE: If list of diffs is large, terminal buffers output and stops on blinknig : => Press  SPACE to paginate and at 'END', DONT press ESC..Just press 'Q'

Git config your username and email

git config --global user.name "Sam Smith"
git config --global user.email [email protected]

Clone a remote repo

git clone <username@host:/path/to/repository>

fetch and merge remote changed

git pull

checkout a remote dev branch

git checkout develop/xxx

JUST init a blank repo for first time on local

git init

Just switch b/w branches

git checkout <branchname>

Create local branch and switch to it use -b

git checkout -b <branchname>

List (all -a) branches

git branch --list -a

Git status tells local state of working tree

git status

Git commit history log

git log --author=tim --pretty=oneline --decorate --all

Preview changes, before merging

git diff <sourcebranch> <targetbranch>

View all the merge conflicts:

git diff

Undo all local bad changes to start clean

git reset --hard origin/master

Delete a branch on your remote repository

git push origin :<branchname>

Delete local branch

git branch -d <branchname>

Git grep

git grep "foo()"

Git diff between current and last 2 commits for a given file

git diff HEAD^^ HEAD <path of file>

Git add,commit makes files ready for push

git add .

*=saferThan.dot .= nu+modified , -u=modified+deleted-nu, -A=everything:nu+modified+deleted

git commit -m "STORY123 - my message "

Push the branch to your remote repository, so others can use it

git push -u origin <branchname>

Merge changes to local feature branch, from say remote develop

..remain in your local branch where you want to have latest remote changes

git merge <remote develop branch where others have committed changes>

Create a local branch from latest dev, then set its upstream tracking so that latest changes can be pulled

git checkout <dev>

git branch -b <my local feature>

git branch --set-upstream-to=origin/develop/19.4.0 feature/<my local feature>

..keep working locally and pulling latest now

..finally push to origin, for others to see nu local branch of yours

git push -u origin <my local feature>

.. if local branch was named with temporary name till story was wip, use below when real name is known later

git push origin local-name:remote-name

.. in case we didnt clone from remote and just are inventing a new branch for POC:-

git remote add origin <remote server>

MOVING to a new branch for re-implementation while maintaining older branch untouched

Save current working tree if I want to replicate same into new branch

git stash

Switch to latest dev branch

git checkout feature/dev

Pull latest

git pull

create and checkout new branch out of latest dev, locally (-b flag if used makes it single step one)

git branch feature/my-new-local-branch

git checkout feature/my-new-local-branch

Make local branch follow remote dev

git branch --set-upstream-to=origin/dev/00.1.0 feature/my-new-local-branch

git branch --set-upstream-to=origin/rel/00.1.0 feature/my-new-local-branch

Copy back stash - might not help fully

git stash pop

Copy files from one branch to the one we are on - Works fully if we goto older commit on web git and copy all relevant file name paths and use below to transfer

git checkout feature/my-older-local-branch projName/path/...../js/.../abcUtil.js ....

Amend last commit (try only on feature branch of yours) , to change commit message or replace a whitespace file mess

..after a bad commit

.. update the file you want to change in last commit, locally

discard all other changes to keep tree clean with only missed changes dirty

git add .

git commit --amend

..update commit message in vi above opens

git push -f origin HEAD

Uncommit the latest commit NOT yet pushed to remote

git reset HEAD~1

RENAME branch - while on same branch in local

git branch -m feature/newName

RENAME branch - If you are on a different branch:

git branch -m old-name new-name

RENAME branch - Delete the old-name remote branch and push the new-name local branch

git push origin :old-name new-name

RENAME branch - Reset the upstream branch for the new-name local branch.Switch to the branch and then

git push origin -u new-name

DELETE branch

REMOTE first

git push -d <remote_name> <branchname>

LOCAL second

git branch -d <branchname>

Local Shelf using Git, and finally Squash

  • Create 'squash' branch from develop - feature/OCPS-0001-MyFeature1-Squash
  • Create 'eventual feature' branch from develop - feature/OCPS-0001-MyFeature1-Eventual
  • Locally keep working and committing freely in feature/OCPS-0001-MyFeature1-Squash
  • Finally when done:
  • git checkout feature/OCPS-0001-MyFeature1-Eventual
    
  • git merge --squash feature/OCPS-0001-MyFeature1-Squash
    
  • commit with -m and push as usual
    
  • delete the feature/OCPS-0001-MyFeature1-Squash branch
    
  • Raise PR from feature/OCPS-0001-MyFeature1-Eventual to develop
    
⚠️ **GitHub.com Fallback** ⚠️