Search This Blog

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

Migrate git repository to GitLab using the command line

  • Create a new EMPTY repository in GitLab. (Note: You MUST uncheck "Initialize repository with README")
  • Clone from the source remote repository (from e.g. BitBucket, Github):
    git clone --mirror git@bitbucket.org:path/to/REPO.git
  • Push the local repository to GitLab:
    cd REPO.git
    git push --mirror git@gitlab.com:path/to/REPO.git



see also

Git: disable autocrlf end-of-line conversion

When adding files to git, git add --all, you may get the error:
fatal: CRLF would be replaced by LF in ... ... 



You can turn off the autocrlf to fix this:
git config --global core.autocrlf false



see also

Git end-of-line conversion settings

Enable end-of-line conversion

git config --global core.autocrlf input
  • Make sure your IDE (Text) Editors are unix/dos EOL aware.
  • Global git config:
    git config --global core.autocrlf input
    git config --global core.safecrlf true
  • Create .gitattributes file at the root the git repository, and add configuration like below:
    *       text=auto
    *.java      text
    *.xml       text
    *.sh        text eol=lf
    *.cmd       text eol=crlf
    *.bat       text eol=crlf
    *.jpg       -text
    *.png       -text
    *.gif       -text
  • Normalise the EOL for all the files in the repository:
    git add --renormalize .
    git status        # Show files that will be normalized
    git commit -m "Introduce end-of-line normalization"



Disable end-of-line conversion

git config --global core.autocrlf false



See also

Configure Git-Bash for Windows Terminal

  • Open Settings tab with Ctrl+,, the click Gear icon to Open settings.json file
  • Add/Edit Git Bash profile:
    {
        "guid": "{2ece5bfe-50ed-5f3a-ab87-5cd4baafed2b}",
        "hidden": false,
        "name": "Git Bash",
        "source": "Git",
        "fontFace" : "Consolas",
        "fontSize" : 11,
        "startingDirectory" : "%USERPROFILE%",
        "acrylicOpacity" : 0.75,
        "closeOnExit" : true,
        "colorScheme" : "Campbell",
        "cursorColor" : "#FFFFFF",
        "cursorShape" : "bar",
        "historySize" : 9001,
        "padding" : "0, 0, 0, 0",
        "snapOnInput" : true,
        "useAcrylic" : true
    }



see also

git not working after Mac OS upgrade

The problem: after upgrade to Catalina, got the error below when executing git command:
$ git push
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun






To fix it:
xcode-select --install

SSH access to Github from multiple accounts

  • Generate SSH Keys:
    ssh-keygen  -t rsa -b 4096 -C "email@personal.com" -f ~/.ssh/id_rsa_personal
    ssh-keygen  -t rsa -b 4096 -C "email@work.com" -f ~/.ssh/id_rsa_work
    
    The two commands above generate two key pairs: id_rsa_personal/id_rsa_personal.pub, id_rsa_work/id_rsa_work.pub. 
  • Add SSH Keys to your github accounts.
    • Add id_rsa_personal.pub to your personal github account:
      • Login to your github
      • Go to Settings -> Add SSH Keys
      • Copy of the content of id_rsa_personal.pub to the key field
      • Click Add Key
    • Add id_rsa_work.pub to your github account for work:
      • Login to your github
      • Go to Settings -> Add SSH Keys
      • Copy of the content of id_rsa_work.pub to the key field
      • Click Add Key
  • Create a file: ~/.ssh/config and add:
    • Host personal
         HostName github.com
         User git
         IdentityFile ~/.ssh/id_rsa_personal
      
      Host work
         HostName github.com
         User git
         IdentityFile ~/.ssh/id_rsa_work
      
  • Update stored identities:
    • ssh-add -D
      ssh-add id_rsa_personal
      ssh-add id_rsa_work
      
  • Test
    • ssh -T personal
      Hi githubPersonal! You've successfully authenticated, but GitHub does not provide shell access.
      $ ssh -T work
      Hi githubWork! You've successfully authenticated, but GitHub does not provide shell access.
      

Github: Use Access Token to authenticate over HTTPS

  • Login to Github
  • If you have not enabled Two-factor authentication, you need to enable it:
    • Go to "Settings" -> "Security"
  • Generate Personal Access Tokens:
    • Go to "Settings" -> "Personal Access Tokens"
  • Click "Generate new token" at the top right corner.
  • Remember/Copy the token.
  • Insert the token to your repository url:
    https://<token>@github.com/owner/repo.git
  • Now you can push to the repository without typing username & password.

Github: fork a repository and sync the fork

  1. Fork a repository
    • Clone the repository:
      git clone https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git
    • Add the original repository as upstream:
      git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git
    • Check the status:
      git remote -v
      You will see:
      # origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
      # origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
      # upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git (fetch)
      # upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git (push)
      
  2. Sync the fork with upstream(original):
    • Fetch the branches and their respective commits from the upstream repository:
      git fetch upstream
    • Check out your fork's local master branch:
      git checkout master
    • Merge the changes from upstream/master into your local master branch. This brings your fork's master branch into sync with the upstream repository, without losing your local changes.
      git merge upstream/master
    • Push local repository:
      git push --tags

git: ignore .DS_Store files globally








git config --global core.excludesfile ~/.gitignore
echo ".DS_Store" >> ~/.gitignore







git: revert back to a specific version

  • Command 1: Revert back to previous version(Note: substitute to your commit hash.):
    git reset --hard 76f134a2692c9472af3227d2f9fdb50548de6c0c
    git clean -f # clean up local repository
    git push -f # push back to central repository
  • Command 2: The following command reverts everything from the HEAD back to the commit hash:
    git revert --no-commit 76f134a2692c9472af3227d2f9fdb50548de6c0c..HEAD

see also

git: list all the files for a particular commit

Use one of the commands below:
  • git show --name-only 76f134a2692c9472af3227d2f9fdb50548de6c0c
  • git diff-tree --no-commit-id --name-only -r 76f134a2692c9472af3227d2f9fdb50548de6c0c
  • ggit show --pretty="format:" --name-only 76f134a2692c9472af3227d2f9fdb50548de6c0c

Mac OS X: install git without xcode

git is included in xcode. but if you just want git, you do not have to install the xcode. You can download and install git for mac.
  1. Download from http://git-scm.com/download/mac
  2. Mount the .dmg file and install the .pkg package inside the image.
  3. After installation you need to prepend /usr/local/git/bin to /etc/paths file and remove /etc/paths.d/git:
    sudo sed -i  -e  '1s/^/\/usr\/local\/git\/bin\'$'\n/g' /etc/paths; rm /etc/paths.d/git
  4. Set PATH environment variables for non-terminal applications:
    /usr/local/git/bin/setup\ git\ PATH\ for\ non-terminal\ programs.sh

git: discard local changes

  • To discard all local uncommitted changes:
    git reset --hard
    which is equivalent to:
    git reset --hard HEAD
  • To discard the latest local commit(move HEAD back by one commit):
    git reset --hard HEAD^
    or move HEAD by 3 commits:
    git reset --hard HEAD~3

git: count total number of lines

  • Method 1:
    git ls-files | xargs wc -l

    or count only .java files:
    git ls-files | grep '.java$' | xargs wc -l
  • Method 2:
    git diff --stat $(git hash-object -t tree /dev/null)

Mac OS X Mountain Lion: Install git

  1. Install xcode from App Store
  2. Start Xcode, select "Xcode" -> "Preferences..." from the menu bar
  3. Activate "Downloads" tab, install "Command Line Tools"
  4. Now you should be able to run git command in Terminal.