Progmar Marcin Załęczny

Language:

Git sample commands

To install git in Ubuntu, issue following command: sudo apt-get install git

Display git version number: git --version

The newest git sources you can find at: https://www.kernel.org/pub/software/scm/git/

Display complete git command list: git help --all

To create git repository in current directory execute command: git init It creates subdirectory .git which is managed by git and contains all project's history and meta-data.

To add a new file or modified one to a repository you must add it to the staging area first. It is achieved with: git add main.c

If you want add a few files at once (ex. test1.c, test2.c, test3.c) enter command: git add . It adds all new and modified files in current directory to the staging area and therefore ready to commit to a repository.

To see changes status for current directory (index status), type in: git status In the output in the section "Changes to be committed" next to every new file there is a text "new file:" and a text "modified:" next to every modified file. This means that all these files (new and modified) will be saved in the repository after next commit command.

To be able to commit changes to repository first you have to specify your user name and email. Do it by issueing commands below: git config --global user.email "mzaleczny@gmail.com"
git config --global user.name "Marcin Załęczny"
This data will be stored in ~/.gitconfig file; You can also set your user data by specyfying appropriate environment variables: GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL - This settings will take the priority before data in ~/.gitconfig file.

Now we are ready to commit changes: git commit -m "Adding files main.c, test1.c, test2.c, test3.c" After the -m parameter we type in short description of the commit. It allows us to get known in the future what changes are connected with the commit. If we modify any file we must add it to the staging area before next commit except when we issue following command: git commit --all It will auto-commits all modified files in the current directory.

If you would like to enter more complex description, set GIT_EDITOR environment variable to the name of text editor you would like to edit descriptions with, ex: export GIT_EDITOR=vim Add the line to ~/.profile file to have the variable set after each system reboot. From now the -m parameter along with the description is not necessary in the command line. Every commit will open the chosen editor and allow you to add more detailed description. If you are in the editor and sudden change your mind not to applying commit then you should exit the editor without saving data or by saving empty text.

If you edited some file (ex. main.c) that already exists in repository and want to commit it again in one single command then you have to specify its name in the command: git commit main.c

To display commit history: git log It displays changes starting from the newest to the oldest. It is equivalent to: git log HEAD If you want to display history starting with specified commit, then enter: git log COMMIT_HASH

To display a commits history in a specified range execute command: git log --pretty=short --abbrev-commit master~10..master~8 It displays eighth and nineth commit in master branch. First parameter --pretty=short is responsible for amount of showed data for every commit. It canbe one of: oneline, short, full. Second parameter --abbrev-commit prints shortcuts for commits hashes.

If you want to see data for one chosen commit, use option -1: git log -1 COMMIT_HASH

If you want to see changes in the commit, then add parameter -p: git log -1 -p COMMIT_HASH There is also available parameter --stat which shows modified files count for the commit and modified lines count for every file: git log -1 --pretty=short --stat COMMIT_HASH

To display details of given commit enter command: git show COMMIT_HASH COMMIT_HASH is printed in commits' history. On the other hand command below: git show entered without commit's hash will display details of the most last commit.

More compact commit history is printed by command: git show-branch --more=10 After parameter --more specify max additional commits count.

To view differences between two commits execute command: git diff COMMIT_1_HASH COMMIT_2_HASH where COMMIT_1_HASH is prior version and COMMIT_2_HASH is later version.

To remove file from repository and from directory tree execute commands: git rm test3.c
git commit -m "Usunięcie pliku test3.c"
The commands above don't remove the file history stored in repository.

The following commands rename the file: git mv test2.c test3.c
git commit -m "Zmiana nazwy pliku test2.c"

Repository can be clonned either by command (creates so called working directory): git clone git-folder-1 new-git-folder or by command (creates so called bare repository): git clone --bare git-folder-1 new-git-folder

Clonning remote repository: git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-4.01.git

Fetches from remote repository objects and connected with them metadata: git fetch

