Skip to content

Instantly share code, notes, and snippets.

@IsaEs IsaEs/git-commands.md
Last active Mar 6, 2019

Embed
What would you like to do?
Some Useful Git Commands

Merging Git Repositories

Start creating new project folder

mkdir new_parent_project
cd new_parent_project
git init

Initial commit.

 touch README.md
 git add README.md
 git commit -am "initial commit"

Merge project projectA into subdirectory projectA

  git remote add -f projectA {git_url,ssh}
  git merge -s ours --no-commit projectA/V6-006
  git read-tree --prefix=projectA/ -u projectA/V6-006
  git commit -m "merging projectA into subdir: projectA"

Merge project projectB into subdirectory projectB

 git remote add -f projectA {git_url,ssh} {git_url,ssh}
 git merge -s ours --no-commit projectB/V6-006
 git read-tree --prefix=projectB/ -u projectB/V6-006
 git commit -m "merging projectB into subdir: projectB"

Renaming branch

Rename branch locally

   git branch -m old_branch new_branch       

Delete branch

   git push origin :branch_name  

Push the new branch, set local branch to track the new remote

  git push --set-upstream origin new_branch  

Download only one folder/file from public projects with SVN

svn export https://{gitlab,github,vs}.com/{user,org}/{repository_name}.git/trunk/{folder/filer_or_name}

Change old commits user name or email

git change-commits GIT_AUTHOR_NAME {old-name} {new-name} <br> 
git change-commits GIT_AUTHOR_EMAIL {old@email.com} {new@email.com}
//just from n commit from last
git change-commits GIT_AUTHOR_NAME {old-name} {new-name} HEAD~{n}..HEAD

Remove folder and it's content from git history

git filter-branch --tree-filter 'rm -rf {folder_name}' --prune-empty HEAD
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d

Change email from old commits

If you get and A previous backup already exists in refs/original/ add -f after filter-branch

git filter-branch --env-filter '
OLD_EMAIL="isa@nitro.io"
CORRECT_NAME="IsaEs"
CORRECT_EMAIL="merhaba@isaes.com.tr"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

### Split subfolder out into new repository
```console
git filter-branch --prune-empty --subdirectory-filter {folder_name} {branch_name} 
git remote -v
git remote set-url origin {new_repo_url}

Some other stuff

Rename local branch

git branch -m {branch_name} {new_branch_name}

Revert to first commit

git update-ref -d HEAD

Revert back to commits

 git reset --soft HEAD@{count}

Unstage file

git reset {filename}

Create Branch and set upstream

git branch {name} -u

Delete Branch from upstream

git push {upstream_name} :{branch_name}

Unset upstream

git branch --unset-upstream

Set upstream

 git branch --set-upstream-to={upstream-branchname} {branch}

Delete file from Githistory

 git filter-branch -f --index-filter     'git rm --cached --ignore-unmatch {filename}'     --tag-name-filter cat -- --all

Add Staged changes to the previous commit

git commit --amend --no-edit

Interactive Rebase

 git rebase -i master 

Git Log %h = abbreviated commit hash %x09 = tab (character for code 9) %an = author name %ae = author email %ad = author date (format respects --date= option) %s = subject

 git log --pretty=format:"%h%x09%an%x09%ad%x09%s"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.