Notes by Peter Galonza(Пётр Галонза)
GitHub Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Git

Commands

Removed git tags

git tag -d $(git tag -l)
git fetch
git push origin --delete $(git tag -l)
git tag -d $(git tag -l)

Edit .gitconfig

git config --global --edit
git config --global user.name
git config --global user.email

Interactively choose hunks of patch between the index and the work tree and add them to the index.

git add --patch

Author name and email

git config --global user.name "author_name"
git config --global user.email email_address

All repository settings

git config --list

Remove paths only from the index

git rm --cashed

Discard changes in file

git checkout <hash> -- file_name

Replace the tip of the current branch by creating a new commit

git commit --amend

Revert some existing commits

git revert commit_hash

Create a new branch

git checkout --branch <branch_name>
git branch <branch_name>
git branch <branch_name> <upstream_name>/<remote_branch>

Add remote repository

git remote add <upstream_name> <url>

Push in upstream and create Merge Request

git push -u <upstream_name> -o merge_request.create

Merge without checkout and push

git fetch . <src_branch>:<dst_branch>
git push --all

Remove file from all commits

git filter-branch --index-filter 'rm -f <file name> -- --all'

Delete remote brannch

git push --delete origin <remote branch>

Add file from other branch

git checkout <from branch> <file or directory>

Pull branch from another remote repository

git remote add <remote label> <url>
git pull <remote label> <remote branch name>:<new local branch name>

Create new repository

Create the folder and initialization on server

mkdir example.git
cd example.git
git init --bare --share
useradd -s /usr/bin/git-shell -d /git git

Create local repository, add remote,

git init
git remote add origin ssh://username@example.com/var/git/example.git
touch changelog
git add changelog
git commit -a -m"Initital commit"
git push origin master

Git ignore templates

Clone

Clone large svn repository

git svn clone -r1:HEAD http://my-project.googlecode.com/svn/ --authors-file=users.txt --no-metadata -s my_project
git svn fetch -r1:HEAD --authors-file=users.txt

Clone and auto checkout

git clone --branch

Сlone a specific branch

git clone --branch --single-branch

Apply range of commits

git cherry-pick <start hash of commit - 1>..<end hash commit>

Patch

git diff (-binary) <> > <name>.patch
git format-patch <>

git apply <name>.patch

Stash

Stash the changes

git stash
git stash apply
git stash --save "massage_text"

Reset

Unstage

git reset HEAD file_name

Resets the index and working tree

git reset --hard commit_hash

Does not touch the index file or the working tree at all

git reset --soft commit_hash

Resets the index but not the working tree. Default

git reset --mixed commit_hash

Resets using index

git reflog
git reset HEAD@{<index>}

Log

Show log with graph

git log --decorate --graph --branches --oneline
git log --all --decorate --graph --oneline

BFG Repo-Cleaner

Install

git clone --mirror git://example.com/some-big-repo.git

Clean the repository

java -jar bfg.jar --strip-blobs-bigger-than 100M some-big-repo.git

Replace the passwords

bfg --replace-text passwords.txt  my-repo.git

Remove the files and folders

bfg --delete-folders .git --delete-files .git  --no-blob-protection  my-repo.git

SVN to GIT

  1. Create commits authors file

    svn log --xml | grep author | sort -u | perl -pe 's/.*>(.*?)<.*/$1 = /' > users.txt
    
  2. Then allow the record to the form

    krasnovov = Krasnov Oleg Viktorovich <krasnovov@example.com>
    
  3. Cone repository

    • git svn clone http://my-project.googlecode.com/svn/ --authors-file=users.txt --no-metadata -s my_project
      
    • If repository so large and get timeout, use host with fast file system

    • For contine, execute command

      git svn fetch --authors-file=../users.txt
      
    • To clone to a specific revision use -rXXX:HEAD parameter

      git svn clone -rXXX:HEAD http://my-project.googlecode.com/svn/ --authors-file=users.txt --no-metadata -s my_project
      git svn fetch -rXXX:HEAD --authors-file=../users.txt
      
  4. Create .gitignore file

    git svn show-ignore > .gitignore
    
  5. Create backup

  6. Convert branches and tags

    for t in $(git for-each-ref --format='%(refname:short)' refs/remotes/tags); do git tag ${t/tags\//} $t && git branch -D -r $t; done
    for b in $(git for-each-ref --format='%(refname:short)' refs/remotes); do git branch $b refs/remotes/$b && git branch -D -r $b; done
    
  7. Remove tag and branch with suffix @xxx

    for p in $(git for-each-ref --format='%(refname:short)' | grep @); do git branch -D $p; done
    for p in $(git for-each-ref --format='%(refname:short)' | grep @); do git tag -d $p; done
    
  8. Push local repository to remote

    git branch -d trunk
    git remote add origin git@my-git-server:myrepository.git
    git push origin --all
    git push origin --tags
    

To synchronize the changes made in svn, restore from backup step №5 and repeat the steps.

git svn fetch --authors-file=../users.txt

GPG

Copy key to another machine

PC1

gpg —list-keys
gpg —export <Key ID> > public.key
gpg —export-secret-key <Key ID> > private.key

PC2

gpg —import public.key
gpg —import —allow-secret-key-import private.key
gpg --list-secret-keys --keyid-format=long
git config --global user.signingkey <Key ID>
git config --global commit.gpgsign true

Troubleshooting

Windows

Filename too long

git config --global core.longpaths true

Illegal instruction

git config --global core.fscache true