GIT useful tips

Introduction

Git commands

Remote repository

Get a copy of a remote repository

git clone <git-repository-url>

When authenticating with SSH, use the appropriate SSH url.

Update list of repository’s remote branches

git remote update <remote-repo> --prune
  • --prune removes any local reference to non-existing remote branches

If we have other remotes, just repeat the process changing the remote name:

git remote update <another-remote-repo> --prune

Set the remote repository branch to track

git branch -u <remote-repo>/<remote-branch>

Check repository tracking config

You can always check what branches are tracked for each local branch in the .git/config file:

[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = git@github.com:ProtossGP32/gameBoardStats.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
        remote = origin
        merge = refs/heads/main
[remote "upstream"]
        url = git@github.com:cifoteam/gameBoardStats.git
        fetch = +refs/heads/main:refs/remotes/upstream/main
[branch "task/59-create-gamescollectionmanager-class"]
        remote = origin
        merge = refs/heads/task/59-create-gamescollectionmanager-class
[branch "task/66-modify-gamescollection-class-to-include-info-related-to-the-games-usage"]
        remote = origin
        merge = refs/heads/task/66-modify-gamescollection-class-to-include-info-related-to-the-games-usage

Branches

Show available branches

git branch
  • Also show remote branches:
git branch -a

Clone an existing branch

git checkout <branch-name>

If you want to checkout a branch from another remote repository, do the following:

Create a new branch

git checkout -b <new-branch-name>
Differences between new switch command and checkout

This StackOverflow thread explains the reason why switch command has been added and the translation of older checkout commands to the new ones

Delete a local branch

git branch --delete <branch-name>

Rename a remote branch (StackOverflow post)

# In this option, we will push the branch to the remote with the new name
# While keeping the local name as is
git push <remote> <remote>/<old_name>:refs/heads/<new_name> :<old_name>

Delete a remote branch

git push origin --delete feature/login

Updating local checkouts

Retrieve latest changes in remote repositories

git fetch
  • This command downloads the git objects (blobs, trees and commits), it doesn’t merge the changes into the local checkouts.
    • This means that the files in local branches are still behind the latest commits, they must be merged or fast-forwarded

Update local branches

git merge <remote-branch> <local-branch>

Overwrite local branches with remote repositories

git pull

Pushing local checkouts

Push a local branch to a remote repository

When pushing a branch for the first time, it has to be created in the remote repository. This is done during push with the following command:

git push --set-upstream <remote-repo> <local-branch>
  • --set-upstream automatically sets up the tracking information for that branch

If the branch is already created in the remote repository or you’re pushing to a different remote (and don’t want it as the following repository), you can simply push it to the desired remote by executing:

git push <remote-repo>

Be aware that for this to work, a remote repository must be already configured as the following one. I.e: you already told Git that you’re following the same branch in origin repo and now you want to simply push it to the upstream repository.

Git Large File System

Due to size limits on GitHub, Git LFS can be used instead.

Git LFS handles large files by storing references to the file in the repository, but not the actual file itself.

Follow the next articles to both install and configure what’s needed to use LFS:

  • Install: this will install the required binaries in your local server to start working with LFS

  • Configure: this will help you configure the type of files that you want to be stored in Git LFS

  • Pushing: if having troubles uploading to LFS for the first time, try to execute first this command:

    $ git lfs push --all origin
    Uploading LFS objects: 100% (17/17), 1.3 GB | 30 MB/s, done.             

    A progress count should verify that indeed the LFS is available. Oncs done, push the rest of the commits with the classic git push

In order to track some type of extension file as LFS, launch from your repository:

$ git lfs track "*.<selected-extension>"

Git operation after installing and configuring LFS are the same as always, the only thing to know is that git lfs commands will execute underneath them.