• Mon. Oct 7th, 2024

Working with Git

What is Version Control System?

VCS helps to manage the changes in files. Each and every change will be maintained in repository, which can be referred at any point of time

Why Version Control System?

  • Combined work task
  • Storing all version
    • Not only saves changes, but also maintains version of each packages
  • What exactly changed and when changed

Version Control Tools

Centralized

  • Sub Version
  • PTC Integrity
  • CVS
  • Clear Case

Distributed

  • GiT
  • Mercurial

GiT and GitHub

GitHub provides space for storing repository

Advantages of Distributed System

  1. Serves as local back up
    • When clone the repository, a complete copy of project files will be stored as repository
    • Entire development history will be available in local repository
  2. Speed
    • Internet is required only when making push or pull request
  3. Branching
    • Master hold production code
  4. Reliable
    • Even when server crashes, the local repository holds all versions and could be easily restored
  5. Secure
    • Convert commit objects to SHA1 encoding
  6. Licensing
    • Basic operation doesn’t cost any license and under GNU licensing

GiT Operations

OperationsCommand
Initializing commandsInit
Clone
Config
ignore
To make changesStatus
Add
rm
Commit
Stash
To Synchronize ChangesAdd Orgin
tag
Push
Pull
Log
Revert
Parallel DevelopmentBranch
checkout
Merge
Rebase
Advance ConceptsPlumbing
Porcelain
Submodules

GiT Commands

Create Central Repository

Central repository could be on GitHub or GitLab or enterprise server

Create local repository

When the project is not available in repository yet and created newly in local repository

Git init

To copy back the project from local to remote repository

Git remote add orgin “https://xxxx”

When project exist in GitHub or GitLab and requires to be synchronized,

Git clone <https://xxxx>

Ignore list

To ignore the list of extensions that requires not be added to repository

.gitignore

Staging

To add the modified file to repository, it requires to be added to stages

Git add abc.py (to add selected files)
Git add . (to add all files to stages)

To store the changes in local repository

Commit the staged files with description. On commit it will be saved to local repository

Git commit –m “Commit to record changes in local repository”

Remove files

To remove the files from repository

Git rm abc.py (remove selected file)
Git rm . (remove all files)

Status

To show the current status of repository. Mainly to know what’s the status of files, whether its committed or staged

Git status

Difference

To show the difference of added member by diff launch tool

Git diff

Push

Changes that are committed are resided in local repository. So to pull the changes to global repository, it requires Push operation to be performed

Git push origin master
Git push orgin –tags è Pushes tagged element from local to remote

Pull

To get others push, pull is required. This will leads to synchronize files or merge with existing files

Git pull origin master

Fetch

Fetch command is used to extract the file from remote repository to local repository. Unlike pull, here merge will not be taken care. It just fetches the file and copy to local directory

Git fetch orgin

Create Branch

  • Branching is simply a copy of master. Mainly used for parallel development
  • Every branch is pointing to head revision
  • Once the work in branch is done, they are merged to master
Git branch <new branch name>

Show branches

To show list of branches and master name visible

Git branch

Checkout branch

To navigate to newly created branch. Once navigated, the modification done will be applicable only on newly created branch

Git checkout <new branch name>

Member added in branch will not be get reflected in mainline and vice versa.

Checkout Master

To switch back from branch to mainline

Git checkout master

Members added in branch will not be visible when switch to master.

Merge

Merge branch and master. Members in branch will be automatically copied to master

Git merge <new branch name”

Merge on branch

When merge needs to done between branch and master. Usually when developer is working on two different branches

Git checkout <new_branch>
Git merge master <new branch>   or git rebase master

Delete branch

If any changes in done and not committed and to inform this will option “-d”. the “-D” represent the permanent deletion

Git branch –d <new branch name>
Git branch –D <new branch name” {permanent delete will not notify any local uncommit changes}

Combination of Branch and Check point

Command shall invoke both creation of new branch and activation of branch in single step

Git checkout –b new_branch

Stash

Working for new future and postponing the commit. Similar to defer in Integrity,

Git stash – All files are moved to staging area now. No uncommitted changes files will be shown
Git stash list – Shows whether any stash is listed
Git stash show – Shows uncommitted files
Git stash apply <stash ID> - To apply stash
Git stash drop <stash ID> - Remove stash based on Stash ID
Git status – Shows all uncommitted stage files
Stash will not submit the entry to local DB, so commit needs to be called
Git stash pop  - To remove the stash which is applied last
Git stash clear – to clear the stash

