Coder Social home page Coder Social logo

gittutorial's Introduction

Git tutorial

This repo handles mainly Git but it also contains some good2know commands and the use of the terminal. Feel free to check it out and leave comments if you want to change something.

Content:

Git:

Stages:

  1. Commited:
    Saved in the git repository database

  2. Staged:
    Not saved in the database, waiting to be commited

  3. Unstaged:
    New files or files that have been edited but not staged

Start:

To start with Git you must first start a repository. This is done with the git init command in the folder you want to create a repository in.
Note: some things might look different because I use a theme in my terminal but the content should be the same.

➜ ~ mkdir testfolder

➜ ~ cd testfolder

➜ testfolder git init
Initialized empty Git repository in /Users/lindberg/testfolder/.git/

➜ testfolder git:(master)

Now you have a git repository to work with. To check that everything works correctly you could use the git status command.

➜ testfolder git:(master) git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

➜ testfolder git:(master)

As you can see there are no commits yet. This means that there are no saved points in the repository. To create a commit (saved point) there must be something to commit, a change. I start by adding a file to my empty repository.

testfolder git:(master) touch newfile.txt

➜ testfolder git:(master?) git status
On branch master

No commits yet

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

 newfile.txt

nothing added to commit but untracked files present (use "git add" to track)

Now I have something to commit, first we must select what we want to commit and we do that with the git add command.

➜ testfolder git:(master?) git add newfile.txt

➜ testfolder git:(master+) git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   newfile.txt


➜ testfolder git:(master+)

Now you have staged a file, this means it is ready to commit. To unstage (to unselect the file) you can use the command above git rm --cached <filename>

We want to commit so we use the git commit command.

This will open your terminal editor most likely vim or nano which are two terminal editors. This is what it looks like in Vim.

1
2 # Please enter the commit message for your changes. Lines starting
3 # with '#' will be ignored, and an empty message aborts the commit.
4 #
5 # On branch master
6 #
7 # Initial commit
8 #
9 # Changes to be committed:
10 #       new file:   newfile.txt
11 #

To start editing type i and to stop editing type esc. To quit vim type :wq to write and quit or just quit :!q. We will go over vim later but these are the navigation commands most people know and use.

Now you have commited (created a save point).

➜ testfolder git:(master+) git commit
[master (root-commit) c8b452a] Test commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 newfile.txt

To check previous commits you can check the log with the git log command.

commit c8b452acee6e408a0adc80356e7ac0fe3983230e (HEAD -> master)
Author: Adrian Lastname <[email protected]>
Date:   Thu Jan 18 09:33:29 2018 +0100

    Test commit
(END)

To exit git log type q.

These are the most basic commands that you will use in your life.

Here is a summary of the commands:

Git init

git init Initializes a new git repository in the current folder

Creates a hidden .git/ folder in the repo

Git status

git status Used to check the status of the repository, see what files have been changed

add an alias for this, you will use it very often. I use 'gs'.

Git Add

git add <file(s)> Add the following files to be staged

git add -A Adds all edited files in the repository

Git commit

git commit -m 'Commit description goes here' Saves the edits as a commit to the database, it is now possible to revert back to this position in time

'-m' = message

git commit -am 'Message' Adds all files and commits

git commit -amend Adds new files to last commit, allows you to change the last commit message

git commit --amend -m 'newmessagehere'

git commit —-amend —no-edit Adds new changes to last commit without editing the commit message

Git log

git log See all the commit history

When you become more experienced you might want to start to look at the git branch command and the git checkout command. These two create a powerful tool that helps you organise commits and to go back if something went wrong.

Here is the summary of the two commands:

Git branch

git branch <branchname> Creates a new timeline for your repository. To go to that timeline you need to use git checkout.

Git checkout

git checkout <branchname> Changes to another branch (timeline) in the repository

git checkout -b <branchname> Creates a new branch with branchname and changes to that branch (timeline)

