0% found this document useful (0 votes)
3 views

VesionControl_Basic

Version control is a method for managing changes to files and projects, allowing collaboration among multiple developers while tracking revisions and maintaining a history. It offers benefits such as preventing conflicts, facilitating code reviews, and enabling experimentation through branching. The document also covers basic Git commands, the differences between version control systems like Git, SVN, and Mercurial, and best practices for using Git effectively.

Uploaded by

Thanh Pham Minh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

VesionControl_Basic

Version control is a method for managing changes to files and projects, allowing collaboration among multiple developers while tracking revisions and maintaining a history. It offers benefits such as preventing conflicts, facilitating code reviews, and enabling experimentation through branching. The document also covers basic Git commands, the differences between version control systems like Git, SVN, and Mercurial, and best practices for using Git effectively.

Uploaded by

Thanh Pham Minh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Beginner Print

What is version control? Tutorial


Version control is a systematic approach to managing changes made to files and projects over time. It
enables multiple individuals to collaborate effectively by tracking revisions, maintaining a history of
edits, and allowing the recovery of previous versions. This ensures that developers can work on
different features or bug fixes simultaneously without overwriting each other's changes, thus enhancing
productivity and reducing conflicts.
Copy
Initializing a Git repository
git init my-project

Cloning an existing repository


git clone https://github.com/user/repository.git

Checking the status of the repository


git status

Adding changes to the staging area


git add .

Committing changes with a message


git commit -m 'Initial commit'

Pushing changes to a remote repository


git push origin main

Benefits of version control Tutorial


Version control provides numerous benefits that enhance collaboration and productivity in software
development. By allowing multiple developers to work on the same codebase simultaneously, version
control systems help prevent conflicts and data loss. It enables tracking changes over time, allowing
teams to identify when specific features were added or bugs were introduced. Additionally, version
control facilitates code reviews and documentation processes, making it easier to track project
progress and maintain high code quality. With features like branching, teams can experiment on new
ideas without affecting the main codebase, which encourages innovation and faster development
cycles.
Copy
Creating a branch
git checkout -b new-feature

Merging changes
git merge new-feature

Viewing commit history


git log --oneline
Reverting to a previous commit
git revert <commit_hash>

Cloning a repository
git clone https://github.com/user/repo.git

Git vs. SVN vs. Mercurial Tutorial


Git, SVN (Subversion), and Mercurial are popular version control systems that enable developers to
manage changes to code over time. Git is a distributed version control system, allowing multiple
developers to work independently in their own local repositories; changes can be shared with others
through a central repository. SVN, on the other hand, uses a centralized model where all version history
is stored in a single server, making it easier to manage access controls but limiting some flexibility.
Mercurial is also a distributed system like Git, known for its simplicity and performance, but it has a
different command architecture and is often perceived as easier to learn for beginners.
Copy
Git clone
git clone https://github.com/user/repo.git

SVN checkout
svn checkout https://svn.example.com/repo/trunk

Mercurial clone
hg clone https://www.mercurial-scm.org/repo

Basic Git commands: clone, add, commit, push, pull Tutorial


Basic Git commands are essential for managing your repository and tracking changes in your projects.
The 'clone' command allows you to create a local copy of a remote repository. The 'add' command
stages changes for a commit, while 'commit' records those changes along with a message. The 'push'
command is used to upload your local commits to a remote repository, and 'pull' retrieves updates from
the remote repository to keep your local copy synchronized.
Copy
Clone a repository
git clone https://github.com/user/repository.git

Stage changes for commit


git add filename.txt

Commit changes
git commit -m 'Add feature X'

Push changes to remote


git push origin main

Pull changes from remote


git pull origin main
Understanding repositories (local vs. remote) Tutorial
In version control, a repository is a storage space where your project files reside. There are two types of
repositories: local and remote. A local repository is stored on your personal computer, allowing you to
manage and make changes to your files without needing internet access. A remote repository is hosted
on a server (like GitHub, GitLab, or Bitbucket), enabling collaboration with others over the internet.
Changes made in a local repository can be pushed to a remote repository, and changes in a remote
repository can be pulled down to a local repository. Understanding these differences is essential for
effective version control practices.
Copy
Creating a Local Repository
mkdir my-project
cd my-project
git init

Cloning a Remote Repository


git clone https://github.com/username/repo.git

Pushing Changes to Remote Repository


git add .
git commit -m 'my changes'
git push origin main

Pulling Changes from Remote Repository


git pull origin main

advertisement
Creating a new repository Tutorial
Creating a new repository in Git allows you to start tracking files for a new project. This is the first step
in using Git for version control, enabling you to manage changes to your files over time. A repository can
be created locally on your computer or hosted remotely on platforms like GitHub or GitLab. With a new
repository, you can begin adding files, making commits, and collaborating with others.
Copy
Creating a new local repository
mkdir my-project
cd my-project
git init

Cloning an existing remote repository


git clone https://github.com/username/repository.git

Creating a new repository on GitHub (using GitHub CLI)


gh repo create my-project --public

Basic branching with Git Tutorial


Basic branching in Git allows you to create separate lines of development, enabling you to work on
features, bug fixes, or experiments in isolation from the main codebase. This functionality is essential
for maintaining the stability of the main project while still allowing for continuous development. When
you're ready, you can merge these branches back into the main branch.
Copy
Create a New Branch
git checkout -b feature-branch

Switch to an Existing Branch


git checkout main

List All Branches


git branch

Merge a Branch into Main


git checkout main

git merge feature-branch

Delete a Branch
git branch -d feature-branch

Using .gitignore file Tutorial


.gitignore is a file used in Git repositories to specify intentionally untracked files that Git should ignore.
This is particularly useful for excluding files that are not useful for collaboration or are generated during
the development process, such as temporary files, build artifacts, and sensitive configuration files.
Copy
Ignoring specific files
# Ignore a specific file
config.json

Ignoring a directory
# Ignore all files in a directory
logs/

Ignoring file types


# Ignore all .log files
*.log

Ignoring files in multiple directories


# Ignore all .tmp files in any directory
**/*.tmp

Ignoring everything except specific files


# Ignore everything in node_modules except package.json
node_modules/*
!node_modules/package.json

Understanding commits and commit messages Tutorial


Understanding commits and commit messages is crucial in version control as it provides a clear history
of changes made to a repository. A commit represents a snapshot of your project at a particular point in
time, including the changes made and the rationale behind them. Good commit messages describe
what changes were made, why they were necessary, and provide context for future reference, making it
easier for others (and yourself) to understand the project's evolution.
Copy
Simple Commit
git commit -m "Fix typo in README file"

Commit with Multiple Changes


git commit -m "Add feature X and improve performance"

Using Git Commit with a Detailed Message


git commit -m "Refactor authentication module" -m "- Improved password hashing algorithm
- Added email verification"

Amending the Last Commit


git commit --amend -m "Correct previous commit message"

How to view commit history Tutorial


Viewing commit history in Git is essential for understanding the changes made in a repository over time.
It allows you to see what changes were made, who made them, and when they were made, thus
providing context for each modification. The git log command is primarily used to access the commit
history, presenting a chronological list of commits with their IDs, authors, dates, and messages.
Copy
Basic Git Log
git log

One-line Log Output


git log --oneline

Limit Number of Commits


git log -n 5

View Changes in Each Commit


git log -p

Filtering Commits by Author


git log --author='Author Name'

You might also like