Git Managing branches

Managing Git Branches

A single Git repository can maintain multiple branches of development. To create a new branch named “adding-shopping-branch”, use

$ git branch adding-shopping-branch

If you now run

$ git branch

you’ll get a list of all existing branches:


  adding-shopping-branch
* master

The “adding-shopping-branch” branch is the one you just created, and the “master” branch is a default branch that was created for you automatically. The asterisk marks the branch you are currently on; type

$ git checkout adding-shopping-branch

to switch to the adding-shopping-branch branch. Now edit a file, commit the change, and switch back to the master branch:

(edit file)

$ git commit -a
$ git checkout master

Check that the change you made is no longer visible, since it was made on the adding-shopping-branch branch and you’re back on the master branch.

You can make a different change on the master branch:

(edit file)

$ git commit -a

at this point the two branches have diverged, with different changes made in each. To merge the changes made in adding-shopping-branch into master, run

$ git merge adding-shopping-branch

If the changes don’t conflict, you’re done. If there are conflicts, markers will be left in the problematic files showing the conflict;

$ git diff

will show this. Once you’ve edited the files to resolve the conflicts,

$ git commit -a

will commit the result of the merge. Finally,

$ gitk

will show a nice graphical representation of the resulting history.

At this point you could delete the adding-shopping-branch branch with

$ git branch -d adding-shopping-branch

This command ensures that the changes in the adding-shopping-branch branch are already in the current branch.

If you develop on a branch crazy-idea, then regret it, you can always delete the branch with

$ git branch -D crazy-idea

Branches are cheap and easy, so this is a good way to try something out.

Merge Branches

1. Create branch
2. Add Files
3. Checkout Master
4. Merge to Master from new branch

1. git checkout -b day_1
2. git commit & git push
3. git checkout Master
4. git merge day_1

Adding New Branch and Files


C:\Train\GitDemos>git branch
* master

C:\Train\GitDemos>git branch adding-shopping-branch

C:\Train\GitDemos>git branch
  adding-shopping-branch
* master

C:\Train\GitDemos>git checkout adding-shopping-branch
Switched to branch 'adding-shopping-branch'

C:\Train\GitDemos>git branch
* adding-shopping-branch
  master

C:\Train\GitDemos>git add index.html style.css

C:\Train\GitDemos>git status
On branch adding-shopping-branch
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        new file:   index.html
        new file:   style.css


C:\Train\GitDemos>git commit index.html style.css -m "Added File in New Branch"
[adding-shopping-branch 322fbfb] Added File in New Branch


C:\Train\GitDemos>git status
On branch adding-shopping-branch
nothing to commit, working tree clean

C:\Train\GitDemos>git remote add adding-shopping-branch https://github.com/kprabhanew/GitDemo.git

C:\Train\GitDemos>git push -u adding-shopping-branch
Username for 'https://github.com': ****@gmail.com
Password for 'https://*****@gmail.com@github.com':

Edit Files in New Branch And Commit in Branch


C:\Train\GitDemos>git status
On branch adding-shopping-branch
Your branch is up to date with 'adding-shopping-branch/adding-shopping-branch'.

Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        modified:   index.html
        modified:   style.css

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   index.html

C:\Train\GitDemos>git add index.html

C:\Train\GitDemos>git status
On branch adding-shopping-branch
Your branch is up to date with 'adding-shopping-branch/adding-shopping-branch'.

Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        modified:   index.html
        modified:   style.css

C:\Train\GitDemos>git commit -m "adding-shopping-branch"

C:\Train\GitDemos>git push -u origin adding-shopping-branch
Username for 'https://github.com': ****@gmail.com
Password for 'https://*****@gmail.com@github.com':

To merge from branch to master

Run the below command


C:\Train\GitDemos>git checkout master
Switched to master branch 

C:\Train\GitDemos>git merge adding-shopping-branch
Auto-merging style.css
CONFLICT (add/add): Merge conflict in style.css
Auto-merging index.html
CONFLICT (add/add): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

Check the changes in master file symboled by “>>>>>” or “<<<<”

For more details please refer this Click Me

To see the confilct in UI run the below command

C:\Train\GitDemos>gitk

And do the necessary changes in master, then


C:\Train\GitDemos>git add .

C:\Train\GitDemos>git commit -m "Resolved confilct"
[master be653ae] Resolved confilct

C:\Train\GitDemos>git push -u origin master
Username for 'https://github.com': ***@gmail.com
Password for 'https://***@github.com':

Pull Request to get changes from Github to local machine folder

Create a pull request for ‘adding-shopping-branch’ on GitHub

C:\Train\GitDemos>git pull adding-shopping-branch

How do I delete a Git branch both locally and remotely?

Executive Summary


$ git push --delete  
$ git branch -d 

Note that in most cases the remote name is origin.

Delete Local Branch

To delete the local branch use one of the following:


$ git branch -d branch_name
$ git branch -D branch_name

Note: The -d option is an alias for –delete, which only deletes the branch if it has already been fully merged in its upstream branch. You could also use -D, which is an alias for –delete –force, which deletes the branch “irrespective of its merged status.” [Source: man git-branch]

Delete Remote Branch

you can delete a remote branch using


$ git push  --delete 

git fetch --all --prune

Example


C:\Train\GitDemos>git branch -D adding-shopping-branch
Deleted branch adding-shopping-branch (was 684d726).

C:\Train\GitDemos>git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

C:\Train\GitDemos>git push https://github.com/kprabhanew/GitDemo.git  --delete adding-shopping-branch
Username for 'https://github.com': *****
Password for 'https://kprabhanew@github.com':
How to clone a specific branch in git
git clone -b  

F:\2020Projects\xyzSERVER>git clone -b xyz https://github.com/xyz/xzy.git


F:\2020Projects\xyzSERVER>git status
On branch xyz
Your branch is up to date with 'origin/xyz'.

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   Readme.md
        modified:   model/staff.js
        modified:   resolvers/staff.js
        modified:   schema/staff.js

Untracked files:
  (use "git add ..." to include in what will be committed)
        .babelrc
        .env

no changes added to commit (use "git add" and/or "git commit -a")

F:\2020Projects\xyzSERVER>git add .

F:\2020Projects\xyzSERVER>git status
On branch xyz
Your branch is up to date with 'origin/xyz'.

Changes to be committed:
  (use "git restore --staged ..." to unstage)
        new file:   .babelrc
        new file:   .env
        modified:   Readme.md
        modified:   model/staff.js
        modified:   resolvers/staff.js
        modified:   schema/staff.js


F:\2020Projects\xyzSERVER>git commit -m "Update Delete For BreakTimings and Tim
ings"
[xyz 716e025] Update Delete For BreakTimings and Timings
 6 files changed, 835 insertions(+), 187 deletions(-)
 create mode 100644 .babelrc
 create mode 100644 .env
 rewrite Readme.md (75%)
 rewrite model/staff.js (73%)
 rewrite resolvers/staff.js (80%)
 rewrite schema/staff.js (61%)

F:\2020Projects\xyzSERVER>git push
Username for 'https://github.com': prabakaran@***.com
Password for 'https://prabakaran@***.in@github.com':
Enumerating objects: 19, done.
Counting objects: 100% (19/19), done.
Delta compression using up to 2 threads
Compressing objects: 100% (11/11), done.
Writing objects: 100% (11/11), 4.76 KiB | 1.59 MiB/s, done.
Total 11 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To https://github.com/****/xyzSERVER.git
   316d783..716e025  xyz -> xyz

F:\2020Projects\xyzSERVER>

Git Repository Config File Changes

User Name
Email
Markdown Online Editor
Markdown Online Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *