- 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)
initial:
master A - B - C - D - E
final:
newbranch C - D - E
/
master A - B
Ans:-----------------------------
- create a new branch:
git branch new-branch(imp: first create the branch before resetting) - hard reset master to the last commit (commit B from abv e.g.):
git reset --hard <SHA-of-commit-B> - "git checkout newbranch"
- fetch the changes from the remote ->
git fetch origin - show commit logs of changes ->
git log master..origin/master - show diffs of changes ->
git diff master..origin/master - apply the changes by merge.. ->
git merge origin/master - .. or just pull the changes ->
git pull
to undo previous commits - will create a new commit by undoing all the the changes that need to be revert (incomplete)
- -> If you have done some commit or a rebase or anything that SHOULDN'T BE THERE IN THE FIRST PLACE, then
- type ->
git reflog- list all the changes/operation made recorded in git with its head index - 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 - Your head will now point to
HEAD@{n}i.e. it is nowHEAD@{0} - you can carry on your work from here
- -> If you have made some changes in a file (let say in 6 places) and you commit ONLY 3 OF THOSE CHANGES, then
- type ->
git add --patch <filename>(or -p for short) -> for every hunk git will pop a question with options - -> Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]? -> for this case we will use only 'y' and 'n'
- press 'y' if you want to stage this hunk, and 'n' if you don't want to commit this hunk
- For the part you selected 'y' will be staged for commit
- for rest of the option in [y,n,q,a,d,/,j,J,g,s,e,?] see: https://stackoverflow.com/a/1085191/3939451
- -> 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
addusestash
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
- If you need to stash only 1 or 2 files (complete changes in those files)
- 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>Follow this stack overFlow link
-
- 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
-
In directory /home/shadman(~)/.ssh/ create 2 ssh key for different account
-
create file in the above folder
confifgcontaining sets of value for differentiating the 2 ssh a. Host - normaly it'sgithub.combut to mark anyone to differentiate it with the default one append '-' (or something like this) after it b. HostName - it should begithub.comfor all github related accounts c. User - usuallygitd. 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
-
Once the config file is created, start setting your repo
-
you have to clone the repo using the ssh clone url.
-
on your local machine, for the git repo to configure it with the respective ssh the main to set the
url -
click
git remote -vto check your url configutation setting for that repository usually url are in form of @: -
you have modify the part or set it acoording to the
Hostin the config file you created in first step -
Once the above step are done you are good to go
β havenβt added smw7156 ssh key
run ssh-add ~/.ssh/<yourPrivateKey> - its start working
β how to get whats/which keys are added in ssh
-
- 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
- type ->
git branch -D <br1> <br2> <br3> <br4> ....-> when you know the specific branches to delete, - type ->
git branch -Dgit branch --list $*`` -> to delete branches have name in some specific pattern- Note: branch listing command should be in ```` beside 1 num key
- type ->
git branch -Dgit 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
-
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 masteror you can setmainalso . Anything works fine -
and push the code to origin
β 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
- 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> - 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/shadmanfolderStructureUpdatefor 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
-min 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'
git diff --cached // --> for all the stage file
git diff --cached <File path and name> //--> for a specific staged file