Skip to content
Notes and useful commands used while learning Git
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE
README.md

README.md

Learning Git - notes and useful commands

Problem with pulling new repository from GitHub to new project on local machine:

git pull origin master
From https://github.com/sebastianczech/Learning-Go
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

Solution

git pull origin master --allow-unrelated-histories

After adding files to stage, git diff shows nothing

git add main.go templates/chat.html ../README.md

git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   ../README.md
	modified:   main.go
	new file:   templates/chat.html

git diff

Solution

git diff --cached {filename}

Change author of several commits on local and remote repository

git rebase -i 40e8b607d395f1a8a8f9711fd3aa3d7f04361ac1 -x "git commit --amend --author='Sebastian Czech <sebaczech@gmail.com>'"

git push --force origin master

Git Push Fails - fatal: The remote end hung up unexpectedly

Solution

git config http.postBuffer 524288000
git config --global http.postBuffer 157286400

Git protocol error - expected old/new/ref, got 'shallow ...'

Solution

git fetch --unshallow

Install git auto-completion on macOS

ℹ️ install bash git completion

cd
wget https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
mv git-completion.bash .git-completion.bash
vi .bash_profile
......
if [ -f ~/.git-completion.bash ]; then . ~/.git-completion.bash
fi

Git configure user name and email

ℹ️ customizing Git configuration

git config --global user.name "Sebastian Czech"
git config --global user.email "sebaczech@gmail.com"

git config user.name
git config user.email

Copy output to clipboard from terminal

Solution for Windows

git config user.name | clip

Solution for macOS

git config user.name | pbcopy

Show last 4 git log message, print them in one line and show messages from last 15 minutes

ℹ️ show commit logs

git log -n4
git log --oneline
git log --since='{15 minutes ago}'

Commit all files from current directory without recording the changes already staged

ℹ️ record changes to the repository

The contents of these files are also staged for the next commit on top of what have been staged before.

Solution

git commit .

Use git repository browser

ℹ️ gitk

gitk
gitk --all

Lightweight tag

ℹ️ git tagging

git tag tagName
git tag tagName commitID

Show list of created tags

git tag

Annotated tag

git tag tagName commitID -a -m "simple message about annotated tag"

Show all logs from commit, which has tags

ℹ️ show commit logs

git log --oneline tagName

Show all information about commit with tag or using commit ID

ℹ️ show various types of objects

git show commitID
git show tagName

Delete tag

git tag -d tagName

Show 2 commits before current commits

ℹ️ show various types of objects

git show master~2
git show master^^

Show 4 commits before commit with tag tagName

git show tagName~4
git show tagName^^^^

Show all commits which are between commits with tags tag1 and tag2 (without commit with tag1)

git log tag1..tag2

Show all commits which are between commits with tags tag1 and tag2 (with commit with tag1)

git log tag1^..tag2

Branches - basic operations

ℹ️ list, create or delete branch

git branch NAME_OF_NEW_BRANCH
git branch
git branch -D NAME_OF_BRANCH_TO_DELETE

Changing branch

ℹ️ switch branches or restore working tree files

git checkout NAME_OF_BRANCH
git checkout TAG_NAME
git checkout COMMIT_SHA_ID
git checkout REF_NUM_EG_MASTER~4

Checkout to the previous HEAD e.g. to the previous branch

git checkout -

Create new branch and checkout

git checkout -b ANOTHER_NEW_BRANCH

Show heads (last commits in each branch) in repository

$ ls -la .git/refs/heads/
total 24
drwxr-xr-x  5 seba  staff  160  3 maj 13:42 .
drwxr-xr-x  4 seba  staff  128 26 kwi 23:48 ..
-rw-r--r--  1 seba  staff   41  3 maj 13:42 another-branch
-rw-r--r--  1 seba  staff   41 27 kwi 00:11 master
-rw-r--r--  1 seba  staff   41  3 maj 13:40 my-new-branch

$ more .git/refs/heads/my-new-branch
1ffdd27bba12c9f67028484fe43fa4634f40a55a

Show HEAD

ℹ️ Git Tools - Reset Demystified

$ more .git/HEAD

head vs. HEAD

  • HEAD - current commit, which is source for working copy
  • head - reference to last commit in each branch
  • @ - alias for HEAD

Show log message and textual diff of last commit

git show
git show HEAD
git show @

Where was my head 45 minutes ago ?

$ git show @@{'45 minutes ago'}

What was the list checked out commit ?

git show HEAD@{1}
git show @@{1}

Detached HEAD - when working copy is NOT last commit in branch

git checkout @^
git checkout ll

Exit from detached HEAD

git checkout -

Detach HEAD and create new branch

git checkout @^
git checkout -b BRANCH_FROM_DETACHED_HEAD

Get file from previous commit

git checkout @^ -- FILE_FROM_PREVIOUS_COMMIT.java

-- is optional, without it this command works, if file name is different from name of branch.

Change name of branch

git branch -m NEW_NAME_FOR_CURRENT_BRANCH

Revert last rebase

