View Branches#
A: View Local Branches
Use the git branch
command, as follows: git branch
The branch marked with * is the branch you are currently on.
B: View Remote Branches
The command is as follows: git branch -r
C: View All Branches
The command is as follows: git branch -a
- Create a New Branch Locally
The command is as follows: git branch [branch name]
For example: git branch plateformSuperdog
- Switch to the New Branch
The command is as follows: git checkout [branch name]
For example: git checkout plateformSuperdog
- Create and Switch Branch
The command is as follows: git checkout -b [branch name]
For example: git checkout -b plateform2
In this case, git checkout -b [branch name]
is equivalent to two steps:
git branch [branch name]
git checkout [branch name]
- Push the New Branch to GitHub
The command is as follows: git push origin [branch name]
- Delete Local Branch
The command is as follows: git branch -d [branch name]
- Delete Remote Branch on GitHub
The command is as follows: git push origin :[branch name]
In this case, the colon before the branch name represents deletion.
For example: git push origin :plateform2
-
Commit Local Code to New Branch in Git
-
Switch to the new branch
The command is as follows: git checkout [branch name]
For example: git checkout plateform2
- Add the code to be committed locally
The command is as follows: git add .
- Commit the local code
The command is as follows: git commit -m "modification description"
- Push to the Git repository
The command is as follows: git push origin [branch name]
For example: git push origin plateform2
Summary:#
- Clone the repository:
git clone [ssh repository address]
//cd into the repository
cd folder name (cloned folder)
- Copy the code to be committed to the repository (cloned folder)
//Create a branch
git branch branch name
//Push the branch to the remote repository
git push origin branch name
//Switch to the desired branch
git checkout branch name
//Commit the code to the desired branch
git add ./
git commit -m "code committed"
git push
//or
git push origin branch name
Note: This requires binding a GitHub account.
Merge Branches#
https://blog.csdn.net/chenyao1994/article/details/114686758
Git Merge Branch (Easy to Understand)
Suppose we are on the dev branch and have just finished developing a project. We execute the following commands:
git add .
git commit -m 'commit message'
git push -u origin dev
To merge the dev branch into the master branch, follow these steps:
- Switch to the master branch
git checkout master
- If it is a collaborative development, pull the code from the remote master
git pull origin master
//If you are the only developer, there is no need. For safety, it is recommended to pull.
- Merge the code from the dev branch into the master branch
git merge dev
- Check the status and execute the commit command
git status
On branch master
Your branch is ahead of 'origin/master' by 12 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
//This means you have 12 commits that need to be pushed to the remote master.
> Finally, execute the following commit command:
git push origin master
- Other commands
Update the list of remote branches
git remote update origin --prune
View all branches
git branch -a
Delete the remote branch Chapater6
git push origin --delete Chapater6
Delete the local branch Chapater6
git branch -d Chapater6