Cloning a branch from GitHub#

To clone, or make a local copy of a repository, use:

git clone <repo_url/https/ssh/>

git remote # to see remote origins
git remote -v # more deaitls, verbose.

Fetching from remote#

To update our local copy of the remote repository use:

git fetch origin <branchname>   # to fetch a specific branch
git fetch origin                # to get all new commits on all branches
git fetch                       # git asumes the "origin" keyword
git branch -vv                  # very verbose details on the brnach and it's
                                # relation to the origin.

Note: The fetched are not yet committed to the local repo.

# merge from the local version of the branch
git merge origin/<branchname>

Pulling from Remote#

When we use fetch we also need to use merge. Pull is fetch and merge. But there are options, a pull, fetch and three-way merge or a pull –rebase, a fetch plus rebase.

# three-way merge
git pull # if your gitconfig default is set to --no-rebase
# equivalent to
git pull --no-rebase

# to force a rebase merge
git pull --rebase

# defaults can be set

git config pull.rebase false  # merge
git config pull.rebase true   # rebase
git config pull.ff only       # fast-forward only

# replace git config with git config --global to set as default

Push a commit#

To push or update the remote branch with local commits we use:

git push <remoteRepository> <branchName>
# git assumes brnachName to be the current branch
git push <remoteRepository>
# git assumes <remoteRepository as origin so we can write
git push # to push the local commits on the current branch to origin.

Share and Delete a Tag#

To share a local tag on a commit to a remote repository:

# create the tag
git tag <tagName>   # tags the current commit
git tag <tagName> <commit ref> # tags the particular commit

# to share the tag to the remote repository

git push origin <tagName>

# To delete a remote tag
git push origin --delete <tagName>

# To delete tag locally (as above in Tagging section)
git tag -d <tagName>


Releases are a high level feature built on top of tags. (in Github)

Sharing Branches#

To share our local branches:

git branch -vv # show local branches, and the remote associated branch if it
git branch -r  # show the remote branches

git -u origin <branchName> # for first push
# the branch can now be used as a normal linked branch like our main.

git push -d origin <branchName> # delete the remote branch
# however if we look at
git branch -r # we see our local branch is still linked with a remote branch
marked 'gone'
git branch -d <branchName> # delete the local branch


To pull a remote branch and work on it locally:

git fetch
git branch -r  # display remote branches
git switch -c <localBranchName> origin/<remoteBranchName>
# this creates and links the new branch to the newly fetched remote branch

To remove a remote branch that no longer exists remotely but the link to it does locally:

git branch -d <localBranchName> # delete local version
git remote prune origin         # delete remote link

# delete

Work on Open Source#

Read and follow the instructions for contributing provided by the repository owner(s).

  • Fork on Github and clone your fork locally.
  • Create a new branch locally.
  • Push changes to your fork and from there create a pull request.

Keeping Fork Updated#

In order to keep our fork up-to-date with the original repository we can add a link between to our local copy of the forked repository and the original repository. We can then push updates to our fork for local.

git remote -v   #show details of remote repositories
git remote add <nameOfBaseOrUpstreamRemoteLink> # usually base or upstream
git remote -v # confirm the new remote connections

# to rename remote repository
git remote rename upstream base  # change name from upstream to base
git remote rm <remotename> # Remove the remote link

git fetch upstream  # fetch latest commits from upstream remote

git log ....

# If the local is not in snyc with the upstream
git switch main  # switch to local main

git merge upstream/main main

# push any changes to the forked repository
git push

# It's always good to build you changes on the most recent version of the main
# branch.
git switch <featBranchName>
git merge main