git checkout -- <filename(s)> Resets the all changes to the file(s) to the state of the last commit

Why would I want to use this? You want to use this because when for example multiple people are working on the same files at the same time. These two commands helps you merge the combined changes that all people create.

Start by creating a new branch by git branch <nameofbranch>.

➜ testfolder git:(master) git branch name

➜ testfolder git:(master) git branch
* master
  name

The first command git branch <nameofbranch> (the branch I created is called name) creates a new branch (timeline) and the second command lists all current branches (timelines). The current branch master is shown by a *.

To go to the other branch you use the git checkout <branchname> command.

➜ testfolder git:(master) git checkout name
Switched to branch 'name'

➜ testfolder git:(name)

You are now on your branch. As you can see on the last line it is shown that I am currently on the branch called name in git:(name).

So far everything is stored locally on your computer, to save it on a remote place like Github you want to work with remotes. To connect your local repository with Github you must first create a Github repository.

After you've done that you can connect the remote repository with the local one like this:

➜ testfolder git:(master) git remote add origin [email protected]:lindbergan/test.git

➜ testfolder git:(master) git push -u origin master
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 443 bytes | 443.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To github.com:lindbergan/test.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

The first command specifies that you want to add a new remote. origin is the name of the remote and [email protected]:lindbergan/test.git specifies where the repository is on the Github server.

[email protected]:lindbergan/test.git could also be https://github.com/lindbergan/test.git depending on if you are using SSH or HTTPS.

The second command git push -u origin master specifies that you want to save your commits on another server (in this case Github).

To do this you use the command git push which pushes your branch to a remote remote repository.

-u is short for --set-upstream which means that the current master branch will send commits when pushed to the remote origin and the branch master on the remote.

Here is the summary for the new commands:

Remote

git remote add <remote name> [url] Adds a new remote with name 'remote name' and url 'url'

Remotes are used to backup data to a database. You can put (push) and get (pull / fetch) information from that database

Git push

git push Pushes all local commits to the remote

git push --set-upstream origin <branchname> Creates the upstream remote for the repository and pushes to it

git push -u origin <branchname>

-u is short for --set-upstream

When you want to get changes someone else has done you use the git pull command. This pulls the changes from the remote repository and merges them to your local repository.

➜ testfolder git:(master) git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:lindbergan/test
   57b0af7..9d813af  master     -> origin/master
Updating 57b0af7..9d813af
Fast-forward
 newfile.txt | 1 +
 1 file changed, 1 insertion(+)

You could also use git fetch. The difference between git pull and git fetch is that git fetch does not merge the changes but gives you the option to do that yourself.

Pull and Fetch

Fetch

git fetch [remote name] [branch name] Get data from repo without merge

Pull

git pull [remote name] [branch name] Get data from repo with merge

You have now learned everything that you need to successfully work with git both locally and remotely. I suggest you go through the Workflow to get a better understanding how to work with git in projects with multiple people.

To learn more about git I will go through some more commands:

Git reset

git reset HEAD <file(s)> Undo files from staged state

Different from checkout because it keeps the changes but need to be added again

Remove Commit

git rebase -i HEAD~ Remove last commit from local head

git reset HEAD~ Remove last commit but keep changes

Git diff

git diff HEAD^ See the difference in the last commit

git diff <commit hash> Difference in that commit

git diff --cached difference of all files

git diff --staged difference of all staged files

Workflow

What I think is the correct workflow. The pictures are from: LearnGitBranching

Initial state: Init state

As you can see here we are on a branch name and we have another branch master. We are currently on name.

To get the latest changes from master to name we use the git rebase command.

➜ testfolder git:(name) git rebase master

After rebase: After rebase

Now you can see that we are on name branch and we have retrieved the changes that we didn't have from master. To get master to where we are now we use the git checkout command and git merge.

➜ testfolder git:(name) git checkout master
Switched to branch 'master'

➜ testfolder git:(master)

After checkout: After checkout

