GIT: Useful commands
By JoeVu, at: 08:16 Ngày 07 tháng 11 năm 2023
Git is a powerful version control system that helps developers manage their code repositories efficiently. Whether you're a seasoned Git pro or just starting your journey with version control, this guide covers essential Git commands and some handy tips and tricks to improve your Git workflow.
1. Essential Git Commands
Repository
Initializing a Local Git Repository:
git init
In case you already have some existing code, and want to push new changes to a remote/repo
git remote add origin https://github.com/glinteco-com/...
git remote add non_existed_remote https://non_existed_remote.com/glinteco-com/...
git remote remove non_existed_remote # remove a remote
Clone public repository
git clone [repo_url]
Clone private repository
git clone ssh://[email protected]/[username]/[repository-name].git
One problem that many new developers encountered is: cannot clone a private repository that he/she has access to, simply because of ssh-token. A simple solution is to generate a personal token and do:
git clone https://[email protected]/glinteco-com/Samples
[type your token here]
Checking Repository Status
git status
Staging Changes
Add a specific file to the staging area:
git add [file-name]
Add all new and changed files to the staging area:
git add -A
Committing Changes
Commit your staged changes with a descriptive message:
git commit -m "[commit message]"
Removing a File or Folder
Remove a file (or folder) from the repository and working directory:
git rm -r [file-name.txt]
Managing Branches
List all local branches with the active branch marked with an asterisk:
git branch
List all branches, including remote branches:
git branch -a
Create a new branch:
git branch [branch name]
Delete a branch:
git branch -d [branch name]
Forcefully delete a branch:
git branch -D [branch name]
Delete a remote branch:
git push origin --delete [branch name]
Create a new branch and switch to it in one command:
git checkout -b [branch name]
Clone a remote branch and switch to it:
git checkout -b [branch name] origin/[branch name]
Rename a local branch:
git branch -m [old branch name] [new branch name]
Switch to a different branch:
git checkout [branch name]
Switch to the branch you last checked out:
git checkout -
Discard changes made to a file and restore it to its last committed state:
git checkout -- [file-name.txt]
Merge a branch into the active branch:
git merge [branch name]
Merge a source branch into a target branch:
git merge [source branch] [target branch]
Delete a remote branch:
git push origin --delete [branch name]
Rename your current branch
git branch -m [new_name]
Stashing Changes
Stash changes in your working directory to switch branches or work on something else:
git stash
Remove all stashed entries:
git stash clear
Pushing Changes
Push your branch to your remote repository:
git push origin [branch name]
Push changes to a remote repository, associating the branch if needed:
git push -u origin [branch name]
Push changes to a remote repository for a previously associated branch:
git push
Updating and Pulling Changes
Update your local repository to the newest commit from the remote repository:
git pull
Pull changes from a specific branch in the remote repository:
git pull origin [branch name]
Managing Configurations
Set your global Git username and email:
git config --global user.name "joevu"
git config --global user.email "[email protected]"
View your global Git configuration:
git config --global --list
Set your local Git username and email for a specific repository:
git config --local user.name "joevu"
git config --local user.email "[email protected]"
View the local Git configuration for a specific repository:
git config --local --list
2. Git Tips and Tricks
Skipping Git Pre-Commit Hooks
You can skip Git pre-commit hooks using the -n or --no-verify option:
git commit -n -m 'Update' # or git commit -m 'Update' -n
Exploring Git History
View a log of your Git history:
git log
View a summarized Git history with commit statistics:
git log --summary
View a concise, one-line representation of your Git history:
git log --oneline
Previewing Changes before Merging
Compare changes between two branches before merging:
git diff [source branch] [target branch]
Investigating Changes with Git Blame
Determine who made changes to specific lines of code using git blame:
git blame path/to/file
Reverting Commit Changes
Revert a specific commit by its ID:
git revert [commitid]
Advanced Rebase
Rebase your current branch from a specified main branch (e.g., develop):
git rebase -i develop
Comparing Git History
Compare changes between two commits for a specific file:
git diff $start_commit..$end_commit -- path/to/file
Using Git Stash
Stash temporary changes and later pop them back into your working directory:
git stash save
git stash pop
Pick some commits from other branches
Find the git commit hash from other branches that you want to pick using git reflog or git log. Then do:
git cherry-pick hash_value
3. Git Branching Model - nvie's Flow
For a successful Git branching model, consider following Vincent Driessen's nvie's Git Branching Model. This model provides a clear and efficient structure for managing Git branches in your projects, ensuring a smooth workflow for development teams.
The model proposes the following branches:
-
Main Branches:
-
master/main: Represents the production-ready code. Only merge into it from the develop branch when you're ready to release a new version.
-
staging: Represents the staging-ready (uat-ready) code. Only merge into it from the develop branch when you're ready to release a new version.
-
develop: The main integration branch where feature branches merge their changes for testing and preparation for the next release.
-
-
Supporting Branches:
-
Feature branches: These branches are created for new features or bug fixes. They branch off from develop and are merged back when the work is complete.
-
Release branches: Prepare for a new release from develop. Only bug fixes and final adjustments are allowed in this branch.
-
Hotfix branches: Created from master to address critical issues in the production version. Merged back into both master and develop.
-
Vincent Driessen's model simplifies collaboration, release management, and issue tracking in Git projects, making it a valuable reference for teams looking to streamline their development processes.
With these essential Git commands, tips, and an introduction to nvie's Git branching model, you'll have the skills and knowledge to enhance your version control and collaboration efforts, helping you become a more proficient Git user.