Useful commands in git and dockers
Tags: osdc, linux, docker, git
Published: 2023-05-14 06:30:41

Hi All,
In the past last weeks of our OSDC training we've learned to setup local repositories from github uding dockers.
These lessons required the use of plenty new commands (mostly linux) that I"ve documented in this post. Hope it will be helpful to you.

#**Basic linux commands:**
ls -l    (alias ll)
ls -al /root/       (show hidden files in the root dir. ll does the same)
cd /home/       (user directory)
whoami
alias           (which asliases are configured in ~/.bash_aliases)

##Change working user to root
su

##Launch a specific one time process as root without changing the active user (super user do)
sudo

##print current working directory
pwd    

##Handling directories
mkdir
rmdir 
cd ../../home/
cd ~/

##Files
cat filename (View file content as text)
more filename (with paging. Up/ Down/ Q to scroll and quit)
echo some text > filename.txt (Create/ override to new file)
echo more text >> filename.txt (Append to existing file)

##list all commands history for current session/ container
history




#**Dockers**
[Dockers hub](hub.docker.com) is your first place to visit

##Run a container with specific OS

- docker run -it -w /opt --name ubu ubuntu:22.10
- docke r run -it --name flask-dev -w /opt -v c:\users\freddyad\flask:/opt python:3.11 bash

##Exit the container but keep the same state
exit

##List all running docker containers
docker ps 

##List all available docker containers
docker ps  -a

##Open exitsting docker
docker container start -i ubu

##Delete container
docker container rm ubu

##Get list of all updates and apps for the current container's OS
apt-get update
apt-get install package1 package 2

##Resource meter for the docker (requires installation)
htop

##Simple file editor (requires installation)
nano filename (text editor)




#**Git bash**

##Clone a repository from github
git clone https://github.com/path...

##Creating a new git project (from MS terminal or bash outside the docker)
mkdir <project name>
cd <project name>
notepad README.md
(# Add some test to the file, such as a title of the project)
git init
(adds the folder /.get/  with configuration for the new project)

##Show any changes in the local repository
git status

git add readme.md
(use tab to auto complete file name)

(try git status to see the new file in staging)

git commit -m "Description of the commit"

git config --global user.email "mail@domain.com"
git config --global user.name "my name"

##Show configuration file
cat ~/.gitconfig
Note that there is a config file in the user directory and in a project directory


##History of commits
git log

notepad ~/.gitconfig

##return to last directory (only on bash)
cd -

##Uploading a new project to GitHub
To upload a new project to GitHub, use the web site to create a new repository, no need to keep the folder's name

Attach the project to the github repository. Use the SSH as the key
git remote add origin git@github.com/<git_username>/<repository_name>.git

##Show the project's corresponding repository on github
git remote -v

##Change the remote repository
git remote set-url origin git@github.com/<git_username>/<repository_name>.git
(Can be done directly in the project's config file)


##Use the main branch
git branch -M main

## Adding a public key to the current user on the local machine
###Check if there is a public key in the local computer
ls -l ~/.ssh/

###Generate a key
ssh-keygen
(You don't have to add passphrase)

###Show the public key to the config file
cat ~/ssh/id_rsa.public

Copy the generated key to the github user ->settings -> ssh and GPG keys -> new SSH key. Add a meaningful name to the key, such as "current computer name"
 

##Push the changes in the local project to the github repository, and set the origin and the main in the config file
git push -u origin main
(All new files and commits are now visible on github)

##Regular changes in the project after the first configuration
git add <file name>
or git add . (all files)
git commit -m "explanation of the commit"
(Using this command without a -m will open the default editor for the commit details)
git status
(ahead/ staging)
git push

##Creating new branch
git checkout -b <branch name>
git status (show the active branch)

#Creating forks
Creating forks is done only on github's web site.
This enables users without permissions to update the fork copy.
##Push changes to the new fork:
git remote add fork git@... (ssh code of the fork because we are using public key)
git remote -v (view the association of both main and forks)

##push changes to the fork
git push --set --upstream fork .

##Sync local copy with the github repository
git checkout main
git pull origin main (as configured in the config file)

##Show all changes in the repository on all branchs
gitk --all & (will open in different thread)

##rebase the changes in the repository
(This will PR the changes as if they were created after the last change in the base fork)
git checkout <branch name>
git rebase main


##Delete last commit
git reset head~1 --hard

##Override remote with our changes
git push --force

##Merge fork with the main version
git checkout main
git checkout fork
git merge main

Hope this will be helpful for anyone...
Cheers, 
Freddy