A Beginner's Guide - Git Handbook
Due to the most time on my job, we need to type many git command lines. So that this article is used to sort several git commands frequently used for easily searching.
Intorduction
Git is one of a distributed version control system (DVCS) commonly used for open source and commercial software development with significant benefit for individuals, teams and businesses. DVCS allows full access to every file, branches, and interation of project. Unlike once popular centralized version control system, such as perforce, DVCS don't need a constant connection to a central repository. Developers can work anywhere and collaborate asynchronously from any time zone.
Basic Git Commands
Set your Name and Email in Git
Before working with Git on the command line, there are some basic configurations need to be set in advance.
By replacing {YOUR NAME} with your first and last name:
$ git config --global user.name {YOUR NAME}
Then, replaceing {EMAIL} with the email associated with your GitHub account:
$ git config --global user.email {EMAIL}
Now, you can see your current configurations, type:
$ git config --list
Note: The method saves the credentials in plaintext on your terminal. Please use credentials before pull changes.
$ git config --global credential.helper store
Connect an existing progect with github
If you have an existing progect and want to connect with github, you can do the following and start to tract with git.
# Initialize the local directory as a Git repository.
$ git init
# Add the URL for the remote repository where your local repository will be pushed.
$ git remote add origin < repository URL >
# Verifies the new remote URL
$ git remote -v
# Push an existing repository
$ git push -u origin master
High Frequency Git Commands
-
Show the status of the files on your branch
-
Create a new branch
-
Check out to your new branch
-
Join two or more development histories
together
-
Compare the changes
-
Add your file to the staging area
-
See the histroy of commits
-
Commit your file and type the commit message
-
Push your commit to the remote and set a trackng
branch
-
Update your local copy of repository
-
Stash your work
-
Binary search the commit
-
Start with submodules
Git Commands
- Show the status of the files on your branch:
- Create a new branch:
- Check out to your new branch:
- Join two or more development histories together:
- Compare the changes:
- Show you any uncommitted changes since the last commit, type:
- Compare a specific file across branches, type:
- Add your file to the staging area:
- See the histroy of commits:
- Commit your file and type the commit message:
- Push your commit to the remote and set a trackng branch:
- Update your local copy of repository:
- Stash your work:
- Stash uncommitted changes in local repo
- Re-apply your stashed changes
- Manage multiple stashes
- View stash diff
- Binary search to find the commit:
- Use binary search to find the commit
- Start a bisect session
- Bisect reset
- Bisect skip
- Show bisect log
- Starting with submodules:
- Add an existing Git repo as a submodule
- Fetch and merge in the subdirectory
- Show the status of the submodule
- Synchronizes submodules's remote URL configuration setting
$ git status
$ git branch {BRANCH-NAME}
$ git checkout {BRANCH-NAME}
$ git merge {BRANCH-NAME}
# First, let's check out to the "master" branch
$ git checkout master
# Then, merge Dev to current branch
$ git merge Dev
Description: these two syntaxes will replay the changes made on the "Dev" into the current branch "master" since "Dev" is diverged from "master".
Now, you can push a new commit describing the merging change, and hence the current commit is already on the top of "master".
$ git diff
$ git diff {BRANCH-NAME} {OTHER-BRANCH-NAME} {FILE-NAME}
# Preparing to become part of the next commit
$ git add {FILE-NAME}
$ git log
$ git commit -m "your message"
$ git push -u origin {BRANCH-NAME}
$ git pull
$ git stash
# Reappling the changes and removing
$ git stash pop
# Reappling the changes and keeping
$ git stash apply
$ git stash list
$ git stash show -p
$ git bitsect <subcommand> <options>
$ git bitsect start
$ git bitsect reset
$ git bisect skip
$ git bisect log
# good: [72a35907200b42246fd039d495cbef8d80fdefe3] Miscellaneous
git bisect good 72a35907200b42246fd039d495cbef8d80fdefe3
# good: [72a35907200b42246fd039d495cbef8d80fdefe3] Miscellaneous
git bisect good 72a35907200b42246fd039d495cbef8d80fdefe3
# skip: [72a35907200b42246fd039d495cbef8d80fdefe3] Miscellaneous
git bisect skip 72a35907200b42246fd039d495cbef8d80fdefe3
# bad: [72a35907200b42246fd039d495cbef8d80fdefe3] Miscellaneous
git bisect bad 72a35907200b42246fd039d495cbef8d80fdefe3
$ git submodule add
$ git submodule update --remote
$ git submodule status
$ git submodules sync --remote
Reference
Thanks for reading! Feel free to leave the comments below or email to me. Any pieces of advice or discussions are always welcome. :)
Hope this post will help! :)