0% found this document useful (0 votes)
17 views4 pages

Git_Basic

This document provides a comprehensive tutorial on essential Git commands for beginners, including 'git init' for initializing a repository, 'git clone' for copying repositories, and 'git add' for staging changes. It also covers commands for committing changes, checking status, viewing commit history, managing branches, and synchronizing with remote repositories using 'git pull' and 'git push'. Each command is explained with examples to facilitate understanding and practical application.

Uploaded by

Thanh Pham Minh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Git_Basic

This document provides a comprehensive tutorial on essential Git commands for beginners, including 'git init' for initializing a repository, 'git clone' for copying repositories, and 'git add' for staging changes. It also covers commands for committing changes, checking status, viewing commit history, managing branches, and synchronizing with remote repositories using 'git pull' and 'git push'. Each command is explained with examples to facilitate understanding and practical application.

Uploaded by

Thanh Pham Minh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Beginner Print

git init - Initialize a new Git repository. Tutorial


The 'git init' command is used to create a new, empty Git repository or to reinitialize an existing one.
This command sets up the necessary files and directories for Git to track changes in your project,
allowing you to start version control from scratch. It's typically the first command you run in a new
project to initiate tracking with Git.
Copy
Initialize a New Repository
git init my-project

Reinitialize an Existing Repository


cd existing-project && git init

git clone <url> - Clone a repository into a new directory. Tutorial


The 'git clone <url>' command is used to create a copy of an existing Git repository. This command
downloads the repository and its entire version history into a new directory, allowing developers to work
with the code locally. It is the primary way to obtain a repository from a remote server.
Copy
Clone a repository from GitHub
git clone https://github.com/user/repository.git

Clone a private repository using SSH


git clone [email protected]:user/repository.git

Clone with a specific folder name


git clone https://github.com/user/repository.git my-folder

git add <file> - Stage changes to a specified file. Tutorial


The git add <file> command is used to stage changes in a specified file for the next commit in a Git
repository. This command allows users to selectively choose which changes to include in the commit,
effectively enabling them to build commits incrementally by organizing and grouping related changes
together.
Copy
Stage a single file
git add file.txt

Stage multiple files


git add file1.txt file2.txt

Stage all changes in the directory


git add .
Stage all changes to a specific type of file
git add '*.html'

Stage all changes except for a specific file


git add --all :/ && git reset file_to_exclude.txt

git commit -m 'message' - Commit staged changes with a message. Tutorial


The 'git commit -m' command is used to record changes to the repository. When changes are staged
using 'git add', you can finalize these changes and include a descriptive message using the '-m' flag,
which helps in tracking the history of modifications made to the files.
Copy
Commit a simple change
git commit -m 'Fixed typo in README file'

Commit after adding new feature


git commit -m 'Added authentication feature to user login'

Commit with detailed message


git commit -m 'Refactored user service to improve performance and reduce complexity'

Commit after fixing a bug


git commit -m 'Resolved issue with data validation on form submission'

git status - Show the working tree status. Tutorial


The 'git status' command is used to display the state of the working directory and the staging area. It
helps to inform you about which changes have been staged, which haven't, and which files aren’t being
tracked by Git. This is a crucial command to verify the current state of your repository before
committing changes.
Copy
Check Status
git status

Check Status for Specific Path


git status <file-path>

advertisement
git log - View commit history. Tutorial
The 'git log' command is used to view the commit history of a Git repository. It displays a chronological
list of commits made to the repository, showing details such as the commit hash, author, date, and
commit message, which helps developers track changes and understand the evolution of the project.
Copy
Basic Log
git log

One-line Log
git log --oneline

Log with Graph


git log --graph --abbrev-commit --decorate

Log by Author
git log --author='Author Name'

Log with Date Range


git log --since='2023-01-01' --until='2023-12-31'

git branch - List branches in the repository. Tutorial


The 'git branch' command is used to list, create, or delete branches in a Git repository. A branch is a
pointer to a commit, allowing developers to work on different features or fixes in isolation while keeping
the main codebase intact. This command helps in visualizing and managing the branches available in
the repository.
Copy
List All Branches
git branch

List All Remote Branches


git branch -r

List All Local and Remote Branches


git branch -a

Show Current Branch


git branch --show-current

git checkout <branch> - Switch to the specified branch. Tutorial


The 'git checkout <branch>' command is used to switch between branches in a Git repository. This
allows developers to work on different features or fixes in isolation without affecting the main codebase.
When you switch branches, your working directory is updated to reflect the state of that branch,
including any files that are added, modified, or deleted.
Copy
Checkout to an existing branch
git checkout feature-branch

Checkout to the main branch


git checkout main

Create and switch to a new branch


git checkout -b new-feature

View branches before checkout


git branch
git pull - Fetch from and integrate with another repository or a local branch.
Tutorial
The 'git pull' command is used to fetch changes from a remote repository and merge them into the
current branch. This command is a combination of 'git fetch' (which retrieves updates from a remote
repository) and 'git merge' (which integrates those updates into the local branch). It is essential for
keeping your local repository up to date with the latest changes made by others.
Copy
Basic pull from origin
git pull origin main

Pull changes from a specific remote branch


git pull origin feature-branch

Pull and rebase instead of merge


git pull --rebase origin main

git push - Update remote refs along with associated objects. Tutorial
The 'git push' command is used to upload local repository content to a remote repository. This
command updates the remote refs with the local commits, effectively sharing changes made by the
developer with the rest of the team. It is essential for collaboration, as it allows all contributors to
access the latest changes in the project.
Copy
Push to Default Remote Branch
git push

Push to a Specific Remote and Branch


git push origin main

Force Push Changes


git push --force

Push All Branches to Remote


git push --all

Push and Set Upstream Tracking


git push -u origin feature-branch

You might also like