14 June 2022

git

  • git is a source control management system

example code

cd ~/Desktop
mkdir myproject # create a project directory

git --version # check to make sure git is installed on local computer
git init # initiatilize git

ls -la # check for .git directory
vim main.py # create test script

git status # look at current status of git repository
git add main.py # add main.py script to git repository
git commit # add comment to top of document

vim .gitignore # create gitignore file to specify files in directory that should NOT be included in push/pull
# main.pyc
git add main.py .gitignore
git commit -m "added main.py and .gitignore files" # use -m tag to include commit in command line

git status
git checkout -b dev # make a new branch for current state of code (ex: frustrated with code and need to step away)
git add main.py
git commit -m "code is busted"

git checkout master # return to master branch... still has original main.py script (without latest updates that were pushed to dev branch)

git branch --list # list branches that exist in repository

git reset --hard # erases EVERY change since the last commit ****BE CAREFUL WITH THIS COMMAND****

git checkout dev
config file
vim ~/.gitconfig 

[user]
    email = jmnosh@gmail.com
    name = Jaclyn Noshay
vim settings
  • adjust command line vim viewer
vim ~/.vimrc

syntax on

set tabstop=4
set softtabstop=4
set expandtab

set number
set showcmd

set cursorline

filetype indent on

set wildmenu

set lazyredraw

set showmatch

set incsearch
set hlsearch

# when in vim if want numbers removed... 
    # :set nonumber

# to remove full line
    # dd
# to remove X lines below cursor
    # dX[down]

github connection

  • Set-up connection between github profile and command line

  • Github online: make new repository [titled: git_practice]

# Add ssh to github account
cd ~/.ssh 
cat config # look at current defined ssh commands
mkdir githubkey

ssh-keygen -t ed25519 -C "jmnosh@gmail.com"
mv id* githubkey/.
eval "$(ssh-agent -s)"
cd githubkey
ssh-add ./id_ed25519

head id_ed25519.pub
  • Github online: new ssh key
    • paste contents of id_ed25519.pub file
# Push existing repository from local computer
git remote add origin git@github.com:nosha003/git_practice.git 
git branch -M main
git push -u origin main

git checkout master
vim main.py # make adjustment to file
git push # git push --set-upstream origin master
# on Andes
git clone git@github.com:nosha003/git_practice.git # clone existing repository onto olcf
git log # see git commit history
vim main.py

git checkout ba76527cdb2334b145cb3d5f0cbfe21ef0249ecc # going "back in time" to a previous state of updates --> 'detached HEAD' state
vim main.py # look at state of script

# make adjustments to main.py 
git checkout -b testbranch # create new branch with state of scripts
git add main.py 
git commit -m "back in time plus things"
git push # git push --set-upstream origin testbranch

git checkout main # go back to main branch (does not include the updates that were pushed to testbranch)
vim main.py # make changes in main.py from main branch
git add main.py
git commit -m "update in main"
git push # push to repository
git status
# back on local computer
# adjustment made and pushed to repository... update on local
git pull

# You can fetch all branches from all remotes like this:
git fetch --all
# To update local branches which track remote branches:
git pull --all

# To track all remote branches execute this oneliner BEFORE git pull --all:
git branch -r | grep -v '\->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done