Skip to content

alloppi/learning_git

Repository files navigation

This repo was used to learn Git and GitHub (Version Control System)

Version control system keeps track of changes to a group of files. When you have a history of these changes, it lets you find specific versions, compare changes between versions, recover files you may have deleted, or revert files to previous versions.

A distributed version control system means that different users maintain their own repositories of a project, instead of working from one central repository. Users have full file tracking abilities and the project’s complete version history without needing access to a central server or network.

Three Sections of a Git Project

  1. Git directory or : (YOUR-PROJECT-PATH/.git/) is where Git stores everything it needs to accurately track the project. This includes metadata and an object database which includes compressed versions of the project files.
  2. Working directory: Pulls the project’s files from the Git directory’s object database and makes local changes to a project.
  3. Staging area: Cache file that stores information about what will go into your next commit. A commit is when you tell Git to save these staged changes. Git takes a Snapshot of the files as they are and permanently stores that snapshot in the Git directory.

GitHub Workflow

  1. Branching
  2. Commits
  3. Pull Request
  4. Collaborate
  5. Merge or
  6. Rebase

Git Install for Ubuntu

sudo apt update
sudo apt-get install git
git --version
git config --global user.name "xxx"
git config --global user.email "xxx@example.com"
git config --global color.ui true
git config --global core.editor "code --wait"
git config --global diff.tool vscode
git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
git config -l = List config

GitHub SSH access

Setup GitHub SSH sccess GitHub SSH

Working with GitHub projects

I. Create the repository in remote repo (GitHub), then clone it to Ubuntu

  1. Create a directory that save the new repository in GitHub
  2. Clone local copy of the repository
cd [NAME OF DIRECTORY]
git clone [HTTPS REPO ADDRESS (GitHub)]
git remote -v = Check the remote origin
  1. Four steps in a commit: ‘status’ , ‘add’ , ‘commit’ and ‘push’
git status
git add [FILENAME] [FILENAME] [...]
git add *
git commit -m "Add commit message" -m "Add description message"
git remote
git push origin main

II. Start on project locally, create the repository on GitHub and push project to remote

#### Create a new repository in GitHub
mkdir <repos_will_be_inside>
cd <repos_will_be_inside>
echo "# git_test2" >> README.md
git init 
git add README.md
git status
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:alloppi/git_test2.git  /* origin is the alias */
git push -u origin main

Help for Git

git help COMMAND
git COMMAND --help
man git-COMMAND

Git commands

git --version
git --help = contains good Git tutorial insided
git init . = initial git repository
git clone = bring a repository from the remote host (GitHuib) to local machine
git add . = Track all files in current dir and add to staging area
git add -A = Track all files in all dir
git commit = save files in Git, add to commit history
git remote add origin git@github.com:alloppi/git_test2.git = set remote 
git push origin main = Upload Git commit history to remote repo (GitHub) in main branch
git pull = Download changes from remote repo to local machine, opposite to push
git branch = Check any branches in Git (-r for remote, -a for all, - for previous)
git branch feature_1 = Copy commit from current branch to branch feature1 
git branch -m feature_1 feature_2 = Rename branch name from feature_1 to feature_2
git branch -d feature_1 = Delete branch feature_1
git checkout feature_1 = Switch to branch feature_1
git checkout -b feature_1 = Create and switch to feature_1
git status = Check current branch, untracked files and track files
git rm <filename> = Remove file from staging area to unstage
git rm -r --cached . = Recusively remove all files from staging area to unstage
git mv <filename> <newfile> =  Rename file
git log = show all commit history
git log --oneline --graph = show commit history in one line
git log -g = Show Reflog all commit history
git show <hash> = show diff for that commit image
git restore <filename> = discard changes after git commit
git diff = show different current file contenet vs last commmited
git commit --amend -m "Amend commit message after commited" = amend last commited message
git comment --amend --no-edit = Add to last commit
git commit -a -m "update content" = Add and commit together
git rebase -i <hash_id> = Interactive changed commit information
git reset = Undo last commit
git blame <filename> = Show user, time commit the source
git reflog = Show all commit, reset, etc. history

git remote -v
git branch
git commit -am "Modify only"
git ls-files
git rm [FILENAME] [FILENAME] [...]
git mv [NEW_FILE] [OLD_FILE]
git status -s
git diff --staged
git difftool --staged
git log
git log --oneline --reverse
git show d601b90
git show HEAD
git ls-tree HEAD~1
git restore
.gitignore > logs/ main.log *.log

Branching

git checkout -b <Branch> = Create branch <Branch>
git branch <Branch> = Create branch <Branch>
git switch <Branch> = Switch to Branch <Branch>
git checkout main = Switch back to branch main
git diff <Branch> = Difference between current branch & <Branch>
git branch -d <Branch> = Delete branch <Branch>
git branch -a = Check all branches
git push origin <Branch>

Updating & Merging

Detail explanation

git checkout main = Switch to Branch that will be merged i.e. main
git merge <branch> = Merage another branch into active <branch>
git pull = update local repo to latest commit
git merge <Branch> = Merge another branch into active <Branch>
git diff <source_branch> <target_branch> = Preview the different before merging

Tagging

git tag 1.0.0 <commit_id> 

Git Roll back Method 1, Checkout commit, (Virtual Commit, require create new branch and merge into main)

git checkout <filename> = restore the file changes from staged or last HEAD 
git checkout . = restore all files that deleted from staged or last HEAD
git checkout <commit_id> = Roll back to the hash that commit and update to staged area
git checkout HEAD~2 <filename> = Roll back file to HEAD-2 commit and update to staged area
git switch - = Reverse the previous roll back command

Git Roll back Method 2, Revert Commit

git revert <commit_id> = Roll back to the hash that commit

Git Roll back Method 3, Reset Commit (permanent delete all the commits after commit_id)

git reset = Unstage: Undo after git add
get reset -- <file> = Unstage the file
git reset HEAD~1 = Undo the last commit
git reset <commit_id> = 
git reset --hard <commit_id> = Undo the commit to both staging area and working directory

Git Roll Back Method 4

git restore <file> = discard changes in working directory
git restore --staged <file>..." = to unstage

Git Undo Example: Undo last Commit

Detail explanation

git reset HEAD~1 = Undo the last commit to save on staging area OR
git reset HEAD^ = Undo the last commit, ^ means one previous ^^ means 2 previous OR
git reset master^ = Undo the last commit
git reset HEAD~2 = Undo the 2 previous commit
git checkout . = Copy from staging area to working directory

What is HEAD in Git

cat .git/HEAD = Show the head of the branch 

Code review by team and give comment in GitGub

  1. Check every changes
  2. Discuss and comment by team
  3. Resolve conflicts code
  4. Merge & versioning Find more details in GitHub

Branch vs Rebase

Git rebase tutorial

git rebase --interactive <base>

Common Error in GitHub

  1. Use SSH instead of HTTPS if pushing to GitHub have error:
git remote set-url origin git@github.com:username/repo.git

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages