Git commands

Git repository clone locally.

cp -r SourceDir TargetDir

Push a local repository to Gitlab.UTU

Set up a Project on gitlab.utu.fi this will bring an instruction page.

cd existing_repo
git remote add origin https://gitlab.utu.fi/remoterepo/coolproject.git
git push -u origin --all
git push -u origin --tags

To change git remote repository mapping:

Go to the local git repo and check the remote repository URL.

cd git.repo
git remote -v

To change the remote repository

git remote set-url origin https://[newURL]

Fix blank diff in Git repositories

Remove the problem of diff being blank on git when file permissions change. Happens when git repos are synced to the cloud.

Go to the git directory/repository and run

git config core.filemode false

Clone a remote repository, but disable Push.

For times when you want to disable push so you accidentally don’t push any changes to the remote repo.

git clone https://gitlab.utu.fi/EloGroup/Project.git
cd Project
git remote -v # Check remote
git remote set-url --push origin Push_Disabled
git remote -v # Check that it says Remote disabled for push

Cleaning up sensitive files or data from Git repository

Clone your git repo as a bare repo using:

git clone --mirror git@gitlab.com:robocopAlpha/pihole_lists.git

Download BFG and run:

# bfg --delete-files id_{dsa,rsa}  pihole_lists.git

bfg --delete-files makehosts.R pihole_lists.git
git reflog expire --expire=now --all && git gc --prune=now --aggressive
git push

If push fails with a message like

 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@gitlab.com:robocopAlpha/pihole_lists.git'

then go to gitlab.com and -> project settings->protected branches-> click un-protect. Then push using

git push -u -f  master

Cloning a remote repository into a separate branch in another repository

Initialiaze a new repository with a readme on github /gitlab / locally.

git clone git@gitlab.com:dotGuard/cereberus.git
cd cereberus

Now we can start cloning the other remote repo to a local branch

git switch master
git remote add fork git@gitlab.com:dotGuard/imac-2013.git
git switch -c iMac2013
git fetch fork

# Optional merge
# git switch master
# git merge --allow-unrelated-histories remotes/fork/master import

Then rebase through some gui like:

Screenshot of the Mac app Fork showing where to find the ‘rebase’ option.

Then push the changes upstream to a new branch on origin and remove remote for fork:

git push -u origin iMac2013
git remote rm fork
git remote -v
# should only list origin

Swtiching to branches and restoring files:

​```git checkout branch``` & ```git checkout filename``` have been changed slightly
# git checkout branch is now:
git swtich branch
# Force switch
git switch -f branch

# create a branch and switch to it
git switch -c branch

# git checkout filename is now:
git restore filename

List files in a git repo (useful for bare repo)

dotGuard ls-tree --full-tree -r HEAD # shows hashes
dotGuard ls-tree --full-tree -r --name-only HEAD

Using git lfs

git lfs or git large-file-system has been introduced for smart mangement of large objects in git repos.

Here is how to migrate your big files to git lfs.

  1. Installing and seting up required tools git-lfs:

    # Install brew from https://brew.sh
    # optional get brewlog from https://github.com/dchakro/brewlog
    # OR just substitute brewlog for brew in the following commands
    # update git, as git>=2.2
    brewlog upgrade git
    brewlog install git-lfs git-filter-repo
    
    git-lfs install
    
  2. Use git-lfs to track large files

    # example to add by file type
    git lfs track "*.RDS"
    git lfs track "*.afphoto"
    
    # Add a folder
    git lfs track "assets/**"
    
    git add .
    # OR use git add <filenames>
    
    git push
    

migrate large files to git-lfs

  1. Install and setup git lfs as above.

  2. Archive the repo (without the .git folder)

    tar --exclude=".git"  -cf - repo | xz -v -T2 -z - > ~/Desktop/repo_bak.tar.xz
    
  3. migrate files to git lfs

    cd repo
    git filter-repo --strip-blobs-bigger-than 1M --force
    # remove the folder called "delete"
    git filter-repo --invert-paths --path-glob '*/delete' --path delete
    
    # restore files from backup
    tar -xvf ~/Desktop/repo_bak.tar.xz ~/Desktop/.
    
    cp ~/Desktop/repo.bak/* .
    
    git lfs track "delete/*"
    git lfs track "*.RDS"
    git add .
    gcm 'migrate to git lfs'
    
    git remote add origin git@github.com:username/repo.git
    git push -f origin master
    # Output should say something like:
    # Uploading LFS objects:   0% (0/1), 2.2 MB
    
    # this following command will show the files tracked by git lfs
    git lfs ls-files