Git for version control Tips

Version controls provide a faster and more manageable control on different versions of codes and documents. Git and Subversion are popular Version Control Systems on Linux and MS Windows. Git files are put in queue, then added to a repository.

The file .gitignore is use to determine which files will not be included in the version control of a folder.
Here are common commands on git

Add files to local repository

To create a local git. Change to the folder containing the source codes or create a new folder.
git init

Copy projects from github repository. Here we copy framework7 to a localfolder f7example
git clone https://github.com/tboxmy/helloworld.git example

Copy with a username
git clone https://tboxmy@github.com/tboxmy/helloworld.git example

Add filenames to queue files for version control.
git add helloworld.html

Add all filenames for version control
git add --all

Display status of changes and which branch is being used.
git status

Apply the files to the repository.
git commit -m "These are the initial files"

Check user settings
git config --list
git config user.name

Edit your username and email settings
git config --global user.name tboxmy
git config --global user.email tboxmy@gmail.com

Display current log
git log --decorate
git log --pretty=format:"%h - %an, %ar : %s"
git log  --after="2018-01-2T17:08:30+0800"      \
           --before="2018-02-2T17:08:30+0800" --pretty=fuller


Limit log to the last 2 entries
git log --decorate -2

To view changes between files and in the version control. There are several options available
git diff
git diff --staged
git diff --cached
git diff --stat

Working on Branch

To create a new branch
git branch somethingnew

Switch to that branch
git checkout somethingnew

Or create a new branch while switching to the new branch
git checkout -b somethingnew

Merge changes to the Master branch
git checkout master
git merge somethingnew

Delete a branch
git branch -d somethingnew

Editing a file

Edit a file from the queue. This file have not been made to commit into the repository.
git reset HEAD helloworld.html

Reset file back to the previous version in queue.
git checkout helloworld.html

Remote repositories

List remote repositories
git remote -v

Display remote in use
git remote
git remote show origin 

Add a remote repository
git remote add helloworld git:github.com/tboxmy/hello.git

Retrieve files from remote repository
git remote -v
git fetch helloworld

Send files to remote repository. Need to specify the remote name and a branch
git push origin master



References

https://git-scm.com/book/en/v1/Git-Basics-Viewing-the-Commit-History

No comments:

Blog Archive