(testing remote change)
I listened the Cloud Gurus' podcast episode about CI/CD and CI/CT Pipelines and wanted to brush up on some Github concepts and commands.
This LinkedIn course had a good walkthrough of Github commands and best practices when collaborating with a team.
Version control allows us to document the history of a project over time and creating a "time machine" which gives us the ability to jump back and forth through time. Git and GitHub are tools that programmers can use to keep a historical record of those changes to source code files.
- Git is a software that tracks the changes and
- Github is a website that can host files and information about their change history.
Source: ChatGPT3
# see your existing git configuration
git config -l
# configure user name, email, and credentials
git config --global user.name "Your Name"
git config --global user.email “Your Email”
git config --global credential.helper cache
# initialize local git repository
# Creates an empty .git folder in local repository
# This is where git stores all the project information
git init
# pulling remote repository in locally
git clone [url] [localFolder *optional*]
# pushing local repository into remote
git remote add origin https://github.com/username/[repo_name].git
git add fileName
git add -A
git add -all
git add .
This is what creates a tracked version of a file. Each commit will have a unique hash identifier.
git commit -m 'commit message'
# view commit history
git log --oneline
# view status of project files
git status
Files that are not tracked by git or uploaded to GitHub. Create a .gitignore file at the root level of project folder with files or file patterns you want to ignore. Git doesn't track empty folders.
# create ignore file
code .gitignore
# sample contents
.DS_Store
.vscode/authentication.js
node_modules
notes/
**/*-todo.md
# system wide ignore pattern across all repositories
git config --global core.excludesfile [file]
Going back in time to change history helps with avoiding messy commit histories.
# avoid the diverging branch error when changing commit history by force pushing to GitHub
git push -f origin main
Amending allows you to add things to last commit,
# will open text editor for you to add commit message
git commit --amend
# allows you to add commit msg in command
git commit -am 'New commit msg'
# keeps same commit msg
git commit amend --no-edit
Resetting allows you to go back to a previous commit, undo changes, and send those changes back to an unstaged environment.
# soft reset
git reset logHash
# hard reset
git reset --hard logHash
# deleting files managed by git
# also moves deletion event into staging
# takes care of git add
git rm filename
# renaming files
git mv oldfilename newfilename
# moving files
git mv oldpath/filename newpath/filename
Branches let you create different versions of your file(s). This allows you to play around with different versions of your code.
Each Git branch is like an alternate reality that allows you to create alternate versions of a project. Essentially, it is a copy of a project that you can work on without changing the original. You can then syncronize branches or go back and forth between them.
- Head is the actual reality
- Main (Master)
# look at all of the branches in your repository
git branch
In team collaboration, no one messes with the main branch and each teammate has their own working branch.
- Create feature/fix branch
- Make changes on that branch
- Merge branch to master
- Delete branch
You can visualize your branches and respective commit histories by going to the repo home page on Github > Clicking the top Insights tab > and then selecting the Network option on the left nav menu.
# copying a branch (will have same history as current branch)
git switch -c branchName
git checkout -b branchName # older version of switch command
# switching back and forth between branches
git checkout branchName
# pushing to new branch
git push origin branchName
You can protect important branches by setting branch protection rules, which define whether collaborators can delete or force push to the branch and set requirements for any pushes to the branch, such as passing status checks or a linear commit history.
Read about branch protection rules here.
Rebasing takes commit from one branch and applies them to another.
git rebase <branch>|<commit>
# let's you use text editor to make changes
git rebase --interactive <branch>|<commit>
# useful for long commit histories when you only want to go back a few steps
git rebase -i HEAD~#
# lets you see all commits, better when you don't have too many commits
git rebase -i --root
# local delete
git branch --delete branchName
git branch -d branchName # branches must be free of conflicts
git branch -D branchName # forces git to ignore conflicts and deletes branch
# remote delete
git push origin --delete branchName
Version control allows us to document the history of a project over time and creating a "time machine" which gives us the ability to jump back and forth through time.
Configuring GIT
# git config commands
git config --global user.name #your github “Your Name”
git config --global user.email #github email “Your Email”
Initializing Project Folder
# initialize local git repository
# Creates an empty .git folder in local repository
# This is where git stores all the project information
git init
Each Git branch is like an alternate reality that allows you to create alternate versions of a project. Essentially, it is a copy of a project that you can work on without changing the original. You can then syncronize branches or go back and forth between them.
- Head is the actual reality
- Main (Master)
- Working (last commit status)
- Staging (add command - changes queued prior to commit)
- Commit (new log entry with hash)
Staging Files
git add filename
git add --all
git add -A
git add .
Committing Files to History
git commit -m ‘Your comment’
A Git hash is unique identifier for a commit.
Reviewing Commit History
git log
# viewing condensed version (without scroll)
git log --online
Getting File State
git status
Restoring File to a Previous State
git restore --state (staged) filename
git restore . # restores current directory
git checkout . # older version of restore command
Files that are not tracked by git or uploaded to GitHub. Create a .gitignore
file at the root level of project folder with files or file patterns you want to ignore.
.DS_Store
.vscode/authentication.js
node_modules
notes/
**/*-todo.md
Git doesn't track empty folders
Creating global ignore file with a config
variable that captures ignore settings for any new project
git config --global core.excludesfile [file]
# deleting files managed by git
# also moves deletion event into staging
# take care of git add
git rm filename
# renaming files
git mv oldfilename newfilename
# moving files
git mv oldpath/filename newpath/filename
Showing the difference between files
git diff
# look at a specific hash's diff
git diff hash
# escape diff scroll
q
It is difficult to view when there are a lot of changes. In that case better to use VS Code's Source Control feature.
Going back in time to change history. helps with avoiding messy commit histories.
This line of code avoids the diverging branch error when changing commit history by force pushing to GitHub
# force pushing to github
git push -f origin main
Amending allows you to add things to last commit
# will open text editor for you to add commit message
git commit --amend
# allows you to add commit msg in command
git commit -am 'New commit msg'
# keeps same commit msg
git commit amend --no-edit
Resetting allows you to go back to a previous commit. Think of this like a rewind feature that undoes changesm sending them back to an unstaged environment from commited.
git reset logHash
This resets commit history and also modifies files to the state of the commit being reset.
git reset --hard logHash
Rebasing takes commit from one branch and applies them to another.
git rebase <branch>|<commit>
# let's you use text editor to make changes
git rebase --interactive <branch>|<commit>
# useful for long commit histories when you only want to go back a few steps
git rebase -i HEAD~#
# lets you see all commits, better when you don't have too many commits
git rebase -i --root
Branches let you create different versions of your file(s) so you can play around with different versions of your code.
# look at all of the branches in your repository
git branch
# copying a branch (will have same history as current branch)
git switch -c branchName
git checkout -b branchName # older version of switch command
# switching back and forth between branches
git checkout branchName
# pushing to new branch
git push origin branchName
# local delete
git branch --delete branchName
git branch -d branchName # branches must be free of conflicts
git branch -D branchName # forces git to ignore conflicts and deletes branch
# remote delete
git push origin --delete branchName
- Create feature/fix branch
- Make changes on that branch
- Merge branch to master
- Delete branch
In team collaboration, no one messes with the main branch and each teammate has their own working branch.
You can visualize your branches and respective commit histories by going to the repo home page on Github > Clicking the top Insights tab > and then selecting the Network option on the left nav menu.
You can protect important branches by setting branch protection rules, which define whether collaborators can delete or force push to the branch and set requirements for any pushes to the branch, such as passing status checks or a linear commit history.
Read about branch protection rules here.
Merge conflicts happen when you are merging branches but you or somebody else has made changes to the same items in a file on the same branch.
# while on main branch
git merge newBranch
If there have been changes to main branch since last pull while you were working in the new branch, you will get a conflict error message when you try to merge the new branch with main.
In VS Code there is a helper prompt that gives you different options for resolving the error, within the editor window.
Stashing is a way of putting away code temporarily so that you can work on something else. Used when you want to restore file to a previous state to work on something else, but not lose the current modifications you have made.
# puts changes in temporary "storage facility"
git stash
# see what has been stored
# any new stashes go to the top of the list
git stash list
# retrieving the stash, applying stash changes
git stash apply
# retrieving the stash, removing stash from the list
git stash pop
Let's practice stashing . . .