Logo

dev-resources.site

for different kinds of informations.

Git - Useful Commands

Published at
8/30/2024
Categories
git
versioncontrol
gitcommands
github
Author
ryoichihomma
Author
12 person written this
ryoichihomma
open
Git - Useful Commands

Concept Highlights:

  • Differences Between git clone URL and git remote add origin URL
  • Explanation of Flow Commands (git init, git add ., git status, git commit -m "message", git push)
  • Differences Between git log and git log --oneline
  • Explanation of git restore, git checkout, git revert, git mv, and git diff

1. git clone URL vs. git remote add origin URL

1-1. git clone URL
The git clone URL command is used to create a copy of an existing repository. When you run git clone, Git downloads the entire content of the remote repository to the local machine, including all files, branches, and commit history.

e.g.)

git clone https://github.com/user/repository.git
Enter fullscreen mode Exit fullscreen mode

1-2. git remote add origin URL
If you have a local project that you want to upload to a new GitHub repository, you would first create a repository on GitHub, then use this command to link it.

e.g.)

git remote add origin https://github.com/username/repository.git
Enter fullscreen mode Exit fullscreen mode

Key Differences:

  • git clone URL creates a new directory, copies all content from the remote repository, and sets the remote origin.
  • git remote add origin URL adds a remote repository to an existing local repository without creating a new directory or copying files.

2. Explanation of Flow Commands

2-1. git init initializes a new Git repository in the current directory. It creates a .git directory, where all Git tracking information is stored.

2-2. git add . stages all the changes in the working directory for the next commit. The . means "all changes" - you can also specify individual files or directories.

2-3. git status displays the status of the working directory and staging area. It shows which changes have been staged, which haven't, and which files aren't being tracked by Git.

2-4. git commit -m "message" commits the staged changes to the local repository with a message describing what the commit does.

2-5. git push uploads the commits from the local repository to a remote repository. It's used to share the work with other collaborators.

3. git log vs. git log --oneline

3-1. git log command displays detailed commit history such as commit hashes, author information, date, and the commit message.

3-2. git log --oneline displays a simplified, one-line-per-commit view of the history, showing only the first seven characters of the commit hash and the commit message.

Key Differences:

  • git log provides detailed commit history.
  • git log --oneline provides a compact, easy-to-read summary of the commit history.

4. Other commands

4-1. git restore filename command is used to restore working directory files to their last committed state. This command can discard changes in the working directory.

4-2. git checkout -b new-branch command is used to switch or restore working directory files. When used with a branch name, it changes the current branch; when used with a file name, it restores the file.

4-3. git revert commit-hash is used to revert a commit by creating a new commit that undoes the changes of a previous commit, without removing the commit from history.

4-4. git mv oldname.txt newname.txt is used to move or rename a file or directory in the working directory and stage the change for the next commit.

4-5. git diff is used to show the differences between the working directory and the staging area or between commits.

versioncontrol Article's
30 articles in total
Favicon
Do You Care About Commit Messages?
Favicon
GitHub vs. GitLab vs. Bitbucket
Favicon
Git Hosting Services: A Comparison of GitHub, GitLab, Bitbucket, and Azure Repos
Favicon
Mastering Git Repositories: Initialization, Cloning, Remotes, URLs, and Submodules
Favicon
A Comprehensive Guide to All Git Commands for Every Developer
Favicon
Committing Changes in Git: A Complete Guide to Git Add, Commit, Messages, and Amending
Favicon
Mastering Git Branching and Merging: A Complete Guide to Git Branches, Merge, Rebase, and More
Favicon
Git Tags
Favicon
Essential Git Commands Every Developer Should Know
Favicon
Master Git: A Comprehensive Beginner to Advanced Guide
Favicon
Understanding Git: The Ultimate Guide with Practical Examples
Favicon
.git
Favicon
Code Like a Librarian: A Beginner's Guide to Git and GitHub
Favicon
Version Control with Git: How to Master It - Best Practices for Efficient Code Management by Git
Favicon
A Beginner’s Guide to GitHub
Favicon
Effective Git Workflow: Managing Version Control in Team Environments
Favicon
π“π²π©πžπ¬ 𝐨𝐟 π•πžπ«π¬π’π¨π§ 𝐂𝐨𝐧𝐭𝐫𝐨π₯ π’π²π¬π­πžπ¦π¬:
Favicon
Azure DevOps Series - Azure Repos
Favicon
Mastering DevOps Branching: Your Ultimate Guide to Git Flow, Trunk, Tag-Based, and Hybrid Strategies
Favicon
Ultimate Git Basics Cheatsheet [Live Doc]
Favicon
Understanding the Role of Git Tags in Version Control
Favicon
Git - Useful Commands
Favicon
Version Control and Git: Essential Tools for Modern Software Development
Favicon
Ultimate Git Branches & Merging Cheatsheet [Live Doc]
Favicon
Git and GitHub for Beginners: A Comprehensive Step-by-Step Guide
Favicon
Bagaimana cara memindahkan branch utama git dari master menjadi main
Favicon
Version Control with Git and GitHub: The Importance and Effective Usage
Favicon
A Comprehensive Guide to Using Footers in Conventional Commit Messages
Favicon
Version Control Best Practices with Git and GitHub
Favicon
Using interactive rebase in Git

Featured ones: