Skip to content

Instantly share code, notes, and snippets.

@smw7156
Last active November 13, 2025 07:11
Show Gist options
  • Select an option

  • Save smw7156/0ef0f83dcc200a853ce600202a4147ab to your computer and use it in GitHub Desktop.

Select an option

Save smw7156/0ef0f83dcc200a853ce600202a4147ab to your computer and use it in GitHub Desktop.
Here are some of the command I mostly use,

Git handbook

Difference between commit and files

  • git diff --name-only 4242069 b5011b8 -> only for file changed between two commits
  • git diff b5011b8:StanceBeam/app/src/main/java/stancebeam/quicklogi/com/cricketApp/MainActivity.java 4242069:StanceBeam/app/src/main/java/stancebeam/quicklogi/com/cricketApp/MainActivity.java -> difference between two file from two commits(older commit should be placed first)

For moving any no. of last commit from master to a new branch

initial:
master A - B - C - D - E

final:
newbranch     C - D - E
                     /
master 	        A - B
Ans:-----------------------------
  1. create a new branch: git branch new-branch (imp: first create the branch before resetting)
  2. hard reset master to the last commit (commit B from abv e.g.): git reset --hard <SHA-of-commit-B>
  3. "git checkout newbranch"

See changes before pulling from remote git repository:

  1. fetch the changes from the remote -> git fetch origin
  2. show commit logs of changes -> git log master..origin/master
  3. show diffs of changes -> git diff master..origin/master
  4. apply the changes by merge.. -> git merge origin/master
  5. .. or just pull the changes -> git pull

git revert -

to undo previous commits - will create a new commit by undoing all the the changes that need to be revert (incomplete)


Undo anything in Git:

  • -> If you have done some commit or a rebase or anything that SHOULDN'T BE THERE IN THE FIRST PLACE, then
  1. type -> git reflog - list all the changes/operation made recorded in git with its head index
  2. If you are 100% SURE THAT COUPLE OF LAST COMMIT SHOULDN'T BE THERE -> git reset --hard HEAD@{n} - n: Head index you want in last
  3. Your head will now point to HEAD@{n} i.e. it is now HEAD@{0}
  4. you can carry on your work from here

Adding few of changes in a file to Commit:

  • -> If you have made some changes in a file (let say in 6 places) and you commit ONLY 3 OF THOSE CHANGES, then
  1. type -> git add --patch <filename> (or -p for short) -> for every hunk git will pop a question with options
  2. -> Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]? -> for this case we will use only 'y' and 'n'
  3. press 'y' if you want to stage this hunk, and 'n' if you don't want to commit this hunk
  4. For the part you selected 'y' will be staged for commit
  5. for rest of the option in [y,n,q,a,d,/,j,J,g,s,e,?] see: https://stackoverflow.com/a/1085191/3939451

Stashing few of changes:

  • -> If you have made some changes in a file (let say in 6 places) and want to STASH ONLY 3 OF THOSE CHANGES, then
  • follow the above as same as the commit few file β€”> just instead of add use stash

e.g. -> git stash -p -> this will ask the similar question for stashing with the same options. -> or, to save it with message -> git stash save "stash message" -p -> click 'y' for the hunk you want to stash and 'n' for the hunk you want to don't want to stash


Stashing one/two files:

  • If you need to stash only 1 or 2 files (complete changes in those files)
πŸ’‘ Note: if the file is un-tracked, you have to add it first
  • you can do it with this command
git stash -- <file_name_1> <file_name_2>
  • If you want add message to it, then
git stash push -m"you meassage here in quotes" <file_name_1> <file_name_2>

Renaming a stash

Follow this stack overFlow link


Maintaining 2 GitHub account:

    • If you have 2 accounts in github and you want to maintain or use both in the same machine. You can try using 1 ssh key for the differnt accoun,t but you will not ne able to add the same in 2 github different account, and so i ended up using 2 different ssh key for different accounts
  1. In directory /home/shadman(~)/.ssh/ create 2 ssh key for different account

  2. create file in the above folder confifg containing sets of value for differentiating the 2 ssh a. Host - normaly it's github.com but to mark anyone to differentiate it with the default one append '-' (or something like this) after it b. HostName - it should be github.com for all github related accounts c. User - usually git d. IdentityFile - ~/.ssh/<your-ssh-file> (full path for the ssh file for that account)

    e. In the same way create another list after the above for different accout- if its the default one - nop need to append anything on the 'Host'. Or else, append something

  3. Once the config file is created, start setting your repo

  4. you have to clone the repo using the ssh clone url.

  5. on your local machine, for the git repo to configure it with the respective ssh the main to set the url

  6. click git remote -v to check your url configutation setting for that repository usually url are in form of @:

  7. you have modify the part or set it acoording to the Host in the config file you created in first step

  8. Once the above step are done you are good to go

