YonKeenn </>
Journal About Portfolio Research Post

YonKeenn </>

Hello, I'm Jhon Vargas - a Data Scientist & Technical Lead

Curious. Self Learning. Proactive.

Git Learning

9 minutes
August 16, 2022
1744 Words

How to use git? This is a baseline tutorial for using git and github. We run from 0 to create branches, make pull request and solve merge issues.

source:

Basics

How git works:

Working directory:

You create CSS, JS, HTML files for a web server. with $git add$ we pass those files into staging area with $git commit$ we save those files into your local machine as a commit history. It is a save point.

To save your files we save into github, bitbucket, AWS codecommit, Gitlab we use $git push$.

If someone want to contribute or get those files, we use $git pull$.

Git version:

@yonkeenn:->[Personal_Blog]$ git --version
git version 2.30.2

Git help:

@yonkeenn:->[Personal_Blog]$ git --help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone             Clone a repository into a new directory
   init              Create an empty Git repository or reinitialize an existing one

Git configuration: As a first point, we need to configure git with our credentials.

@yonkeenn:->[Personal_Blog]$ git config --global user.name "yonkeenn"
@yonkeenn:->[Personal_Blog]$ git config --global user.email "jhon.vargasr@gmail.com"
@yonkeenn:->[Personal_Blog]$ git config --global color.ui auto
@yonkeenn:->[Personal_Blog]$ git config -l
user.email=jhon.vargasr@gmail.com
user.name=yonkeenn
color.ui=auto

Git repository: Repositories in git contain a collection of files of varios different versions of a project.

To initialize a git repository(project):

@yonkeenn:->[learning-git]$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>
Initialized empty Git repository in /home/yon/Downloads/learning-git/.git/
@yonkeenn:->[learning-git](master)$ 

To remove the git repository:

@yonkeenn:->[learning-git](master)$ 
@yonkeenn:->[learning-git](master)$ ls -la
total 12
drwxr-xr-x 3 yon yon 4096 Jan  5 00:45 .
drwxr-xr-x 4 yon yon 4096 Jan  4 17:09 ..
drwxr-xr-x 7 yon yon 4096 Jan  5 00:48 .git
@yonkeenn:->[learning-git](master)$ rm -rf .git/
@yonkeenn:->[learning-git]$ ls -la
total 8
drwxr-xr-x 2 yon yon 4096 Jan  5 00:52 .
drwxr-xr-x 4 yon yon 4096 Jan  4 17:09 ..
@yonkeenn:->[learning-git]$ 

To verify the status of files:

@yonkeenn:->[learning-git](master)$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)
@yonkeenn:->[learning-git](master)$ touch test.js
@yonkeenn:->[learning-git](master)$ touch index.html
@yonkeenn:->[learning-git](master)$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        index.html
        test.js

nothing added to commit but untracked files present (use "git add" to track)
@yonkeenn:->[learning-git](master)$ 

Add files to staging area:

@yonkeenn:->[learning-git](master)$ git add index.html 
@yonkeenn:->[learning-git](master)$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   index.html

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test.js

@yonkeenn:->[learning-git](master)$ git add . // To add all files to staging area
@yonkeenn:->[learning-git](master)$ ls -ls
total 0
0 -rw-r--r-- 1 yon yon 0 Jan  5 00:55 index.html
0 -rw-r--r-- 1 yon yon 0 Jan  5 11:44 main.css
0 -rw-r--r-- 1 yon yon 0 Jan  5 00:55 test.js
@yonkeenn:->[learning-git](master)$ git add .
@yonkeenn:->[learning-git](master)$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   index.html
        new file:   main.css
        new file:   test.js

@yonkeenn:->[learning-git](master)$ 

Remove files from the staging area:

@yonkeenn:->[learning-git](master)$ git rm --cached index.html 
rm 'index.html'
@yonkeenn:->[learning-git](master)$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        index.html
        test.js

nothing added to commit but untracked files present (use "git add" to track)
@yonkeenn:->[learning-git](master)$
@yonkeenn:->[learning-git](master)$ git rm -r --cached  . // In a recursive way

Now all files are in staging area. Then we have to save files in the commit history:

@yonkeenn:->[learning-git](master)$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   index.html
        new file:   main.css
        new file:   test.js

@yonkeenn:->[learning-git](master)$ git commit -m "starting a project"
[master (root-commit) 68a7671] starting a project
 3 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 index.html
 create mode 100644 main.css
 create mode 100644 test.js
@yonkeenn:->[learning-git](master)$ git status
On branch master
nothing to commit, working tree clean
@yonkeenn:->[learning-git](master)$

Afte execute “git commit -m”, there is nothing to commit now.

git commit -m "" git log git show hash-commit git diff git commit –amend -m ""

