Using Git in the Real Life for Beginners 3

Tue, Jun 16, 2020 3-minute read

branch is the basic in the real-life work.

4.Branch Things

we are working on our branch, let’s learn more about branch.

in fact when we use Git, we are creating branch called master.

here is some command to create branch

git branch name // in local

git branch // check local branch
git branch -r // check remote branch
git branch -a// check all branches

delete local branch when your branch has new file havenot been merged, use -D, enable you to delete that branch by force; when you use -d, it will remind you that you have new file that you cannot delete it.

git branch -d name 
git branch -D name // force

when you delte your local branch, and you wanna to delete your remote branch, use push

git push origin :name

or delete remote branch

git push origin --delete [branch-name]
git branch -dr [remote/branch]
fatal: ‘branch’ does not appear to be a git repository
fatal: Could not read from remote repository
Administrator@PC-1 MINGW64 /f/hugo/RileyCode (master)
$ git push origin :master
fatal: 'branch' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Administrator@PC-1 MINGW64 /f/hugo/RileyCode (master)
$ git push origin --delete master
fatal: 'branch' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

First, check that your origin is set by running

git remote -v

This should show you all of the push / fetch remotes for the project.

Verify remote name / address

If this returns showing that you have remotes set, check that the name of the remote matches the remote you are using in your commands.


Administrator@PC-1 MINGW64 /f/hugo/RileyCode (master)
$ git remote -v
main    git@github.com:rileyshen/blog.git (fetch)
main    git@github.com:rileyshen/blog.git (push)
origin  branch (fetch)
origin  branch (push)

Administrator@PC-1 MINGW64 /f/hugo/RileyCode (master)
$ git push main --delete master
To github.com:rileyshen/blog.git
 - [deleted]         master

oh, by the way, you cannot rename your branch.

checkout: you want to swifch to another branch

git checkout name

you want to create a new branch, and swifch to that new one

this new branch base on current branch

git checkout -b newName

☕ ———-☕

Don’t forget to connet your local branch with the remote one:

$ git branch --set-upstream [branch] [remote-branch]

base on commit history create a branch

git checkout <commit> -b newName

Now it’s time for you to merge your branch with master.

git merge newName

There are two models for merging, but we don’t need to choose, git will decide it for us. However, we still need to know the difference.

  1. Fast-Forward
image

Fast-Forward

When the commit of the master branch is your parent of your lastest commit,

In the other way, your coworker has not commit anything yet to the master branch since last time you commit or fetch/pull from it (luck!!!).

  1. Recursive Stratege Merge

But in the real life, this heppens a lot, considering it is a team project

image

Recursive Stratege Merge

So the git would new a commit, point to respectly two branches.

image

Recursive Stratege Merge

Oh, bytheway, it’s quite often get an CONFLICT in branch merge. Nothing to worry about.

So everytime before you push to the remote branch, it’s better fetch/pull from remote first. So what’s the difference between “fetch” and “pull”

My suggestion is use git fetch:

git fetch orign master

It downloads conetend from a remote branch without modifying your local state.

git pull origin master

It likes git fetch + git merge, would modifying your file.

Ok, It’s time for Git 4


Git2