♦ πŸ† 2 min, 🐌 5 min

Git introducton

This giude was adpoted from a workshop held at Science Hack day 2017 link

What is git?

Git is code versioning standard. There are many git hosting services available:GitHub, GitLab, BitBucket, Visual Studio Team Services,...

With git you can have your code safely on a remote server, ready to be accessedby whomever you grant access. With the proper use of git all previous versionsof your documents are saved and available.

On Linux, chances are that git command line interface is already installed. OnWindows and Mac computers I recommend you download git tools . You can open a bash terminal with right click in yourpreffered folder and clicking Git Bash Here.

Create project

Create new project. It is recommended that you include a README.md file in yourrepository (like this one) that gives a description of your project.

You can write the README file using the Markdown descriptive language .

Basic git commands

First let us clarify the difference between local and remote repository. Remote repository is the code that is stored on remote server like GitLab server at ETH in our case. Local repository is the repository containing code on your personal laptop or PC.When first creating a local copy of a remote git repository you can use the git clonecommand. The actual url will depend on your username and repository, but forthis repository this command would look like this:

git clone git@bitbucket.org:ZigaBrencic/new_developer.git

Once you have a local copy you will never have to call git clone for thisrepository again.

You have at your disposal the following basic commands:

  • git pull pull new changes from the remote server to your local copy,
  • git push push changes from your local copy to the remote server,
  • git add add all changes in to __staging__,
  • git reset reverse git add,
  • git commit -m commit all changes that are in __staging__,
  • git checkout revert all files in local directory to what they were after commit (be careful here you can loose uncommited changes you've made),
  • git status view the current state of your local copy,
  • git log view the history of commits in this repository,
  • git diff show all unstaged changes in your local copy.

git basics

Creating new project

Now let's go back to our computer and creat a new project:

mkdir git_intro
git init
git remote add origin https://gitlab.ethz.ch/ziga.brencic/git_intro.git
git add .
git commit -m "Initial commit"
git push -u origin master

What a typical workflow might look like

$ git status  # Make sure I have no local changes
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

$ git pull # Get new changes from remote server
Already up to date.
$ touch new_file.txt  # Create a new file
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
(use "git add ..." to include in what will be committed)

new_file.txt

nothing added to commit but untracked files present (use "git add" to track)
$ git add new_file.txt  # Add new file to staging
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
(use "git reset HEAD ..." to unstage)

new file: new_file.txt
$ git commit -m "Create new file"  # Commit the changes
[master 6b1ff71] Create new file
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 new_file.txt
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)

nothing to commit, working tree clean
$ git push  # Push the new file into the remote repository
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 315 bytes | 315.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://gitlab.ethz.ch/ziga.brencic/git_intro.git
817f344..6b1ff71 master -> master

Branches

Sometimes you want to create a new branch of commits. Maybe to create a newfeature or to fix a bug without disturbing the master branch, which is thedefault branch name.

git branch

You can create a new branch with the command

git branch 

You can switch between branches with the command

git checkout 

Joining new branches back into the master branch

Once you complete a feature or resolve a bug you might wish to port the changesback to the master branch.

There are two ways of joining branches; merging branches and rebasing branches.

Git merge

git merge - merges into the current branch andcreates a so-called merge commit where it commits the different changes fromboth branches.

git merge

Git rebase

git rebase - applies the changes from and thenre-applies the changes of the current branch on top.

git rebase

Typical workflow

$ git branch new-feature  # Create a new branch
$ git checkout new-feature # Move to the new brach
$ echo "Adding a line to README" >> README.md # Edit a file
$ git add README.md # Add a file to staging
$ git commit -m "Update README" # Commit the file
$ git checkout master # Move back to the master branch
$ git pull # Fetch the new changes on master
$ git checkout new-feature # Move back to the new branch
$ git rebase master # Join changes from master to the new branches
$ git checkout master # Move to the master branch
$ git merge new-feature # Merge the changes into the master branch

Jokes

git fire

Get notified & read regularly πŸ‘‡