Notes on Git

Version Control System (VCS)

Is a software that helps software developers to work together and maintain a complete history of their work. Git is a VCS software where it supports a distributed version control system, across the network.

Git

Is a free software distributed under the terms of the GNU General Public License version 2. Installed on your computer, Git provides a private workplace or also known as a working copy. These working copy and its versions are part of the repository. Users can carry out actions such as add, remove, rename, move any file(s), then commit the changes.

Git manage changes in the folder .git, stores in the root folder of a git project. Special files that provide instructions to git are named .gitignore and .gitattributes.

For those seeking specific task, here are notes on how to push to a remote repository.

Basic process flow to add or update file(s) can be listed in 5 steps.
  1. Create a git project if not done. Then create or modify a file in the project folder
  2. Add the file to a staging area
  3. Commit the file from a staging area
  4. Push commits to a git repository
  5. Manage branches

1. Create

Configure user details

git config --global user.name "My Name"

git config --global user.email tboxmy@email.address

Create a new git project

git init

Status

git status

git diff

git show <commit_id>

git show HEAD

git show <branchname>

History of activities

git log

git log -p

git log --stat --summary

Git from another project

git clone /home/originalproject myproject

Clone from a remote SSH or HTTPS site

git clone ssh://username@servername:port/repo/originalproject myproject

git clone https://servername/repo/originalproject myproject

2. Edit files to Staging area

Add list of files to a staging area.

Add a file

git add file1.ext

git add file1.ext file2.ext file3.ext

Add all changed files

git add .

List files to be committed

git diff --cached

Search text in files

git grep "searchstring"

Undo adding of file to staging area

git reset HEAD <filename>

Undo a modified file

Retrieve back the last commit of a file. This will replace changes made for that file.

git checkout -- <filename>

Delete a file

Deletes a file from the repository.

git rm <filename>

3. Commit

Permanently store files from staging area.

git commit -m "remarks for commit"

Display commits

git log

Name a commit

git tag <tag_name> <commit_id>

git tag laravel7 c382b0b018dbd53301119636ce0b5770b1958d06

Search text in files of a commit

git grep "searchstring" <tag_name>

Display file of a commit name

git show <commit_name>:<file_name>

Display file for a given commit name

git show laravel7:resources/views/home.blade.php

Add file without creating a new commit

git commit --amend

4. Push

Send files to remote repository

git push origin master

List files changed at remote or local

git fetch origin
git show --name-only master origin/master
git diff --name-only master origin/master --

5. Manage Branches

Create a new branch

git branch development

List branches

git branch

Switch to another branch

git switch development

Delete branch

git branch -d development

Delete branch without validation

git branch -D development


6. Access repository on remote server

Add remote source through shared network drive

git remote add [-t <branch>] [-m <master>] [-f] [--tags|--no-tags] [--mirror=<fetch|push>] <name> <url>

git remote add origin /home/originalproject

Add remote source through SSH

git remote add tboxmy ssh://tboxmy@10.1.1.10:22/repositories/myproject

Remove remote source

git remote remove <name>

git remote remove origin

List remote source

git remote

git remote -v

git config --get remote.origin.url

Example:

Pull files from remote source

This will fetch file(s) from a remote branch, then merge them into the current branch.

git pull origin master

Fetch remote source but do not merge

git fetch origin

Display change of current files with remote source

git log -p origin/originalproject

Delete remote branch

This the dangerous bit, you are warned, NO UNDO function. Better to leave the branch and create new branch.

git push origin --delete branchname


Blog Archive