πŸ’‘ *Issue face afterward:* unable to use git operation from the other account is OS session e.g.: `ERROR: Permission to smw7156/Test3.git denied to 56-smw93.`

β†’ haven’t added smw7156 ssh key run ssh-add ~/.ssh/<yourPrivateKey> - its start working β†’ how to get whats/which keys are added in ssh


DELETING MULTIPLE BRANCH

    • If you want to delete mutliple git branches except couple of them
  • my case: I wanted to delete all the local brnaces except 'development' and 'main' follow the methods
  1. type -> git branch -D <br1> <br2> <br3> <br4> .... -> when you know the specific branches to delete,
  2. type -> git branch -D git branch --list $*`` -> to delete branches have name in some specific pattern
    1. Note: branch listing command should be in ```` beside 1 num key
  3. type -> git branch -D git branch --list | grep -v '\|'`` -> to delete all branches except br1 and br2
  • e.g. ( in Mac terminal )

    -> for i in `git branch --list 'smw/*'`; do echo $i; done
    
    smw/activity-detail-cardUI
    smw/bug-fixes
    smw/button-loader-fix
    smw/fixes-3
    smw/mix-panel-check
    smw/prod-bugs-1
    smw/some-improv
    
    -> git branch -D `git branch --list 'smw/*' | grep -v 'smw/prod-bugs-1'`
    
    Deleted branch smw/activity-detail-cardUI (was 201b0231e).
    Deleted branch smw/bug-fixes (was 6e9e75d01).
    Deleted branch smw/button-loader-fix (was 7b11c9d8d).
    Deleted branch smw/fixes-3 (was de2a9cc11).
    Deleted branch smw/mix-panel-check (was 675021966).
    Deleted branch smw/some-improv (was b7723a183).
    
    // check whats deleted and whats remaining by running the same command again
    -> for i in `git branch --list 'smw/*'`; do echo $i; done
    
    smw/prod-bugs-1
    
    Note : the above command will only delete from local to also delete from origin you can use loop in terminal
    
    -> for  brName in `git branch --list 'smw/*' | grep -v 'smw/fixes-3'`; do echo `git branch -D $brName && git push origin :$brName`; done

When creating a new Repository, and adding a existing project in it

  • created Test1 in github Test123 in local

  • init git inside Test123 i.e. Test123/.git/ -> is here [this is the correct way]

    β†’ .git should not be the sibling of Test123 directory

  • add .idea/ in .gitignore before making the first commit.

  • add remote origin url from github

    β†’ git remote add origin <url>

  • set your user.name and user.email in local config

    git config --local user.name <userName>
    git config --local user.email <user.email>
    

    β†’ ^ " not required for the user name or email

  • set the branch as master or main

    git branch -M master or you can set main also . Anything works fine

  • and push the code to origin


Logging style

β†’ git log --oneline -5 --author cbeams --before "Fri Mar 26 2009"

β†’ git log --pretty=format:"%C(yellow)%h%x09%C(green)%ad%x09%C(blue)%an%x09%C(auto)%d%s" $args


Cherrypick

  • You can pick a specific commit on one branch and copy the commit to another branch.
// Switch to branch on top of which you want to add

// will add a new commit by adding the changes of the commit
git cherry-pick <commit-hash-you-want-to-add> 

// will add the changes but not commit, it will be in local
git cherry-pick --no-commit <commit-hash-you-want-to-add> 

Listing branch with specific keyword

  • If you have a lot of branches and want to search/list with some specific keywords use this command
git branch --list '<pattern>'
git branch -l '<pattern>'

e.g. 1
git branch --list 'bug/*'
>  bug/notice-board/shadman
>  bug/propduction-bugs/shadman
>  bug/shadman/onboarding-productions

e.g. 2
git branch --list '*shadman'
>  bug/notice-board/shadman
>  bug/propduction-bugs/shadman
>  local/create-spot/shadman
>  task/local-BS-UI/shadmanfolderStructureUpdate
πŸ’‘ should be enclosed by single quotes `' '`. Without it listing will not work

Git alias

issue:

for alias : if taking input and doing something after that is not working

e.g. alias gcmp='git commit -m $1 && git push origin git branch --show-current

is not working

but other like alias gcm='git commit -m $1' is working fine

β†’ Resolution: Passing the single line parameter in git alias is not promising

  • So instead, stop using -m in git command the let it prompt with the default text editor so that you can enter there the way you want.

e.g. : alias gcmap='git commit -a && git push origin git branch --show-current'


Seeing changes(diff) in the staged file

git diff --cached   // --> for all the stage file
git diff --cached <File path and name>  //--> for a specific staged file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment