GIT useful tips
Introduction
Git commands
Remote repository
Get a copy of a remote repository
When authenticating with SSH, use the appropriate SSH url.
Update list of repository’s remote branches
--prune
removes any local reference to non-existing remote branches
If we have other remotes, just repeat the process changing the remote name:
Set the remote repository branch to track
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
- Also show remote branches:
Clone an existing branch
If you want to checkout a branch from another remote repository, do the following:
Create a new branch
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
Rename a remote branch (StackOverflow post)
Delete a remote branch
Updating local checkouts
Retrieve latest changes in remote repositories
- 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
merge
d orfast-forward
ed
- This means that the files in local branches are still behind the latest commits, they must be
Update local branches
Overwrite local branches with remote repositories
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:
--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:
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:
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 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.