ℹ️ reset current HEAD to the specified state

git reset --hard ORIG_HEAD

Merge (for current branch get changes from other branch)

ℹ️ join two or more development histories together

git checkout BRANCH_FROM_WHICH_WE_MERGE
git merge BRANCH_NAME_WHERE_ARE_CHANGED_FILES
git branch -d BRANCH_NAME_WHERE_ARE_CHANGED_FILES
  • Fast-forward (FF) - it doesn't leave any message in history. It is possible, when base is the same as target
  • 3-way merge - commit by commit, file by file, line by line:
    • base - last common commit
    • source - branch which we merge
    • target - branch from which we merge
  • Octopus merge - we merge more than 2 branches
  • Each merge (beside FF) creates merge commit

Copy one commit from one branch to another

ℹ️ apply the changes introduced by some existing commits

git checkout -b NEW_FEATURE
git commit . -m "CHANGE 1"
git commit . -m "CHANGE 2"
git commit . -m "CHANGE 3"
git tag ONE_FILE_TO_COPY
git checkout master
git cherry-pick ONE_FILE_TO_COPY
git tag -d ONE_FILE_TO_COPY

Force to delete branch which is not fully merged

git branch -D NEW_FEATURE

Recommended commends

  • add new files to git
git add .
  • add commit with message
git commit -m
  • beautiful diff
git diff --word-diff

Merge "no fast-forward" (with special message)

git checkout master
git merge --no-ff FEATURE_BRANCH_NAME

git branch -d FEATURE_BRANCH_NAME
git log --graph --oneline

Empty commit

git commit --allow-empty
git commit --allow-empty -m "message"

Create local copy

mkdir my_clone
cd my_clone/
git clone ../my_project .

Verbose information about branch

git branch -vv

refs/remotes/origin/master

refs - reference to commits

remotes - commits on remote repositories

origin - remote repository with name origin (default name of remote repository)

master - brach master in repository origin (default name of branch in repository)

Show information about remote repository

git remote
git remote -v

git remote show origin
cat .git/config

Push changes to remote repository

git push

Bare repository (without working copy)

mkdir BARE_REPO_WTH_NAME_DOT_GIT.git
cd BARE_REPO_WTH_NAME_DOT_GIT.git
git init --bare

Add/remove remote repository

ℹ️ manage set of tracked repositories

git remote add origin PATH
git remote remove origin

Push to origin repository branch master and update information about upstream branche

ℹ️ update remote refs along with associated objects

git push origin master -u
git push -u origin master

git push
git remote show origin

Send branch with different name on remote repository

git push origin local_name_of_branch:remote_name_of_branch

Show local and local with remote branches

git branch
git branch -a

Remove remote branch

git push origin :remote_name_of_branch
git push origin --delete remote_name_of_branch

Backup all files, all branches, all tags

git push --mirror NAME_OF_REPO

Change name of remote repository

git remote rename OLD_NAME NEW_NAME

Fetch (update tracking branch)

ℹ️ download objects and refs from another repository

git fetch

Pull (fetch + merge)

ℹ️ fetch from and integrate with another repository or a local branch

git pull

Pull (fetch + rebase)

git pull --rebase

Push some tag

git push origin TAG_NAME

Push all tags

git push --tags

Push annotated tags (not lightweighted)

git push --follow-tags

git config push.followTags true

Remove tags from local and then from remote repo

git tag -d TAG_NAME
git push origin :TAG_NAME

Remove tags from local, which were removed on remote repository

git fetch --prune --prune-tags

Fetch without tags

git fetch --no-tags

Index = staging area = cache

  • working copy -> git add -> index -> git commit -> commit
  • SRP (single responsibility principle) for commits -> cohesion of commits.

Add file to index

ℹ️ add file contents to the index

git add FILENAME

Add files in interactive mode

git add -i

Show diff in working copy

ℹ️ show changes between commits, commit and working tree, etc

git diff

Show diff in index

git diff --cached

Show diff in GUI

git gui

Unstage file (remove from index)

git add -i
3: revert

or

git reset

Add part of file (hunk) to index

git add -i
5: patch

Add parts of every file to index

git add -p
git add --patch

Recover file(s)

git checkout -- FILE_NAME
git checkout BRANCH_HNAME -- FILE_NAME

git checkout -- *.FILE_EXTENSION

Reset all changes and return to HEAD

git reset --hard

Remove new files

ℹ️ remove untracked files from the working tree

git clean -i
git clean -f

Remove files, directories and files ignored by git

git clean -fdx

Clean modified and new files

git reset --hard
git clean -f

Stash files

ℹ️ stash the changes in a dirty working directory away

Modified files:

git stash

Modified and new files:

git stash -u

Show stash list

git stash list

Pop change from stash

git stash pop

Save stash with specified name

git stash save -u "STASH_NAME"

Reset changes before current commit

git reset @^

Remove file from stash, which was commited before adding to .gitignore

git rm --cached FILENAME

Ignoring files

vi .gitignore

Private .gitignore

vi .git/info/exclude 

Check if file is ignored

