How to check which branch we are currently working on
git branch
* denotes the currently working branch is Staging
How to create a new branch:
To create a new branch and switch to it at the same time, you can run the git checkout command with the -b switch:
$ git checkout -b iss53
Switched to a new branch "iss53"
This is shorthand for:
$ git branch iss53
$ git checkout iss53
To delete the feature branch
$ git branch -d branch_name //Ex: git branch –d feature/NEORND-3065
To delete the remote branch
$ git push -d origin <branch_name> // Ex: git push -d origin feature/NEORND-5849
How to checkout a branch
git checkout “branchname”
Example: git checkout master or git checkout feature/NEORND-3015
$ git checkout iss53
To delete the feature branch
$ git branch -d branch_name //Ex: git branch –d feature/NEORND-3065
To delete the remote branch
$ git push -d origin <branch_name> // Ex: git push -d origin feature/NEORND-5849
How to checkout a branch
git checkout “branchname”
Example: git checkout master or git checkout feature/NEORND-3015
How to clone a specific commit as branch
git checkout “sepcific commitID”
Example: git checkout 742eac86357ce5a5ccbffda671e9a1c4635799c0
Example: git checkout 742eac86357ce5a5ccbffda671e9a1c4635799c0
How can I know the location from where I cloned my git repository
git remote show origin //This will show the Git repo URL of the checked out branch as well as branches found in the repository. From there if you want you can checkout to specific branch as well. Please see the below commands:
To see all the branches
git branch -a
To switch b/w branches
git checkout –
git checkout – v/s git reset --hard
git checkout --
git checkout -- . will only reset the changes to the state of the index. So if you already have added files to the index, that’s what it will reset to. On the other hand git reset --hard will also throw away the index
To commit the changes
git commit –m “commit remarks”
(How do you add items from your Staging Area to your Local Repository? The git command git commit takes all changes in the Staging Area, wraps them together and puts them in your Local Repository. A commit is simply a checkpoint telling git to track all changes that have occurred up to this point using our last commit as a comparison. After committing, your Staging Area will be empty.)
To commit a specific file
$ git commit -m "GZip Compression static Http client code done" demand-gateway/Channel/Channel/DgNeo/AvailabilityClient.vb
To commit a specific folder or directories
$ git commit -m "Kenrnal folders only" -- ./demand-gateway/Kernal/HtmlUI ./demand-gateway/Kernal/Kernel/BusinessObjects ./demand-gateway/Kernal/Kernel/DataAccess ./demand-gateway/Kernal/Kernel/Persistency ./demand-gateway/Kernal
To commit the new files added as well as existing file changes
git commit -a
To merge the feature branch changes to master branch:
https://www.youtube.com/watch?v=qY6IooRlNGI (Git branches tutorial. How to use Git branches.)
1. First checkout to master branch by
git checkout master //this will switch the current VS code to master branch
2. Now merge the feature branch to master by
git merge “feature branch” //Eg: git merge feature/NEORND-3095
git merge is a command used to get the files from the local repository into the working directory.
To check the sync status of a branch with Git repository & its local repository
git status //Checking the Status of Your Files
Shows what git determines are differences between the working directory, index and the most recent commit. So it will show what files are staged etc. List which files are staged, unstaged, and untracked.
To check the difference b/w working tree and upstream
$ git diff origin/master
To check the difference b/w 2 commits of a file
$ git diff [commit id1] [commit id2] filepath
Eg: $ git diff 859eab3825a25b937c0f98071c69ecc8594b5b23 64154af6d9ed8e8a22f3216f5d54ad1ac038d3f8 search-connector/appsettings.hotfix.json
To show the difference b/w 2 branches
$ git diff feature/NEORND-2777 Origin/Master
To show the files changes not yet staged
$ git diff
Shows unstaged changes between your index and working directory
Ref: https://stackoverflow.com/questions/8452820/how-to-compare-the-working-tree-with-a-commit
So there are three types of diff you can ask for:
1. git diff --cached This is the difference between what is in the index and the last commit. It shows you that changes that will be in the next commit.
2. git diff This shows the difference in between the index and the working tree. These are the changes that you have made to files since you last added them to the index. This is why I wasn’t getting any results. I had no changes between the index and the working tree.
3. git diff HEAD This shows the difference between the files in the working tree and the last commit. There is a gotcha here: if you've made changes, added them to the index, and then backed out these changes in the working tree, you’ll get no results for git diff HEAD (because there is no difference) but you will get output for git diff --cached because there are still changes in the index.
To see the difference b/w branch in the working directory & remote branch after git fetch
$ git diff remotes/origin/Hotfix
This will compare the difference b/w the branch in the working directory with hotfix branch in the remote git repository after you made a git fetch which is now available in the HEAD tree in the local git repository after git fetch.
git diff FETCH_HEAD (This also serves the same need)
To see the difference of a file b/w branch in the working directory & remote branch after git fetch
$ git diff HEAD demand-gateway/HBS_XML4_OTA_Base/HBS_XML4_OTA_2005_A_SYNXIS/HotelAvail/HotelAvailRqOut.cs
To push the changes to Git repository
git push
git push is a command used to add all committed files in the local repository to the remote repository. So in the remote repository, all files and changes will be visible to anyone with access to the remote repository.
How to push local branch to repository
git push origin “branch name” //Ex: git push origin feature/NEORND-XXXX
git push -u origin “branch name”
If you are publishing a local branch for the first time on a remote, the "-u" option is helpful. It makes sure that a tracking connection between the local and the newly created remote branch is established. Creates an upstream tracking connection and is especially useful when publishing a local branch on a remote for the first time.
What is origin in git
origin is an alias on your system for a particular remote repository. It's not actually a property of that repository.
By doing
git push origin branchname
you're saying to push to the origin repository. There's no requirement to name the remote repository origin: in fact the same repository could have a different alias for another developer.
Remotes are simply an alias that store the URL of repositories. You can see what URL belongs to each remote by using
git remote -v
source: https://stackoverflow.com/questions/9529497/what-is-origin-in-git
How to abort the merge
git merge --abort
To see git logs
git log
To see the change history of a file
$ git log -- demand-gateway/Channel/Channel/DataSource/RTDSProvider.vb
How to see the changes made in a particular commit
git show idofCommit //Ex: git show ssfft3453546745gdgdgdgdg45g
How to get a copy of an older version of a file in a git repository
git cat-file -p <sha1>:./file.tex > wherever.tex
Eg: git cat-file -p e1d0818e20b92bb7b936d8913e7088a630e45d8c:push.sln >file1.txt
How to see the list of files made in a particular commit
git show --stat --name-only 64a6c560a991df7d408fb5a32327e0d19c3f5e1c
How to see the list of files and no of lines changed against each of those files made in a specific commit
git show --stat 64a6c560a991df7d408fb5a32327e0d19c3f5e1c
To revert a particular commit
git revert idofCommit
Create new commit that undoes all of the changes made in <commit>, then apply it to the current branch.
To clear git bash terminal
clear
To merge a specific commit of a branch to master
git cherry-pick idofCommit
Note:>> After this you have to push the change to repo by executing the Git push command. And you have to run this command from the master branch if you want to commit the branch change to master.
If any conflict after cherru-pick command run, then resolve the conflict file manually & then add the changes of that file by running the command.
$ git add filePathofFile/filename.cs (Eg: git add mod/assign/locallib.php)
And then git cherry-pick --continue
and then push the command as like after normal cherry-pick operation.
Abort a git cherry-pick
git cherry-pick --abort
Git Add Command for staging
It adds the file to the staging area.
git add is a command used to add a file that is in the working directory to the staging area.
$ git add filePathofFile/filename.cs (Eg: git add mod/assign/locallib.php)
git remote show origin //This will show the Git repo URL of the checked out branch as well as branches found in the repository. From there if you want you can checkout to specific branch as well. Please see the below commands:
To see all the branches
git branch -a
To switch b/w branches
git checkout –
git checkout – v/s git reset --hard
git checkout --
git checkout -- . will only reset the changes to the state of the index. So if you already have added files to the index, that’s what it will reset to. On the other hand git reset --hard will also throw away the index
To commit the changes
git commit –m “commit remarks”
(How do you add items from your Staging Area to your Local Repository? The git command git commit takes all changes in the Staging Area, wraps them together and puts them in your Local Repository. A commit is simply a checkpoint telling git to track all changes that have occurred up to this point using our last commit as a comparison. After committing, your Staging Area will be empty.)
To commit a specific file
$ git commit -m "GZip Compression static Http client code done" demand-gateway/Channel/Channel/DgNeo/AvailabilityClient.vb
To commit a specific folder or directories
$ git commit -m "Kenrnal folders only" -- ./demand-gateway/Kernal/HtmlUI ./demand-gateway/Kernal/Kernel/BusinessObjects ./demand-gateway/Kernal/Kernel/DataAccess ./demand-gateway/Kernal/Kernel/Persistency ./demand-gateway/Kernal
To commit the new files added as well as existing file changes
git commit -a
To merge the feature branch changes to master branch:
https://www.youtube.com/watch?v=qY6IooRlNGI (Git branches tutorial. How to use Git branches.)
1. First checkout to master branch by
git checkout master //this will switch the current VS code to master branch
2. Now merge the feature branch to master by
git merge “feature branch” //Eg: git merge feature/NEORND-3095
git merge is a command used to get the files from the local repository into the working directory.
To check the sync status of a branch with Git repository & its local repository
git status //Checking the Status of Your Files
Shows what git determines are differences between the working directory, index and the most recent commit. So it will show what files are staged etc. List which files are staged, unstaged, and untracked.
To check the difference b/w working tree and upstream
$ git diff origin/master
To check the difference b/w 2 commits of a file
$ git diff [commit id1] [commit id2] filepath
Eg: $ git diff 859eab3825a25b937c0f98071c69ecc8594b5b23 64154af6d9ed8e8a22f3216f5d54ad1ac038d3f8 search-connector/appsettings.hotfix.json
To show the difference b/w 2 branches
$ git diff feature/NEORND-2777 Origin/Master
To show the files changes not yet staged
$ git diff
Shows unstaged changes between your index and working directory
Ref: https://stackoverflow.com/questions/8452820/how-to-compare-the-working-tree-with-a-commit
So there are three types of diff you can ask for:
1. git diff --cached This is the difference between what is in the index and the last commit. It shows you that changes that will be in the next commit.
2. git diff This shows the difference in between the index and the working tree. These are the changes that you have made to files since you last added them to the index. This is why I wasn’t getting any results. I had no changes between the index and the working tree.
3. git diff HEAD This shows the difference between the files in the working tree and the last commit. There is a gotcha here: if you've made changes, added them to the index, and then backed out these changes in the working tree, you’ll get no results for git diff HEAD (because there is no difference) but you will get output for git diff --cached because there are still changes in the index.
To see the difference b/w branch in the working directory & remote branch after git fetch
$ git diff remotes/origin/Hotfix
This will compare the difference b/w the branch in the working directory with hotfix branch in the remote git repository after you made a git fetch which is now available in the HEAD tree in the local git repository after git fetch.
git diff FETCH_HEAD (This also serves the same need)
To see the difference of a file b/w branch in the working directory & remote branch after git fetch
$ git diff HEAD demand-gateway/HBS_XML4_OTA_Base/HBS_XML4_OTA_2005_A_SYNXIS/HotelAvail/HotelAvailRqOut.cs
To push the changes to Git repository
git push
git push is a command used to add all committed files in the local repository to the remote repository. So in the remote repository, all files and changes will be visible to anyone with access to the remote repository.
How to push local branch to repository
git push origin “branch name” //Ex: git push origin feature/NEORND-XXXX
git push -u origin “branch name”
If you are publishing a local branch for the first time on a remote, the "-u" option is helpful. It makes sure that a tracking connection between the local and the newly created remote branch is established. Creates an upstream tracking connection and is especially useful when publishing a local branch on a remote for the first time.
What is origin in git
origin is an alias on your system for a particular remote repository. It's not actually a property of that repository.
By doing
git push origin branchname
you're saying to push to the origin repository. There's no requirement to name the remote repository origin: in fact the same repository could have a different alias for another developer.
Remotes are simply an alias that store the URL of repositories. You can see what URL belongs to each remote by using
git remote -v
source: https://stackoverflow.com/questions/9529497/what-is-origin-in-git
How to abort the merge
git merge --abort
To see git logs
git log
To see the change history of a file
$ git log -- demand-gateway/Channel/Channel/DataSource/RTDSProvider.vb
How to see the changes made in a particular commit
git show idofCommit //Ex: git show ssfft3453546745gdgdgdgdg45g
How to get a copy of an older version of a file in a git repository
git cat-file -p <sha1>:./file.tex > wherever.tex
Eg: git cat-file -p e1d0818e20b92bb7b936d8913e7088a630e45d8c:push.sln >file1.txt
How to see the list of files made in a particular commit
git show --stat --name-only 64a6c560a991df7d408fb5a32327e0d19c3f5e1c
How to see the list of files and no of lines changed against each of those files made in a specific commit
git show --stat 64a6c560a991df7d408fb5a32327e0d19c3f5e1c
To revert a particular commit
git revert idofCommit
Create new commit that undoes all of the changes made in <commit>, then apply it to the current branch.
To clear git bash terminal
clear
To merge a specific commit of a branch to master
git cherry-pick idofCommit
Note:>> After this you have to push the change to repo by executing the Git push command. And you have to run this command from the master branch if you want to commit the branch change to master.
If any conflict after cherru-pick command run, then resolve the conflict file manually & then add the changes of that file by running the command.
$ git add filePathofFile/filename.cs (Eg: git add mod/assign/locallib.php)
And then git cherry-pick --continue
and then push the command as like after normal cherry-pick operation.
Abort a git cherry-pick
git cherry-pick --abort
Git Add Command for staging
It adds the file to the staging area.
git add is a command used to add a file that is in the working directory to the staging area.
$ git add filePathofFile/filename.cs (Eg: git add mod/assign/locallib.php)
Ex: $ git add demand-gateway/Channel/Channel/DgNeo/AvailabilityClient.vb
To add all the files
$ git add . (last is the dot operator not hyphen)
$ git add –all
$ git add –A (To add everything at once)
To add a specific folder or directory
$ git add demand-gateway/Kernal
To remove file from Staging area
$ git rm --cached my-file.ts
The "--cached" option indicates files in the staging area.
The –cached flag removes a file from the staging area. The files from the working directory will remain intact. This means that you’ll still have a copy of the file locally. The file will no longer exist inside your Git repository.
git stash
You made a change in a branch but not yet committed and now checkout another branch, use git stash
error: Your local changes to the following files would be overwritten by checkout:
search-connector/appsettings.dev.json
Please commit your changes or stash them before you switch branches.
Aborting
In this case if you want to keep the change but not want to commit to branch yet, then do git stash
$ git stash
At this point, you can switch branches and do work elsewhere; your changes are stored on your stack.(Ref: https://git-scm.com/book/en/v2/Git-Tools-Stashing-and-Cleaning, https://www.atlassian.com/git/tutorials/saving-changes/git-stash) and later you can git stash apply and then commitAnd if you want to revert this changes then do git clean (my understanding only the underlined lines, need to confirm it later)
· git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on. Stashing is handy if you need to quickly switch context and work on something else, but you're mid-way through a code change and aren't quite ready to commit.
· The git stash command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy
· Note that the stash is local to your Git repository; stashes are not transferred to the server when you push.
· You can reapply previously stashed changes with git stash pop
· git stash pop
git stash pop & git stash apply
throws away the (topmost, by default) stash after applying it, whereas git stash apply leaves it in the stash list for possible later reuse (or you can then git stash drop it).
This happens unless there are conflicts after git stash pop, in which case it will not remove the stash, leaving it to behave exactly like git stash apply.
Another way to look at it: git stash pop is git stash apply && git stash drop
How to show the changes made in a particular stash
First check what all are the changes found in the stash by running the command:
$ git stash list
This will show the result like this example:
stash@{0}: WIP on Staging: f78e488 Merge branch 'Staging' of http://bitbucket.ibsplc.com/scm/dgneo/search-connector into Staging
then run the below command for the changes:
To add all the files
$ git add . (last is the dot operator not hyphen)
$ git add –all
$ git add –A (To add everything at once)
To add a specific folder or directory
$ git add demand-gateway/Kernal
To remove file from Staging area
$ git rm --cached my-file.ts
The "--cached" option indicates files in the staging area.
The –cached flag removes a file from the staging area. The files from the working directory will remain intact. This means that you’ll still have a copy of the file locally. The file will no longer exist inside your Git repository.
git stash
You made a change in a branch but not yet committed and now checkout another branch, use git stash
error: Your local changes to the following files would be overwritten by checkout:
search-connector/appsettings.dev.json
Please commit your changes or stash them before you switch branches.
Aborting
In this case if you want to keep the change but not want to commit to branch yet, then do git stash
$ git stash
At this point, you can switch branches and do work elsewhere; your changes are stored on your stack.(Ref: https://git-scm.com/book/en/v2/Git-Tools-Stashing-and-Cleaning, https://www.atlassian.com/git/tutorials/saving-changes/git-stash) and later you can git stash apply and then commitAnd if you want to revert this changes then do git clean (my understanding only the underlined lines, need to confirm it later)
· git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on. Stashing is handy if you need to quickly switch context and work on something else, but you're mid-way through a code change and aren't quite ready to commit.
· The git stash command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy
· Note that the stash is local to your Git repository; stashes are not transferred to the server when you push.
· You can reapply previously stashed changes with git stash pop
· git stash pop
git stash pop & git stash apply
throws away the (topmost, by default) stash after applying it, whereas git stash apply leaves it in the stash list for possible later reuse (or you can then git stash drop it).
This happens unless there are conflicts after git stash pop, in which case it will not remove the stash, leaving it to behave exactly like git stash apply.
Another way to look at it: git stash pop is git stash apply && git stash drop
How to show the changes made in a particular stash
First check what all are the changes found in the stash by running the command:
$ git stash list
This will show the result like this example:
stash@{0}: WIP on Staging: f78e488 Merge branch 'Staging' of http://bitbucket.ibsplc.com/scm/dgneo/search-connector into Staging
then run the below command for the changes:
$ git show stash @{f78e488}
How to apply a particular stash
git stash apply stash@{0}
Ex: $ git stash apply stash@{f78e488}
To Delete a specific stash
$ git stash drop <stash_id>
How to clear all stash
git stash clear
Viewing unpushed Git commits
git log origin/master..HEAD
To pull the latest from the repository
git pull
git pull origin master
git pull origin Staging
git pull is command used to get files from the remote repository directly into the working directory. It is equivalent to a git fetch and a git merge
git fetch
git fetch is a command used to get files from the remote repository to the local repository but not into the working directory
How to undo the staged changes from a file
git reset HEAD <file>
How to undo the staged changes from a file and revert the local changes equal to svn revert
git reset HEAD <file>
git checkout <file>
Example: You are meant to reset first to unstage the file, then checkout, to revert local changes.
$ git reset foo/bar.txt
$ git checkout foo/bar.txt
Ref: https://stackoverflow.com/questions/3021161/git-cant-undo-local-changes-error-path-is-unmerged/3021184#3021184;
How to undo all the staged changes
$ git reset HEAD .
How do I delete/undo unpushed git commits
git reset --hard origin/feature/NEORND-3630
Note: This is useful when you have committed but not pushed yet. This will synch the latest in remote GIT repository. Ref: https://stackoverflow.com/questions/3197413/how-do-i-delete-unpushed-git-commits ( 2nd answer by Ashkan Sirous)
And if you want to get rid of the commit but keep the local work, then:
git reset --soft origin/<branch>
How to revert the local changes when the code is unstaged
$ git checkout -- <file>
This will checkout the file from HEAD, overwriting your change.
To discard all local changes in all files which are unstaged
$ git reset --hard
This is the most direct, DANGEROUS, and frequently used option. When passed --hard The Commit History ref pointers are updated to the specified commit. Then, the Staging Index and Working Directory are reset to match that of the specified commit. Any previously pending changes to the Staging Index and the Working Directory gets reset to match the state of the Commit Tree. This means any pending work that was hanging out in the Staging Index and Working Directory will be lost.
How to discard or remove untracked files in git
$ git clean -f -d to make sure that directories are also removed.
# Print out the list of files and directories which will be removed (dry run)
$ git clean -n -d
How to get the hash of the latest git commit
$ git log
How to create a directory from Gitbash & checkout the repository to that path
To create directory run mkdir and then below command for cloning to that path
$ git clone http://bitbucket.xyz.com/yourproject.git ./
How to move to a folder location with space in the folder name
If there is a location D:\G Neo\SSH Keys and you want to point Gitbash to this location
Then type with double quotes. Also note the seperator used must be forward slash always irrespective of space in the folder name or not. If you look at the path in Gitbash its always forward slash.
To exit from git result from a long running query result
Type as below
:q
To find the commits that have not been merged yet b/w 2 branches
$ git log feature/NEORND-3018 ^Staging --no-merges
To view the all directories in the current git bash
ls
To change the folder in Gitbash
How to apply a particular stash
git stash apply stash@{0}
Ex: $ git stash apply stash@{f78e488}
To Delete a specific stash
$ git stash drop <stash_id>
How to clear all stash
git stash clear
Viewing unpushed Git commits
git log origin/master..HEAD
To pull the latest from the repository
git pull
git pull origin master
git pull origin Staging
git pull is command used to get files from the remote repository directly into the working directory. It is equivalent to a git fetch and a git merge
git fetch
git fetch is a command used to get files from the remote repository to the local repository but not into the working directory
How to undo the staged changes from a file
git reset HEAD <file>
How to undo the staged changes from a file and revert the local changes equal to svn revert
git reset HEAD <file>
git checkout <file>
Example: You are meant to reset first to unstage the file, then checkout, to revert local changes.
$ git reset foo/bar.txt
$ git checkout foo/bar.txt
Ref: https://stackoverflow.com/questions/3021161/git-cant-undo-local-changes-error-path-is-unmerged/3021184#3021184;
How to undo all the staged changes
$ git reset HEAD .
How do I delete/undo unpushed git commits
git reset --hard origin/feature/NEORND-3630
Note: This is useful when you have committed but not pushed yet. This will synch the latest in remote GIT repository. Ref: https://stackoverflow.com/questions/3197413/how-do-i-delete-unpushed-git-commits ( 2nd answer by Ashkan Sirous)
And if you want to get rid of the commit but keep the local work, then:
git reset --soft origin/<branch>
How to revert the local changes when the code is unstaged
$ git checkout -- <file>
This will checkout the file from HEAD, overwriting your change.
To discard all local changes in all files which are unstaged
$ git reset --hard
This is the most direct, DANGEROUS, and frequently used option. When passed --hard The Commit History ref pointers are updated to the specified commit. Then, the Staging Index and Working Directory are reset to match that of the specified commit. Any previously pending changes to the Staging Index and the Working Directory gets reset to match the state of the Commit Tree. This means any pending work that was hanging out in the Staging Index and Working Directory will be lost.
How to discard or remove untracked files in git
$ git clean -f -d to make sure that directories are also removed.
# Print out the list of files and directories which will be removed (dry run)
$ git clean -n -d
How to get the hash of the latest git commit
$ git log
How to create a directory from Gitbash & checkout the repository to that path
To create directory run mkdir and then below command for cloning to that path
$ git clone http://bitbucket.xyz.com/yourproject.git ./
How to move to a folder location with space in the folder name
If there is a location D:\G Neo\SSH Keys and you want to point Gitbash to this location
Then type with double quotes. Also note the seperator used must be forward slash always irrespective of space in the folder name or not. If you look at the path in Gitbash its always forward slash.
To exit from git result from a long running query result
Type as below
:q
To find the commits that have not been merged yet b/w 2 branches
$ git log feature/NEORND-3018 ^Staging --no-merges
To view the all directories in the current git bash
ls
To change the folder in Gitbash
0 comments:
Post a Comment