➜ testfolder git:(master) git merge name
Updating c8b452a..57b0af7
Fast-forward
 newfileagain.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 newfileagain.txt

➜ testfolder git:(master)

c8b452a..57b0af7 means that we were on c8b452a commit and we are now on commit 57b0af7. newfileagain.txt was a file I created to make another change so that I could merge two different branches.

After merge: After merge

Git config

This is the git config file. The file exists at: ~/.gitconfig or via the command git config --global --edit.

# This is Git's per-user configuration file.
[user]
	email = [email protected]
	name = Your Name
[core]
	editor = vim
[alias]

You can also set values from the terminal like this:

git config --global user.email "[email protected]"

Set alias from terminal

git config --global alias.aliasnamehere "git command here remove git"

Ex: alias hello => git hello => git log --oneline

git config --global alias.hello "log --oneline"

Alias

Some aliases that I use:

# Git
alias go="git checkout"
alias gs="git status"
alias gl="git log"
alias master="git checkout master"

# Dev
alias dev="cd ~/my-dev-folder/"

# Naviation
alias ds="cd ~/Desktop"
alias dl="cd ~/Downloads"

# ZSH
alias zshaliases="open $ZSH_CUSTOM/.aliases"
alias zshrc="open ~/.zshrc"

# Startup
alias ztart-dev="open chrome && open spotify"

# Other stuff
alias linus="log --graph --oneline --color --decorate --abbrev-commit --all"
alias fuck="!sh -c 'git rebase -i HEAD~ && git pull -f'"
alias justpush="git commit -a -m 'stuff' && git push"

Terminal

Show hidden files

defaults write com.apple.finder AppleShowAllFiles -boolean YES && killall Finder

Kill dock

defaults write com.apple.dashboard mcx-disabled -boolean YES && killall Dock

Access android emulator (Ex: Flickpicker project)

adb -s emulator-5554 shell
cd data/data/com.typeof.flickpicker/database
sqlite3 flickpicker.db

Vim

Start editing:

i

Stop editing:

esc

Save:

:w

Quit:

:q
:!q (sometimes you need to overwrite so you do that with ! before q)

Copy:

CTRL+V

Paste:

P

Search and replace:

:s/x/y/g

Search and replace in lineselection

:%s/x/y/g

Search and replace in all lines

x: word that you want to replace
y: word that you want to replace it with

JPS

Shows the current java virtual machines processes.

➜ ~ jps -l
36340 sun.tools.jps.Jps

To kill process use id (in this case 36340).

➜ ~ kill 36340

Scripts

A startup script I made that lets you open multiple apps on computer startup. Can also be triggered manually but is usually used with autostart functionality in the computer.

Bash script:

#!/bin/bash

clear
python startup.py

Here is the startup python script: startup.py

To get it to autostart on Mac:
Mac autostart

Click the + under Users & Groups in System Preferences and select the bash script.

Picture Downloader

Python script:

import os
import urllib


def download_picture(name, path, width, height, amount):
    url = 'http://www.lorempixel.com/' + str(width) + '/' + str(height) + '/'
    new_name = path + '/' + str(name) + '.png'
    urllib.urlretrieve(url, new_name)


print "#########################################"
print "Hello, and welcome to Picture Downloader!"
print "#########################################"

path = raw_input("Enter path: (" + os.getcwd() + ")\n")
if not path:
    path = os.getcwd()
width = raw_input("Enter width: (200)\n")
if not width:
    width = 200
height = raw_input("Enter height: (200)\n")
if not height:
    height = 200
amount = raw_input("How many pictures: (1)\n")
if not amount:
    amount = 1

for x in range(1, int(amount)+1):
    download_picture(x, path, int(width), int(height), int(amount))
    print "Picture: " + str(x) + " is being downloaded."

print "All pictures are finished downloading."

GPG

List GPG keys:

gpg --list-secret-keys --keyid-format LONG

gittutorial's People

Contributors

lindbergan avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.