fx notes

Search

Search IconIcon to open search

Git Basics

Last updated Jan 13, 2023 Edit Source

# Concept

# Workflow

  1. pull latest
  2. create new branch
  3. do work
  4. rebase against main/master
    1. resolve conflicts
  5. push to remote
  6. raise pull request
  7. discuss
  8. merge

# Setup

# Initializing Git Repo:

// create git repo in current dir:

1
git init . 

// get version

1
git --version

# Staging Files

// add files to be tracked by git

1
git add filename

This marks the file and tells git to pay attention to it when commiting to the commit history

// add all files from this directory and downwards

1
git add .

// stage all files in repo (also upwards)

1
git add -A

// unstage files

1
2
3
git rm -r --cached filename

git rm -r --cached .

# Status

// get insight into which files are staged, commited or untracked

1
git status

# Configuration

// configure user info etc.

1
2
3
4
5
git config --global user.name "[name]"

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

git config --global color.ui auto

# Commit, Push & Pull

# Commits

Like a save point for all staged files

1
git commit -m "message to describe what is being commited"

// get overview of commits

1
git log

// compact layout

1
git log --oneline

// show specifics of a commit

copy COMMITHASH from log and dod

1
git show COMMITHASH

// show changes since last commit

1
git diff

// add messages after commit

1
git commit --amend -m "message"

# Branches

Branches are an important part of working with Git. Any commits you make will be made on the branch you’re currently “checked out” to. Use git status to see which branch that is.

// get current branch

1
git branch

// show all branches

1
git branch -a

// show remote branches

1
git branch -r

// rename branch to main

1
git branch -M main

// create branch

1
git branch branchname

// switch branch

1
git checkout branchname

// switch and create branch

1
git checkout -b branchname

// switch to main

1
git checkout -

// delete branch

1
git branch -d branchname

# Push

// push the repository to the remote github repo

1
git remote add origin git@github.com:...
1
git push -u origin main

// push new branch to remote

1
git push --set-upstream origin branchname

or

1
git push -u origin branchname 

# GitHub

# Configure SSH Keys

If you can’t push to your remote repo you might have to configure an ssh access

-> GitHub SSH Docs

# Clone from Remote

// download / mirror remote repo to local machine

1
git clone https://github.com/username/reponame.git

# Pull

// download changes from remote server

1
git pull

// get from specific branch

1
git pull origin main

# Pull Requests

ask the repository owner on e.g. github to merge your changes to the main/master branch

# Merging

# Merge

// merge branch to current branch (main)

1
git merge branchname

# Rebase

Assume the following history exists and the current branch is “topic”:

1
2
3
          A---B---C topic
         /
    D---E---F---G master

From this point, the result of either of the following commands:

//update current branch with changes that happend on master in the mean time

1
2
git rebase master
git rebase master topic

would be:

1
2
3
                  A'--B'--C' topic
                 /
    D---E---F---G master

NOTE: The latter form is just a short-hand of git checkout topic followed by git rebase master. When rebase exits topic will remain the checked-out branch.

# Misc

# .gitignore file

Sometimes it may be a good idea to exclude files from being tracked with Git. This is typically done in a special file named .gitignore. You can find helpful templates for .gitignore files at github.com/github/gitignore.

# Submodules

to be able to use git repos inside other git repos use submodules:

// install a git repo as a submodule into another repo

1
git submodule add https://github.com/username/reponame

when cloning repos with submodules you get the folders but not the files

// to get the files of the submodules

1
2
git submodule init
git submodule update

// do it all in one step

1
2

git clone --recurse-submodules https://github.com/username/reponame

Submodules - Git Book


sources / further reading:


Interactive Graph