Fetches from remote repository objects and connected with them metadata and applies them to current local branch: git pull

Sends objects and connected with them metadata (local changes) to remote repository: git push

Print list of git's configuration options: git config -l

To set a value to any git's configuration option you can use following command: git config --global user.name "Marcin Załęczny"

To remove specified git's configuration option, execute: git config --unset --global user.name

Creating an alias can be done as below: git config --global alias.show-graph "log --graph --abbrev-commit --pretty=oneline" From now on the command: git show-graph will be equivalent to: git log --graph --abbrev-commit --pretty=oneline

Printing content of a file with specified hash: git cat-file -p 802992c4220de19a90767f3000a79a31b98d0df7

Finding full hash for object with specified hash prefix: git rev-parse 802992c422

Prints list of stored in repository files together with hashes of their blobs (objects with content): git ls-files -s

Creates tree object for current index: git write-tree

Prints additional details for given commit: git show --pretty=fuller

Adds a tag name v1.0 to commit with specified hash: git tag -m "Version 1.0" v1.0 COMMIT_HASH

Displays hash (sha1) for specified file (any file - the file don't have to be in repository) git hash-object filename

To remove a file existing in staging area use command: git rm --cached filename The command in contrast to "git rm filename" doesn't remove the file from working directory.

Restore a file that was removed in previous commit: git checkout HEAD -- test6.c

Restore a file that was removed two commits ago: git checkout HEAD~2 -- test6.c

You can define which files shouldn't be stored in repository by placing them in .gitignore file. You can place there also shell wildcards to match unwanted files. The .gitignore can be placed in any direcory and apply to the directory and also all its subdirectories (recursively).
Lines beginning with '#' character are comments.
Exclamation at the beginning of a line negates the rule follow it. It has priority among other rules.

Translates any commit name (here master) to appropriate hash string: git rev-parse master

Displays all branches which names begin with 'bug': git show-branch 'bug/*'

Creates new branch called branch-name that has origin in HEAD of current branch: git branch BRANCH_NAME

Creates new branch called BRANCH_NAME that has origin in specified commit (hash string or tag name): git branch BRANCH_NAME COMMIT_NAME

Prints names of all topic branches existing in repository: git branch

Prints names of all remote branches existing in repository: git branch -r

Prints names of all branches existing in repository: git branch -a

Prints last commit for all topic branches existing in repository: git show-branch

Prints last commit for all remote branches existing in repository: git show-branch -r

Prints last commit for all branches existing in repository: git show-branch -a

Prints two last commits for all topic branches existing in repository: git show-branch --more=1

Prints last commit for specified branch: git show-branch BRANCH_NAME

Switch to specified branch: git checkout BRANCH_NAME

Creates new branch in current point and switches to it: git checkout -b BRANCH_NAME

Creates new branch at specified commit and switches to it: git checkout -b COMMIT_HASH BRANCH_NAME

Removes specified branch (it can't be current branch): git branch -d BRANCH_NAME

Displays differences between working directory and staging area: git diff

Displays differences between working directory and specified commit: git diff COMMIT_HASH

Displays differences between staging area and specified commit: git diff --cached COMMIT_HASH

Displays differences between two specified commits: git diff COMMIT_HASH_1 COMMIT_HASH_2

Merging specified branch into current branch: git merge BRANCH_NAME If there are no conflicts then the merging will be done automatically.

After merging starts you can give up the process with command: git reset --hard HEAD It will restore working directory and staging area to the state just before calling "git merge".

If merging was completed by calling "git commit", then there is a possibility to discard the merging with command: git reset --hard ORIG_HEAD

If something went wrong with resolving conflicts, then you can go back to the beginning of conflicts' state: git checkout -m

Merge base between two or more branches can be obtained with command: git merge-base

Use startegy resolve (default is recursive) during merge: git merge -s resolve branch_name

Modify HEAD pointer to point to specified commit: git reset COMMIT The command above may take following options that play with staging area and working directory: git reset --soft COMMIT
git reset --mixed COMMIT
git reset --hard COMMIT
Option --soft modifies HEAD to point to specified commit, staging area and working directory stay unchanged.
Option --mixed (default) modifies HEAD to point to specified commit, modify staging area to mirror its state at the commit, working directory stays unchanged.
Option --hard modifies HEAD to point to specified commit, modify staging area to mirror its state at the commit, modify working directory to mirror its state at the commit.
The command "git reset" also stores original value of HEAD in ORIG_HEAD.

Displays ref change history in the repository: git reflog

Moves changes saved in specified commit to the current branch: git cherry-pick COMMIT

Reverting changes in specified commit: git revert COMMIT

Applies changes in the newest commit in current branch: git commit --amend

Changes base of specified branch to the last commit of the master branch: git rebase master branch_name The command can cause conflicts, that have to be resolved. After resolving conflicts you should execute: git rebase --continue At this point there is also available command: git rebase --abort to revert changes and restore repository the state just before executing "git rebase".

Storing staging area and working directory state into the stash and labeling it with specified message: git stash save "Message"

Restoring from the stash previous state of staging area and working directory: git stash pop

Removes stored state (of staging area and working directory) from the top of the stash: git stash drop

Restoring from the stash previous state of staging area and working directory without popping it from the top of stash: git stash apply

Displays the stack of stored in the stash contexts starting with the latest: git stash list

Displays changes in the staging area and working directory in the latest item in the stash: git stash show

Displays changes in the staging area and working directory (together with diffs of modified files) in the latest item in the stash: git stash show -p

Displays changes in the staging area and working directory (together with diffs of modified files) in the specified item (ex. stash@{1}) in the stash: git stash show -p position_number

Displays reference registry for the ref of HEAD (default one): git reflog show

Displays reference registry for the specified branch: git reflog branch_name

Clearing the whole reflog: git reflog expire --expire=now --all
git gc

Generates n patch files in form of email messages for last n commits: git format-patch -n

Draws text commits tree: git log --graph --pretty=oneline --abbrev-commit --all

Sends a patch to specified email: git send-email -to developers@domena.pl 0001-A.patch

Saves smtp port value to config file: git config --global sendemail.smtpserverport 465

Applies specified patch into the current repository: git am /tmp/git/patches/0002-B.patch

Applies all patches in specified directory into the current repository: git am /tmp/git/patches/*

Applies all patches in specified directory into the current repository using three way merge. It is useful when patches contain changes from a few branches of base repository: git am -3 /tmp/git/patches/*

Continues applying patches after all conflicts were successfully resolved: git am -3 --resolved

For the time of patches applying, git creates directory .git/rebase-apply/patch. If you get stuck with errors of patches applying, you can find there a reason why something went wrong.

Skips applying current patch after an error occured: git am --skip

Aborts unsuccessful "git am" and returns to the original state: git am --abort

To remove fname file from all over git repository execute: git filter-branch --tree-filter 'rm -f fname' master or git filter-branch --index-filter 'git rm --cached --ignore-umatch fname' master

Removes string "to_remove" and replaces string "rename_me_from" with "rename_me_to" in all messages in the repository: git filter-branch --msg-filter 'sed -e "/to_remove/d" -e "s/rename_me_from/rename_me_to/" master

Displays SHA1 identifiers of all commits that modified fname.c file: git rev-list master -- fname.c

Restores fname.c file from the previous place in current branch: git checkout HEAD~2:fname.c

To create new git command, save your shell script somewhere in the system PATH (ex. /usr/local/bin), name it "git-command_name" (ex. git-my-command) and make it executable (ex. 755). From now the command will be accessible to git by executing: git command_name (ex. git my-command)

Displays changes from the last three days: git whatchanged --since='three days ago' --oneline

Removes files not under version control from current working directory and from all its subdirectories: git clean

Searches git repository for all occurrences of specified pattern in commit's messages: git grep -i pattern

Searches git repository for all files in specifed directory that match given pattern: git grep -l pattern -- directory