Git Product home page Git Product logo

learning-git-github's Introduction

(testing remote change)

Learning Git and Github

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.

Git Setup

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

Connecting to Remote Git Repository

# 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 Environments

Staging

git add fileName
git add -A
git add -all
git add .

Committing

This is what creates a tracked version of a file. Each commit will have a unique hash identifier.

git commit -m 'commit message'

Tracking

# view commit history
git log --oneline

# view status of project files
git status

Ignoring

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]

Restoring Files to a Previous State

Changing History

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

Amend

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

Reset

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, Renaming, and Moving Files

# 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

Git Branches

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

Developer Collaboration Flow

In team collaboration, no one messes with the main branch and each teammate has their own working branch.

  1. Create feature/fix branch
  2. Make changes on that branch
  3. Merge branch to master
  4. Delete branch

Graph of branch commit history

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.

Creating a 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

Protect Branched

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.

Branch Rebasing

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

Deleting Branch

# 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

Git Differences

Showing the Diff

Merge Conflicts


Version Control

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 Setup

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)

Git Environments

  • 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

Ignoring Files

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, Renaming, and Moving Files

# 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

Differences

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.

Changing History

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

Amend

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

Reset

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

Hard Reset

This resets commit history and also modifies files to the state of the commit being reset.

git reset --hard logHash

Rebasing

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

Git Branches

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

New Branch, Remote Push

# pushing to new branch
git push origin branchName

Deleting Branch

# 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

Git Dev Collab Flow

  1. Create feature/fix branch
  2. Make changes on that branch
  3. Merge branch to master
  4. Delete branch

In team collaboration, no one messes with the main branch and each teammate has their own working branch.

View Graph Commit History

Alt text 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.

GitHub Protected Branches

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

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.

Alt text

Stashing Code

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 . . .

learning-git-github's People

Contributors

stephanie-jones78 avatar

Watchers

Kostas Georgiou avatar  avatar

learning-git-github's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.