git check-ignore FILE_NAME

Ignore changes in file added to repository

ℹ️ register file contents in the working tree to the index

git update-index --assume-unchanged FILENAME

Show all files add to repository

ℹ️ show information about files in the index and the working tree

git ls-files

Show ignored files

git ls-files -v | grep "^h "

Stop ignoring changes in file

git update-index --no-assume-unchanged FILENAME

Git configure

ℹ️ get and set repository or global options

git config KEY VAL
git config --unset KEY
git config KEY
git config --list

Git config files

/etc/gitconfig
~/.gitconfig
.git/config

git config --system KEY
git config --global KEY
git config --local KEY

git config --edit --system
git config --edit --global
git config --edit

Configuring git in external file

git config --file .gitprojectconfig KEY VAL
git config include.path ../.gitprojectconfig
git add .gitprojectconfig
git commit -m "Shared project configuration"

Conditional including configuration files

git config --global includeif.gitdir:/home/login/dev/work ~/.gitconfig.work

Udacity git commit message style guide

Set vim as a git commit message editor

git config core.editor vim

Interactive vim tutorial

Git cheat sheet

Clone last N commits

git clone --depth N

Add remote git repository with custom name

git remote add origin URL
git remote add NAME URL

git push -u origin master
git push -u NAME URL

Abort merge

git merge --abort

Reset state of repository before merge

git reset --hard ORIG_HEAD

Use merge tool

ℹ️ run merge conflict resolution tools to resolve merge conflicts

git mergetool

Show list of tools for merge tool

git mergetool --tool-help

Configure merge tool

git config merge.tool vimdiff

Check if RERERE (Reuse recorded resolution of conflicted merges) is enabled

git config rerere.enabled

Enable RERERE

git config rerere.enabled true

Files created by RERERE

ls -la .git/rr-cache/

Revert last commit

ℹ️ revert some existing commits

git revert @

Modify last commit

git commit --amend

Commit driven development

git commit --allow-empty -m "message"
git commit . --amend

Reset changes from staging area and move HEAD

git reset

Rebase vs. merge

Merge connects 2 branches.

a - b - b - m
  \ c - c /

Rebase move commits from one branch and put it at the end of other branch.

a - b - b \
            c - c 

Rebase with branch to create linearl history

git checkout BRANCH_NAME
git rebase master 

Don't do rebase on master branch.

Interactive rebase

git rebase -i COMMIT

Rebase last 5 commits

git rebase -i @~5

Abort rebase

git rebase --abort

Repo cleaner and filter-branch

BFG Repo cleaner

Filter-branch - advanced script tool for Git

Force push with changed history

git push --force
git push --force-with-lease

Generating patches for file to find what was changed

git log -p FILENAME

Replace 1 commit by 2 commits (split 1 commit into 2)

Solution 1

git commit -m "change 1 and 2 feature"
git reset @^
git add -p
git commit -m "change 1 feature"
git add -p
git commit -m "change 2 feature"

Solution 2

git commit -m "change 1 and 2 feature"
git rebase -i @^
git reset HEAD~
git add -p
git commit -m "change 1 feature"
git add -p
git commit -m "change 2 feature"
git rebase --continue

How to work with Git everyday ?

git checkout -b BRANCH_NAME
git push origin -u BRANCH_NAME:REMOTE_BRANCH_NAME
git config push.default upstream

git add -p
git commit -m "COMMIT MESSAGE"
git push 

git rebase -i master
git push --force-with-lease

git checkout master
git pull origin master

git checkout -
git rebase -p master
git push 

git checkout master
git merge --no-ff -m "MERGE MESSAGE" BRANCH_NAME
git push origin master

git push origin :REMOTE_BRANCH_NAME
git branch -d BRANCH_NAME
git remote update origin --prune

Show only merge logs

git log --merges --oneline

Show only logs for merges from commit with tag

git log TAG_NAME..master --oneline --merges --reverse

Advanced team work

Show info about authors of last changes

ℹ️ show what revision and author last modified each line of a file

git blame FILENAME
git gui blame FILENAME

Find commits which contains text in changes (not in commit message)

ℹ️ git pickaxe search

 git log -S TEXT2FIND
 git log -S TEXT2FIND -p

Git log useful commands

git log --oneline
git log --graph
git log --pretty=oneline
git log --until '1 month ago'
git log --format=%h

Find who and when had changed something in code

ℹ️ use binary search to find the commit that introduced a bug

git bisect start
git checkout SOME_GOOD_REPO_IN_PAST
git bisect good
git bisect bad NAME_OF_BRANCH

...
git biscet good / bad 
git biscet good / bad 
git biscet good / bad 
...
git bisect run NAME_OF_SCRIPT_WHICH_RETURNS_GOOD_OR_BAD
...

git bisect reset

Storing very big files

ℹ️ git Large File Storage (LFS)

Git hooks

ℹ️ Customizing Git - Git Hooks

  • local hooks
.git/hooks/*
  • remote hooks
hooks/*

Working with projects, libraries and modules

You can’t perform that action at this time.