Some GIT Commands
Below are list of commands you might find useful and handy for day-to-day work.
- $ ssh git@xx.xx.xxx.xxx
To connect IP xx.xx.xxx.xxx ssh'ly - $ git config core.filemode false
To ignore the file permissions for git - $ git log
To check the git log of local branch - $ git log origin/master
To check the git log of origin master You wont see your branch on master unless its accepted/merged at origin master - $ git log origin/<new_branch_name_on_origin>
To check the git log of origin's <branch_name_on_origin> You will see your branch on master in here even though its not accepted/merged at origin master - $ git log --name-only
Gives name of files for each commit. - $ git branch -r
To list all the branches - $ git branch
To create a new branch from current branch - $ git branch
To create a new branch from source branch - $ git branch -d
To delete the specified branch from local - $ git branch -rd origin/
To delete the specified branch from remote - $ git branch -r | grep AB-2007
To find the particular branch(es) which has 'AB-2007' in its name - $ git branch -r|grep release
To find the particular branch(es) which has 'release' in its name - $ git checkout master
To get out of current branch and point to master - $ git checkout
To switch to the branch specified in command - $ git checkout -b
To create a branch from current branch and then switch to that branch - $ git checkout
To reset the file to its original state. No changes will be kept in file. - $ git add
To make the file status to staged so as to ready to commit. - There are different States of Files in git :
modified files that are changed but not added to staged. staged files that are added to git to make them ready to commit. un-tracked files that git doesn't know about it yet, mostly newly created files falls in this category. tracked files that git knows about and are changed. - $ git reset
To unstage the file so as to skip it from commit. It keeps the changes you made in File. - $ git diff
To show the difference in file from its last commit to your changed version of file. - $ git fetch
To get all the info from origin including branches. Doesn't merge the changes. Just fetches the information.
Hence code is not updated, only information of git is updated. You might want to fire git merge after fetch so as to update the code as well. - Thus, git fetch + git merge = git pull
- $ git pull origin master
To pull origin changes to local. If trying to push yesterday's changes then to ensure no conflicts occurs, try and do this first thing in morning everyday. - $ git rebase
To keep your local commits aside, pulls master from origin to make your local master up to date to its latest state and then apply your commits one by one after its latest state. Make a note, "git rebase master", for this, you need to be in the branch which you want to rebase to master. - $ git revert
It will create another commit but in reverse so as to revert your changes in particular commit. - $ git reset --hard
It will reset your local branch to its latest/last commit. - To rename git branch locally and remotely :
git branch -m old_branch new_branch To Rename branch locally git push origin :old_branch To Delete the old branch on remote git push --set-upstream origin new_branch To Push the new branch, set local branch to track the new remote - Stashing
git stash Now you want to switch branches, but you don’t want to commit what you've been working on yet; so you'll stash the changes to push a new stash onto your stack, run git stash git stash list To see which stashes you’ve stored, you can use git stash list git stash apply You can reapply the one you just stashed by using the command shown in the help output of the original stash git stash apply stash@{2} If you want to apply one of the older stashes, you can specify it by naming it, Like : git stash apply stash@{2}. If you don’t specify a stash, Git assumes the most recent stash And tries to apply it. - authorized_users OR authorized_key this file is in .ssh folder which contains a list of public keys who gets access to git.
- ctrl + R : For reverse search of commands
- To check the difference between two branches with file names only. e.g. master and restructure_master
1. Go to new branch i.e. restructure_master
2. fire a command : git diff --name-status master
You will get list of all the files that are added, deleted or modified between two branches. - To set the different colors for easy identifications in git commands, put below piece of code in . bashrc
function parse_git_branch () {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
NO_COLOR="\[\033[0m\]"PS1="$GREEN\u@\h$NO_COLOR:\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "
Note: . bashrc is a hidden file in your user folder ("/home/username") which can be seen with command "ls -la ~/ | more"
Make a note, the above steps are for Linux / Unix / Ubuntu OS.
For OSX, you need to use the same code given above but instead of . bashrc, put it in
. bash_profile file in your user home directory i.e. /Users/yourusername.For Windows however, . bashrc file won't help.
So, In order to make this possible in windows, you need to install 'posh-git'. To Do So :
-
Verify you have PowerShell 2.0 or better with $PSVersionTable.PSVersion
-
Verify execution of scripts is allowed with Get-ExecutionPolicy (should be RemoteSigned or Unrestricted). If scripts are not enabled, run PowerShell as Administrator and call Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm.
-
Verify that git can be run from PowerShell. If the command is not found, you will need to add a git alias or add %ProgramFiles%\Git\cmd to your PATH environment variable.
-
Clone the posh-git repository to your local machine.
$ git clone https://github.com/dahlbyk/posh-git.git -
From the posh-git repository directory, run .\install.ps1.
-
Enjoy!
And to uninstall the git-posh that we used above steps to install,
- Remove the folder containing the install.ps1 file.
- Edit your profile by executing “notepad $profile”, remove any lines that refer to posh-git or profile.example.ps1
- If there are no lines in this file, then check, “notepad $profile.CurrentUserAllHosts” and remove those lines mentioned in point 2 above, from this file.
-
Add new comment