jhon@yonyon:->[personalPortafolio]$ git branch -M main
jhon@yonyon:->[personalPortafolio]$ git remot add origin https://github.com/yonkeenn/personalPortafolio.git
jhon@yonyon:->[personalPortafolio]$ git push -u origin main
Enumerating objects: 54, done.
Counting objects: 100% (54/54), done.
Delta compression using up to 8 threads
Compressing objects: 100% (45/45), done.
Writing objects: 100% (54/54), 2.37 MiB | 2.67 MiB/s, done.
Total 54 (delta 0), reused 0 (delta 0)
To https://github.com/yonkeenn/personalPortafolio.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
jhon@yonyon:->[personalPortafolio]$ 

Github

Github is a place where we store our project.

With “git push” we takes a copy from your local machine files and store them in remote server like Github.

Github is a platform for hosting and collaborating on git repositories. People can contribute to any repo or repository.

Create a repository on Github:

Remote repository

For push our files to remote repository, we need to: Add remote repo Configure the main branch Configure public key. The public key can be in ssh or http.

The first time:

git push -u origin main

The next time:

git push git push origin

To pull out files from remote repository to out local machine we use:

Create new file on github Commit this change on github git log to see if these new file is on local machine, it does not. git pull to bring the latest changes from remote repo.

Branches

At the begging we create the master or main branch as the default one:

git branch //for knowing the branch where you are git branch -r //to know the list of remote branches git branch -r //to know all branches

To create a new branch:

git branch name-branch git branch feature-a

To switch new branch: git checkout <existing_branch> git checkout feature-a

To switch previous branch git checkout -

To push files to remote repository: git push -set–upstream <remote_branch> <local_branch> git push -u <remote_branch> <local_branch> git push -u origin feature-a

For deleting a branch: git branch -d

// To switch a branch
git checkout <existing_branch>

// To switch to a destination branch which does not exits.
git checkout -b <new_branch>

// To check current branch
git branch

For switch to another branch

// To switch to main branch
git checkout main

For listing all branches:

// to list all branches
git branch -a

// to list remote branches
git branch -r


For remove branch

// delete branch locally
// -d is for deleting only branch if it has already been pushed and merged with remote branch.
// -D if for forcing delete to branch even if it hasn't been pushed or merged yet.

git branch -d localBranchName

// delete branch remotely
git push origin --delete remoteBranchName

// shorter command to delete a branch remotely
git push origin :fix

// To synchronize your branch list using:
// -p parametrer means prine
git fetch -p

Merge and Pull Request

Localate on main branch:

git merge feature-a // To merge feature-a information into main branch

git pull @yonkeenn:->sqlChallenge$ git pull origin main

Advanced:

source: https://nvie.com/posts/a-successful-git-branching-model/

Dealing with conflicts

Revert commit

source: https://stackoverflow.com/questions/22682870/how-can-i-undo-pushed-commits-using-git

Current commit is C:

A —> B —> C —> -B

For revert this stage, we revert to C

git revert git revert C

If you want to revert the current or last commit:

git revert HEAD

Revert Locally:
// For make sure there is no pending changes
git status
// For check the last commint ID
git log
git log --oneline
// For revert
git revert 98cfeb4
git revert HEAD
// After execute lsat command, we can update the message or just:
:qa!  // used for quit or exit VIM mode
// For check reverted commit
git log --oneline

// Or we can use reset parameter
git reset --hard <COMMIT ID> // git reset --hard 89d44eb
git clean -f -d 
git fetch --all
git push -f


Revert Remotelly:
// For get latest changes from the remote repository
git pull
// For revert the revert commit to remote repository
git push
// For verify the origin is in the same commit of the HEAD
git log --oneline


Set the upstream branch

the “Upstream Branch” is the remote branch in git. So we can track and know if this remote branch is sync or not, it means if there is some commits ahead or not in the remote repository. Upstream branches are closely associated with remote branches.

Upstream branches define the branch tracked on the remote repository by your local remote branch (also called the remote tracking branch)

// -u option is for upstream branch
git push -u <remote> <branch>
// alternative command
$ git push --set-upstream <remote> <branch> 

//To see last commits on branches:

 git branch -vv
  main       89d44eb [origin/main] TODO2: Update btree.h htreepage.h demo.cpp
* todoBranch 40d8ef6 [origin/todoBranch] TODO 6-7-8: callInvoke for template ForEach and FirstThat

// Using Alias:
// HEAD is equivalent to push a remote branch with same name as your current branch

git push -u origin HEAD

// For next time, you only need to do
git push origin
// Because tracking branch is already done.

// For check remote branch which are not created locally on git. 
// -t is used for track
git fetch
git checkout -t <remote_name>/<branch_name>
git checkout --track origin/dev

// So example, let’s say that you have a branch named “remote-branch” on the “origin” remote.
git checkout -t origin/remote-branch

Branch 'remote-branch' set up to track remote branch 'remote-branch' from 'origin'.
Switched to a new branch 'remote-branch'


pstream branches so useful because You get references to your remote repositories and you essentially know if you are ahead of them or not.

Git fetch

git fetch is used in conjunction with git remote , git branch , git checkout , and git reset to update a local repository to the state of a remote. git fetch has similar behavior to git pull, however, git fetch can be considered a safer, nondestructive version.