User Tools

Site Tools


coding:git

Git is a version control manager for code. When used together with a hosting service like GitHub, it helps to organize workflows both as a single developer or as a group, provides a way to maintain safe backups, and tag versions.

Mini tutorial

This will walk you trough the first steps with an existing git repository, e.g. the analyze_scripts-group repository. First, open a terminal and navigate to the directory you want the git repository to be saved in. Here, we use ~/Programs/ as an example.

cd ~/Programs/
git clone https://github.com/astatt/analyze_scripts.git 

This will create a folder called ~/Programs/analyze_scripts and download the repositories content into it. You only need to clone a repository once, unless you mess up badly, then you can always delete everything and clone the remote repository again.

cd analyze_scripts
git status

This will tell you some information about the git repository, for example that you are on branch main and up to date. Now, edit and save some files, and you will see the changes highlighted if you do git status as “untracked files”, “Changes not staged for commit:” or “modified files”. You can execute all git commands anywhere in the ~/Programs/analyze_scripts-folder.

If you want to transfer the local changes you have made to a file into the remote repository, you need to tell git.

git add <filename>
git status

First, you add the change to the list of changes to be committed (you can see now that the file is highlighted under “Changes to be committed:” and not “Untracked files:” or “Changes not staged for commit:”. Once you are happy with your edits, do

git commit

This will bring up a terminal text editor, usually 'vim' (you can change the default). It will ask you to describe your changes in 80 characters or less, so be concise and descriptive. Hit i to get into “insert”-mode, then type your description, then hit esc to leave “insert”-mode, then enter :wq to save and exit. After that, do

git push

This transfers the local commits to the remote (GitHub) repository. You can check on GitHub that your changes are there now, go to: https://github.com/astatt/analyze_scripts to see it. To retrieve remote changes do

git pull

To summarize, a simple, typical workflow looks like this:

  • git pull (to make sure you are working with the latest versions)
  • edit some files, test, make changes …
  • git add <all-files-that-changed>
  • git commit
  • (maybe edit more, do more tests, repeat git add and git commit as long as needed)
  • git push (you can push one or many commits)
  • repeat.

Generally, don't change too many things at once in a single commit, don't wait too long between commits, don't wait too long between git push/pull. You can change many of the default git behaviors to adjust them to your workflow. You might also need to tell git your name with

git config [--global] user.name "Full Name"
git config [--global] user.email "email@address.com"

General git tutorials

Some useful comments

Downloading and updating

Downloading/cloning a git repository from GitHub for the first time:

git clone https://github.com/...
Branches

Switching to a particular branch and setup monitoring of the local changes/remote branch:

git checkout feature/mpcd_sine_channel
git branch --set-upstream-to=origin/feature/mpcd_sine_channel feature/mpcd_sine_channel
git pull

See https://devconnected.com/how-to-set-upstream-branch-on-git/ .

For a new feature branch:

git checkout -b feature/pair_hertz
git push --set-upstream origin feature/pair_hertz
Store credentials

You can use the git config to enable credentials storage in git, useful for authentication in GitHub.

git config --global credential.helper store

When running this command, the first time you pull or push from the remote repository, you'll get asked about the username and password. Afterwards, for consequent communications with the remote repository you don't have to provide the username and password.

Highlight changes

See all differences with the previous commit:

git diff HEAD~1 # in general: git diff (sha-id-one) (sha-id-two) where sha=git-hash

Differences in a single file:

git diff HEAD~1 <file-name>

Find out who changed what when:

git blame --color-lines <file-name>

GitHub repositories

How to create your own repository

This is especially useful to backup your codes. You may want to create github repositories for your analyzing and/or simulation scripts. (Be cautious, I advise to make a copy before starting just to be safe.)

  • Open your github account from browser and click on the “+” sign at the top right. Then click new repository. Set the preferences. Dont' add a readme file. Copy the URL.
  • Say you want to backup scripts. Open terminal and cd ~/scripts or wherever they are.
  • git init
  • git add .
  • git commit -m “First commit”
  • git remote add origin <copied url>
  • git push -u origin master Note : main is currently used by github instead of master. So master will just create a branch called master, I think. Update : When I try with git push -u origin main I get the error error: src refspec main does not match any. Master works (as a new branch)

Initializing your repository with .gitignore

In your project or simulations folder you may have simulation outputs (.gsd, .dump, .dat etc.). You shouldn't include these large files to your repository since git will probably fail while trying to push them. Your repository is not automatically created with .gitignore so find one from another git repository (mc_sim for example. Between the steps git init and git add . (see above) add, commit and push that one .gitignore file first without adding anything else. You can/should modify the .gitignore file according to your output files and directories. After that you can add and commit the actual files/folders.

coding/git.txt · Last modified: 2022/10/17 00:13 by bargun2

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki