Most useful GIT commands.

$ git
Shows all available commands.

$ gitk
$ gitk --all
Graphical tool, helps with code review. Shows revision tree with all necessary informations about commits, tags etc.

$ git init
Initialize git repository locally.


$ git add --help
$ git init --help
Manual for given command.

$ git add -h
$ git init -h
Short manual for given command.


$ git config --global ""
$ git config --global "tmorcinek"
Setting up global configuration (required by some git servers).

$ git config --global core.editor "notepad++.exe -multiInst -noPlugins -nosession -notabbar"
To make git more usable from console we can change global editor (is used when committing), default on is vi, i wasn’t very familiar with it so I changed it to notepad++ (on Windows).
$ git config --global core.editor "open -W -n"
Version for MAC.


$ git clone ssh://tmorcinek@some.address:9022/test-repo.git
Clone repository from remote server to folder with default repository name.
$ git clone ssh://tmorcinek@some.address:9022/test-repo.git my-repo-folder
Clone repository to given name.
$ git clone c:\repositories\git-first-repo
Clone repository from local location.


$ git status
$ git status -s
Show repository status (this is the most common command used by me).

$ git add .
$ git add src/
$ git add src/*/**.java
What is staging: the staging area
First command add to staging area all modified files. All the rest are just adding with regular expression.
$ git add -u
Staging all deleted files. Because normal stage would not include deleted files.

$ git reset .
$ git reset src/
$ git reset src/*/**.java
Unstege files.

$ git reset --hard
Reset to last commit, by removing all non committed changes.

$ git checkout -- .
$ git checkout -- src/
$ git checkout -- *.java
Remove all the unstaged changes.


$ git commit
Commit all staged files.
$ git commit -m "Commit message"
Commit all staged files with message.

$ git commit --amend
Modify previous commit. After this command you will see the editor, so you can change the commit message.


$ git branch
List all branches
$ git branch -a
List all branches (including remote)

$ git branch branch_name
Create new branch
$ git branch -d branch_name
Delete existing branch.


$ git checkout branch_name
Switch/Checkout to remote branch or tag (run git branch -a to see the result)
$ git checkout origin/branch_name
Switch/Checkout to remote branch or tag (run git branch -a to see the result)
$ git checkout -b local_branch_name origin/branch_name
Switch/Checkout to remote branch with given name (you are getting remote branch in local repository under local_branch_name, run git branch -a to see the result.)


$ git merge branch_name
Merge with branch: branch_name, if you are on different one.


$ git rebase branch_name
Works similarly to merge but, puts all commits, on the top of the branch_name.


When you want to quickly switch to different branch and do some work, and you have uncommited changes in current. You can stash all changes from current branch (save to cache), and apply it later (even to different branch). You can even stash few portions of changes.

$ git stash save "save message"
Save uncommited changes to stash.

$ git stash apply
Applay the latest stashed item.
$ git stash pop
Applay and drop (pop) the latest stashed item.
$ git stash clear
Remove all items from stash.
$ git stash list
List of already stashed changes
$ git stash drop
Drop the latest stashed item.
$ git stash drop stash@{1}
Drop specific stashed item.


$ git tag
List of tags.
$ git tag -l "v1.2.*"
Advanced tag finding
$ git tag -a v1.2.1 -m "Release 1.2.1"
Creating tag with message, to the revision we are on.
$ git tag -a v1.2 098ak02
Creating tag with message, to some revision before in the tree.
$ git push --tags
Pushing tags.
$ git tag -d v1.4.18
Removing local tag.
$ git push origin :refs/tags/v1.4.18
Remove remote tag.

Modify/Amend specific commit

$ git rebase b66ce189 --interactive
$ git rebase b66ce189 -i
First you need to rebase with interactive mode to specific commit. (NOTE: you can only run it if you do not have any changes in repo, staged or unstaged)
After this command you will get window with default editor. You need to change ‚pick’ to ‚edit’ in the line which commit you want to modify.
Then you can make all changes you wanted, with picked commit.
$ git rebase --continue
With continue you go to the next commit you want to change.
WARNING: Do not amend already pushed commit. It is possible but Linus does not recommend it, because it can cause problems for other people in the team. You can read about it here.


$ git format-patch 16a7833ab13ccc24cfa2 --stdout > patch_name.patch
You can create a patch with git format-patch command.
Creates patch with difference between the commit that you are already in, and specified commit.
$ git format-patch master --stdout > patches\patch_name.patch
Creates patch with difference between the commit that you are already in, and master branch.
$ git am patches\patch_name.patch
Applying patch created with git format-patch command.
You can use git am with several different parameters.

Recovering lost commit

There is a chance that you commit something while you are not in any branch:
no branch
Or after commit you will hard reset branch to some revision.
In that situations, you may have difficulties to find your commit.
Even if you amend a commit the old one is still in repository and can be found.
And there is a way to find any commit.

$ git reflog
The reflog command will give you history of actions on the head of your branches.
After this command you will see the commit’s revision id and you can easily checkout or reset to it.
$ git reset --hard origin/master
$ git reset --hard "@{5 minutes ago}"
$ git reset --hard "@{yesterday}"
Hard reset could be helpful in such cases, you want to go to previous state of the repository.
This works only on commit level.

Jedna uwaga do wpisu “Most useful GIT commands.


Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:


Komentujesz korzystając z konta Wyloguj /  Zmień )

Zdjęcie na Google+

Komentujesz korzystając z konta Google+. Wyloguj /  Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj /  Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj /  Zmień )


Connecting to %s