Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Friday, May 8, 2020

How to update a fork from the original repository

(source)
(more info)

1. [preparation] Clone your fork to have a local copy:
git clone git@github.com:YOUR-USERNAME/YOUR-FORKED-REPO.git
2. [preparation] Add remote from original repository in your forked repository:
cd into/cloned/fork-repo
git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
git fetch upstream
3. [routine] Update your (local) fork from original repo:
git pull upstream master
4. [routine] Update your fork's remote:
git push origin master

Sunday, August 11, 2019

Idea: Search for .gitconfig in parent folders until found

I manage multiple git remote accounts on a single machine and each has a different user name and user email associated with it.

Forgetting to set the name and email in a newly pulled repo has caused me trouble quite a lot.

It would be so nice, if I could just have one .gitconfig file sitting in each folder that I use to store the repos associated with a single account, and git would just read the nearest config it finds.

Friday, August 4, 2017

GitHub: How to cherry-pick within a pull-request?

Source & Credits: https://gist.github.com/ozh/cbce675ba35e0d336cff

1. Create new branch:

git checkout -b otherrepo-master master

2. Get the contents of the PR

git pull https://github.com/otherrepo/my-repo-name.git master

3. Change back to master

git checkout master

4. Now DO NOT merge the whole branch, BUT cherry-pick exactly the commits from the other branch.

The hash uniquely defines the commit - regardless of the branch it is in.
git cherry-pick abc0123

5. Check, remove the temp branch, push

git log
git branch -D otherrepo-master
git push origin master

Tuesday, August 1, 2017

GIT: useful commands

Checkout and start tracking remote branch:
git checkout --track origin/branchname
Publish and create tracking connection for new local branch:
git push -u <remote> <branch>
Revert changes made to your working copy:
git checkout .
Revert changes made to the index: (i.e., that you have added). Warning this will reset all of your unpushed commits to master!
git reset
Revert a change that you have committed:
git revert <commit 1> <commit 2>
Remove untracked files (e.g., new files, generated files):
git clean -f
Remove untracked directories (e.g., new or automatically generated directories):
git clean -fd
Remove ignored files (e.g., IDE configuration):
git clean -fX
Remove ignored directories (e.g., IDE configuration, target dir)
git clean -fdX
Rebase develop branch to master. (Develop will be checked out):
git rebase master develop
Delete branch with all its commits:
git branch -D <branch-name>
Create patch file of staged changes (ignore whitespace differences, like line endings):
git diff --cached --ignore-space-change >> patch.diff
Clean up list of remote branches
git remote prune <origin>

sources:

Friday, March 24, 2017

Changing author and committer info in Git histroy

The script can be found on the Git official help pages.

I added the -f argument to the command to force rewrite existing backups, which would otherwise fail the script run.

#!/bin/sh

git filter-branch -f --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
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

DO NOT REWRITE THE HISTORY OF YOUR GIT REPOSITORY IF IT WAS ALREADY PUSHED AND IS AVAILABLE FOR COLLABORATORS! 

Wednesday, January 18, 2017

GIT: the difference between HEAD~1 and HEAD^1

Source: Stackoverflow --> git-rev-parse manual page

^N: Nth parent of the tip of the current branch.
~N: Nth generation first parent of the tip of the current branch.
Here is an illustration, by Jon Loeliger. Both commit nodes B and C are parents of commit node A. Parent commits are ordered left-to-right.

G   H   I   J
 \ /     \ /
  D   E   F
   \  |  / \
    \ | /   |
     \|/    |
      B     C
       \   /
        \ /
         A
A =      = A^0
B = A^   = A^1     = A~1
C = A^2  = A^2
D = A^^  = A^1^1   = A~2
E = B^2  = A^^2
F = B^3  = A^^3
G = A^^^ = A^1^1^1 = A~3
H = D^2  = B^^2    = A^^^2  = A~2^2
I = F^   = B^3^    = A^^3^
J = F^2  = B^3^2   = A^^3^2
(emphasis by me)

Tuesday, January 17, 2017

Force LF line endings on Windows with GIT

The possible git settings for line endings are:

  • Checkout Windows-style, commit Unix-style
    Git will convert LF to CRLF when checking out text files. When committing text files, CRLF will be converted to LF.
    Git will not perform any conversion when checking out text files. When committing text files, CRLF will be converted to LF.
    Git will not perform any conversions when checking out or committing text files. 
    git config --global core.autocrlf true
  • Checkout as-is, commit Unix-style
    git config --global core.autocrlf input
  • Checkout as-is, commit as-is
    git config --global core.autocrlf false

Solution on Stackoverflow:

The proper way to get LF endings in Windows is to first set core.autocrlf to false:
git config --global core.autocrlf false
Now git won’t do any line ending normalization.
If you want files you check in to be normalized, do this:
Set text=auto in your .gitattributes for all files:
* text=auto
And set core.eol to lf:
git config --global core.eol lf
Now you can also switch single repos to crlf (in the working directory!) by running
git config core.eol crlf
After you have done the configuration, you might want git to normalize all the files in the repo. To do this, go to to the root of your repo and run these commands:
git rm --cached -rf .
git diff --cached --name-only -z | xargs -n 50 -0 git add -f
If you now wano also normalize the files in your working directory, run these commands:
git ls-files -z | xargs -0 rm
git checkout .

Personal opinion: 
I set core.autocrlf to input, to make sure that the repo gets LF line endings always.

Tuesday, May 24, 2016

GIT: Accidentally amended a commit that was already pushed? Here's how to fix it.

git reset HEAD^ --mixed // delete the latest local commit, but keep the working directory
git stash save // stash away working directory changes
git pull // pull the remote commit that you originally amended
git stash apply // apply the stash on it
git add -u // stage the changes
git commit // commit the changes that you originally amended to a new commit