Log

Shows the commit log. More information about Author, Date, SHA checksum

Git log
Git log –author=”sathish”
Git log –before=”21-05-2016”

Revert

Helps to revert back to previous commit

Git log –oneline   # Gives last commit hash
Git revert <hash value>  # revert the last commit
Git revert head # Points to head

Rebase

Project gets branched when more branches are created. So, to merge all the changes from branch to master and make the changes in branches as master copy the rebase is used. When command used, all branches will be moved and head revision of branch files are copied to master. This provide linear sequence in project history

Git branch <new_branch>
Git checkout <new_branch>
Create file under new branch or modify existing file
Git add .
Git commit –m “rebase”
Git rebase master  # This make the commit made in branch to master

Conflict in Rebase

When conflict occurs while rebasing, it has to be fixed. The conflict might be files that are available in other branch and not this branch (pull needed), change in content of binary and text files (merge)

Git pull –rebase orgin master # This will provide list that are conflicted
Git status  # To view the status of members
Git mergetool è Modify the changes via tool and save it
Git rebase –contine # This will continue the rebase from where conflict stops
Git push origin master # Conflict and merged files are pushed to master repo

Interactive Rebase

  • Helps to clean up messed history
  • Should never be used on public branch ie., master
  • When rebase is applied on master, then possibility of all files in branch will be moved to branch. This will be of high risk as master content will be moved to branch and linearly arranged

Rebase Local Cleanup

When set of commit version needs to be linearly arranged then rebase on Head could be done. Ex., when almost 4 branches are available in branch and when all 4 needs to be merged to 1, the rebase on head shall be applied

Git rebase –i head ~ 3

Adding Tag

Tag is a kind of label, that can be applied in commit

Git tag –a<annotation> --m <message>  # Unique name
Git tag  => To get Tag ID

Submodule

To share the GiT project from one repository to another repository

  • The source project which chosen for sharing should be Git Repository
  • Could be added via git submodule
  • To add recursive sub project, it has to be chosen while adding
  • The submodule create a file “.gitmodules” that contains URL path
  • The submodule create a file of name it has been shared that contains only SHA value
  • Changes done in Source needs to be pushed
  • Then from destination the changes are require to be pulled. Once detected the change, the submodule file will contain additional SHA Check value

Git LFS (Large File Storage)

Git LFS is used for storing the files of larger sized. This can be identified and filtered automatically based on Extension added. This could be defined by the attribute “.gitattributes”

This shall be enabled via menu Settings -> General -> Visibility, Project Features and Permissions -> Git Large File Storage

  1. Now clone the project
  2. Execute the command “git lfs track “<file extension>” ” eg.,  git lfs track “*.zip”   to LFS zip files
  3. You could see .gitattributes files have been generated or updated with your entry 
  4. Just add the .gitattributes file into project (commit and push to repo)
  5. Now add the large file. Then commit and push
  6. Just just look the file in GitLab repository

You could see it as LFS enabled.

Via Artifactory

To make use of Artifactory, disable the internal LFS .  This shall be disabled via menu Settings -> General -> Visibility, Project Features and Permissions -> Git Large File Storage

  • Now clone the project
  • Execute the command “git lfs track “<file extension>” ” eg.,  git lfs track “*.zip”   to LFS zip files
  • Now additionally you need to create a file “.lfsconfig” at same location as “.gitattributes” and add the below lines to it,
[lfs]
    url = "http://git_lfs:AP4qLzxGRQvBUrZkUCoafgH9spm@artifactory/api/lfs/Git_LFS_Artifactory"
  • Now checkin this file too in Git project (commit and push)
  • Now add the large file. Then commit and push
  • Just just look at the file in the GitLab repository. You could see only values
  • Now just check in Artifactory under the Artifact “Git_LFS_Artifactory”, you could see your repo. This will be unstructured format

Pros and Cons

  • With Git LFS, you could directly download the LFS file (selected file), but via Artifactory, you could download only as a whole clone of the project (but could download directly in Artifactory)
  • With Artifactory we have replication, which means person who cloning could use chennai Artifactory, just by modifying the lfs url as below,
[lfs]
    url = "http://git_lfs:AP91Kea176yHnzW1xW4cWgbe5tc@artifactory/api/lfs/Git_LFS_Artifactory"
  • This helps the download or upload to be much faster. No need to wait hours for files to get downloaded. Once added in any one location, it will be automatically taken care of in replicating across sites.

Loading

Bookmark (0)

No account yet? Register

Views: 21

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x