Skip to content
MichalKesl edited this page May 2, 2022 · 21 revisions

Installing git

Type git in your terminal. If you see some information about git, then it is already installed. Alternatively, if you computer writes that it doesn’t know what git is (cannot find the command “git”), you need to install it. To install git, write in the terminal sudo apt-get install git. The computer will probably ask you for your account password, and then quickly install git for you.

Git workshop

Take your time and follow along (and do all the practices) this git workshop given by Alon in October 2019, for more advanced subjects follow this lacture advanced Git operations given by Alon in May 2022.

General

The first things you'd like to do after installing git or working on a new computer/server/account is the following:

1. Configure your name and email address:

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

See more here.

2. Make git store your username and password, so you won't have to re-type it each time:

WARNING: This method saves the credentials in plaintext on your disk.

git config --global credential.helper store

See this and that for more info.

2a. Using SSH for remote authorization: Follow the GitHub Instructions to check for existing keys, generate a keypair, add a key and test the connection.

Then, for each repository, you will need to update the remote URL from HTTPS to SSH (fortunately, there are instructions).

3. Signing

It's a good idea to use a key to sign your commits and to do this consistently and it's really easy to set up.

GitHub has a full set of instructions and once you have a GPG key, you can use it for other secure functions, like signing and encrypting email.

Briefly, you'll need to first generate a GPG key pair if you don't already have one. Next, you will need to associate your key with the email address used for GitHub commits, followed by exporting and saving the public key (remember, it's a pair - one private and one public) to you GitHub profile.

Once that is done, you should update your local configuration to know about your key:

$ git config --global user.signingkey $GPGKEYID

You may then choose to individually (manually) sign commits:

$ git commit -S -m your commit message

or set up automatic signing for the current repository:

$ git config commit.gpgsign true

or set up automatic signing for all repositories:

$ git config --global commit.gpgsign true

Workflows

Clone code repositories

To clone (copy) a repository from GitHub to your computer, open a terminal, change the directory into the parent directory where this repository should be located (e.g., ~/Code), and type:

git clone <URL>

where <URL> is the address of the GitHub repo. For example, to clone T3 do:

git clone https://github.com/ReactionMechanismGenerator/T3

Remote

"Remote" is the web address and name we give the corresponding GitHub repository (from now on referred to as "repo" in short). A single repo on your computer may be connected to several "remotes", e.g., the official version of the software online and your own Fork of it under your online GitHub user. We need a "remote" name to avoid typing a web address everything we ask git to communicate with GitHub. Git will associate the remote name with this URL. Note that git will assign the default name "origin" to the remote repo you cloned from. As a convention in our group, we prefer to call the official online software repository with the name "official".

Here's a list of relevant commands:

git remote: show the names of associated online repositories.

git remote -v: show the names and web addressed of associated online repositories.

git remote rename <old> <new>: rename an associated repository from the name <old> to the new name <new>. (you don't need to actually type the < and > symbols)

git remote add <name> <url>: add a new online repo of this project

As a recommendation, type the following command for each of your repositories (e.g., RMG-Py, RMG-database, T3, ARC...) to rename the remote to our convention:

git remote rename origin official

then type git remote -v to see that your changes took place.

branch

Creating and moving (checking out) branches is done via the following commands:

git branch: see the available branches (but take no action)

git branch <branch name>: create a new branch from the point you're on with the name <branch name>

git checkout <branch name>: move to branch <branch name>

git checkout -b <branch name>: create a new branch called <branch name> AND check it out (move to it)

git branch -d <branch name>: delete a branch (git will only let you do that if the branch was merged onto master, so usually this command will fail)

git branch -D <branch name>: (capital D) force git to delete the branch <branch name>

update your branch

Here is a sequence of commands to update your branch with the master branch on the official repo. Everything after the # sign is a comment and should not be typed.

git checkout <branch name>  # move to the desired branch you'd like to update
git log                     # OPTIONAL: shows the recent commits. press "q" to quit this display.
git status                  # OPTIONAL: shows uncommited changes.
git fetch official          # important: make sure git is "aware" of recent changes to the remote official repo

# **if you are on the master branch** and would like to update it with the online master branch, do:
git pull official master

# **if you are on a different branch (not master)** and would like to rebase it on the updated online master branch, do:
git rebase official/master

Note: If you modified code on the RMG-Py repo, sometimes you'll need to re-compile (type 'make' after activating the RMG environment).

Workflow for making code modifications

git fetch official
git checkout master
git pull official master             # making sure your starting point is the updated master branch
git checkout -b <branch name>        # making sure you make your changes on the desired (new) branch

<make your code modifications now>

git status
git add <file>                       # stage the modifications made to a single file, alternatively you can do "git add ." to stage everything
git commit -m "a clear commit message"
git log                              # see your commits before you push them
git push <remote> <branch>           # e.g., "git push official my_fix_branch"

Note that if you already pushed this branch and now rebased it locally or squashed commits, you'll need to do "git push -f" to FORCE PUSH this branch to the remote repo.

One straightforward way to locally squash commits on your working branch is discussed here.

interactive rebase

In the case that you have a branch with multiple commits and now want (need) to squash commits before merge, this is how: With the upstream/remote "origin" and local branch "pull-me"

$ git fetch origin
$ git checkout pull-me 
$ git rebase -i origin/master

< choose squash for all of your commits, except the first one >
< Edit the commit message to make sense, and describe all your changes >

$ git push origin pull-me -f

solving merge conflicts

GUI

ONLY AFTER YOU FEEL YOU HAVE MASTERED THE USAGE OF GIT VIA THE TERMINAL, YOU MAY TRY USING A GUI TO FACILITATE GIT COMMANDS. Two GUI's (graphical user interface) are recommended:

Note that various IDEs (integrated development environment software tools) also offer git interactions. These include, for example, PyCharm and Atom.

Cheatsheet

See the git cheat sheet for more common commands.

Clone this wiki locally