Git Hidden Folder
There is a hidden folder called .git
which tells you that our project is a git repo.
If we wanted to create a git repo in a new project we' create the folder and the initalize that repo using git init
mkdir /workspaces/tmp/new-project
cd /workspaces/tmp/new-project
git init
touch Readme.md
code Readme.md
git status
git add Readme.md
# makes changes to readme.md
git commit -m "add readme file"
We can clone three ways: HTTPS, SSH, Github CLI
Since we are using GitHub Codespaecs we'll a create temporary directory in our workspace
mkdir /workspace/tmp
cd /workspace/tmp
git clone https://github.com/andrew-wc-brown/Github-Examples.git
cd GitHub-Examples
You'll need to generate a Personal Access Token (PAT) https://github.com/settings/token
You will use the PAT as your password when you login
- Give it access to Contents for Commits
git clone git@github.com:andrew-wc-brown/Github-Examples.git
cd GitHub-Examples
We will need to create our own SSH rsa key pair
sshe-keygen -t rsa
For WSL users and if you crete a non default key you might need to add it
eval `ssh-agent`
ssh-add /home/andrew/.ssh/alt-github_id_rsa
We can test our connection here:
ssh -T git@github.com
Install the CLI
eg. Linux (Ubuntu)
type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y)
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y
gh auth login
gh repo clone andrew-wc-brown/Github-Examples
When we want to commit code we can write git commit which will open up the commit edit message in the editor of choice.
git commit
Set the global editor
git config --global core.editor emacs
Make a commit and commit message without opening an editor
git commit -m "add another exclamation"
List of branches
git branch
Create a new branch
git branch branch-name
Checkout the branch
git checkout dev
We can add remote but often you will just add remote via upsteam when adding a branch
git remote add ...
git branch -u origin new-feature
git stash list
git stash
git stash save my-name
git stash apply
git stash pop
git checkout dev
git merge main
When we want to stage changes that will be included in the commit We can use the . to add all possible files.
git add Readme.md
git add .
Reset allows you to move Staged changes to be unstaged. This is useful when you to revert all files not to be not commited
git add .
git reset
git reset will revet a git add.
Git status shows you what files will or will not be commited.
git status
The gitconfig file is what stores your global configurations for git such as email, name, editor and more.
Showing the contnets of our .gitconfig file
git config --list
When you first install Git on a machine you are suppose to set up your name and email
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
git log will show recent git commits to the git tree
When we want to push a repo to our remote origin
git push