Git commands to live by

This is a collection of common git commands that I use very often in my daily work. I have only touched on GitHub and find it fairly sufficient for my needs, and thus this post will base on GitHub as well.

Case 1: start a new repository

Let’s say we want to upload ~/analysis/ to GitHub

On GitHub: create a new repository

The only things I will set here are the repository name and the choice whether it is public or private. I do not initialise the repository with a README if I have the files in a local folder (which is often the case because I get work done on my computer before sharing them on GitHub). Creating any files in GitHub repository will cause clashes (more below).

On Local machine: create a git repository and push

  1. change directory to the directory that will be uploaded. So in this case we need to cd ~/analysis

  2. git init note create a .gitignore file now to exclude selected files from being uploaded (more below)

  3. git add . will add all files under current directory except those in .gitignore. Alternatively, git add *files* can be used to add specific files

  4. git commit -m "some message" to commit the current added changes to local repository

  5. git push origin master to push the changes to online GitHub repository. origin master can be replaced by any other branch name

  6. When the online repository has been updated elsewhere, use git pull origin master to update the changes to local repository

Case 2: download online repository

Use git clone *url* command to sync the online repository to local repository. Downloading code as a zip file is not recommended because that will automatically set up the git repository that allows you to use git in the directory.

Case 3: change online repository address

Sometimes online repository address can be changed when you change https to ssh, or when you change your username or any other secnario. To update the online repository address in local repository, use git remote set-url origin *url*. If you just want to view the current url, use git remote get-url origin

Case 4: branching and merging

Branching is useful to maintain the original structure of the working software while working on new features on a separate copy of the software - the branch.

  • git checkout -b branch_name creates a new branch and switch to that branch. Remove -b if the branch already exists.
  • git branch shows all branches and the one that I am currently on
  • git branch -m old_name new_name renames the branch
  • git push origin :old-name new-name delete the old branch in the remote repository
  • git push origin -u new-name set the remote repository to be pushed to in the current local branch to be the new branch

merge and merge conflicts

First, before we resolve merge conflicts, it is best to understand what a merge conflict is in the first place. This page explains that.

This tutorial is technically still readable.

How to use meld

Meld indicates to you where the changes are by highlighting them. Just follow them and click on the arrows/edit the scripts by hand.

Use this to set up meld as the default merge tool. git checkout – .

overwrite remote repository with local repository

git push --force

Case 5: delete a branch both locally and remotely

$ git push --delete <remote_name> <branch_name>
$ git branch -d <branch_name>

Case 6: Go back to a previous commit

REfer to the selected answer here

Case 7: made changes but want to save the changes to another repository instead.

Notes

One-command push

Sometimes it may be tedious to key in three commands to push just one change. In this case, you can create a function to complete all steps at once.

function lazygit() {
    git add .
    git commit -a -m "$1"
    git push
}

.gitignore

I often choose to ignore everything except the most often used ones.

*
!exception_files

more on this

Resolve clashes

The way to resolve the clash is to pull and merge the files in GitHub. I will update when the error appears again.

conflicts between windows and unix

Windows uses CRLF but UNIX uses LF as line endings. This may potentailly create merge conflict. To solve this, set the git config core.autocrlf input option and follow the advice on GitHub here.

Use file file to indicate the file endings, as explained here

FAQ on Git

  • what is origin origin is a local alias set as a key in place of the remote repository URL. It refers to the remote copy of the git repository.
  • what does “Your branch is up-to-date with ‘origin/master’.” mean? The branch is up-to-date with the latest commit reference that was fetched last time with either git fetch or git merge.
Avatar
Tim
Computational Biologist & Medical Student

Personalizing medicine

Related

comments powered by Disqus