0% found this document useful (0 votes)
2 views

Git Commands

The document provides a comprehensive guide on setting up a local Git repository, including initializing the repository, staging and committing changes, and pushing to a remote repository on GitHub. It also covers how to clone a repository, pull updates from a remote repository, and manage branches, including creating, switching, and merging branches. Additionally, it emphasizes the importance of pulling changes before pushing to avoid conflicts.

Uploaded by

Suresh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Git Commands

The document provides a comprehensive guide on setting up a local Git repository, including initializing the repository, staging and committing changes, and pushing to a remote repository on GitHub. It also covers how to clone a repository, pull updates from a remote repository, and manage branches, including creating, switching, and merging branches. Additionally, it emphasizes the importance of pulling changes before pushing to avoid conflicts.

Uploaded by

Suresh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

<><><><><><><><>< Setting up a local repository with (Staging ,Committing and

logging ><><><><><><><><><><>

===================================================================

install Git or git bash

go to project directory

open git bash here

before that first you must have github.com account

now we are in our project directory(C:\Users\kaushal_kumar1\IdeaProjects\git-demo)

><>< Let's check whether any local repositories created or not.

$ git status

fatal: not a git repository (or any of the parent directories): .git

**Note- so our code is not in our local repository.

><>< To initialize a local repository


$ git init

Initialized empty Git repository in C:/Users/kaushal_kumar1/IdeaProjects/git-


demo/.git/

><>< Now let's check changes in our code

$ git status

Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
.idea/
pom.xml
src/
** now all files newely created we need to add in staging index. now we are
in working copy we need to add in staging index and then we need to commit to our
local repository.

><>< Add these changes to our staging index

$ git add "src" --if you want to add single file or changes to staging index

$ git add . --if you want to add all the files and changes in staging index

** now our all files or changes are in staging index.

><>< Now We need to commit our changes to a local repository

$ git commit -m "added git-demo project to local repo"

** now it will add our files to our local repository.


><>< Now let's check the status after adding all files to our local repository

$ git status
On branch master
nothing to commit, working tree clean

** now all are committed to our local repository. now let's check the log.

><>< Now let's check the log

$ git log

commit 2294b5782eb1510fe37fbeac5586c011cd4de733 (HEAD -> master) -- checksum


value(HashCode) on each commit generate to identify the changes
Author: Kaushal Kumar <[email protected]>
Date: Thu Feb 22 14:07:31 2024 +0530
added git-demo project to local repo -- comments
we have added

*** now let's do some changes in code and let's commit and let's see the log. i
have added some if condition in our code and commited.

$ git commit -m "added if condition to check the data"

><>< Now let's check the log again after second commit

$ git log

commit e47507a61ebe8e9d8c2a4c63fed6068bf608eb2b (HEAD -> master)


Author: Kaushal Kumar <[email protected]>
Date: Thu Feb 22 14:19:10 2024 +0530

added if condition to check the data

commit 2294b5782eb1510fe37fbeac5586c011cd4de733
Author: Kaushal Kumar <[email protected]>
Date: Thu Feb 22 14:07:31 2024 +0530

added git-demo project to local repo

*** now our code is in our local repository. so this is the way we can push our
changes to a local repository and we can log and track the changes or commit
changes.

<><><><><><><><>< Setup - Creating and Pushing local repository


to remote repositories ><><><><><><><><><><>

===================================================================

** Now we need to move our changes from local repository to remote repository.
** Let's create a Remote repository in github.com first.
Remote Repository Link - https://github.com/kaushal8707/git-demo
we have our local repository chnages in our local machine.
><>< Let's push local repository changes to Remote Repository

** In Project Directory open git bash here

><>< Need to set Local Repository to Remote Repository through Command Line.

$ git remote add origin https://github.com/kaushal8707/git-demo.git

** now we set the origin to our remote repository. now we need to push our changes
now.

><>< Now Let's push the changes now to Remote Repository

$ git push -u origin master

** currently we are working in a master branch as we don't have any sub branch.
bcz default branch is master branch only.

Enumerating objects: 22, done.


Counting objects: 100% (22/22), done.
Delta compression using up to 8 threads
Compressing objects: 100% (12/12), done.
Writing objects: 100% (22/22), 3.20 KiB | 819.00 KiB/s, done.
Total 22 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), done.
To https://github.com/kaushal8707/git-demo.git
* [new branch] master -> master
branch 'master' set up to track 'origin/master'.

** our local changes moved to the remote repo. let's go and check in github.
** whatever we have added to our local it came to a remote one.

<><><><><><><><>< Git checkout/clone and


git update/pull ><><><><><><><><><><>

===================================================================

** let's see how we can checkout the code or how we can take an update/pull.
Earlier we have seen how to move code from local repo to remote repo.
** When u joined any org they provided the link for the repo and asked to do the
local setup let's see how we can do or how you can checkout.

><>< let's create a folder first like "Story Name" or "Workspace" name. i have
given name "MyProject".

><>< go to the workspace and open git bash here. (u have the repo link).

$ git clone https://github.com/kaushal8707/git-demo.git

** whetever the code avails to ur remote repository it came to your local


repository.
** now we have the code we can import to our intellij and we can work on
it. assume few more developer is working on to the same repository. suppose one
developer done some changes and push his changes to the remote repository. so let's
do some changes manually on git remote repo so that using git pull we can take the
update in our local repository.

><>< I was unknowing about the changes done on remote repo. so how that changes
will reflect in my local. so for that
** go to your working directory and open git bash here.
** now we are in our local repository.

$ git pull origin master - we want to pull from origin master branch

$ git pull origin master


remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 8 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (8/8), 1.16 KiB | 41.00 KiB/s, done.
From https://github.com/kaushal8707/git-demo
* branch master -> FETCH_HEAD
e47507a..2417c38 master -> origin/master
Updating e47507a..2417c38
Fast-forward
src/main/java/org/example/Main.java | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

*** to avoid the conflict always before pushing the changes to remote
repository first take the pull.

<><><><><><><><>< Git Branch & Merge


><><><><><><><><><><>

===============================================

** We will see How to create a seperate Branch in git and How we can merged
it to a master branch.

** go to current working directory and open git bash here

><>< Let's check a branch first

$ git branch

* master

-- only one branch is there that is the master branch

><>< Let's create a new branch

$ git branch phase2

-- now phase2 branch is created

><>< Let's check the available branch

$ git branch
* master
phase2

-- we can see we are having 2 branch now. but currently we are in


master branch now. let's move to the phase2 branch.

><>< Let's switch the branch form master to phase2

$ git checkout phase2

Switched to branch 'phase2'

><>< Now let's check the branch

$ git branch

master
* phase2
-- so we moved to our phase2 branch, so we are in phase2 branch.

><>< now let's commit and push our changes to our newly created branch phase2
then we will raise pull REQ and merge

$ git status
$ git add .
$ git status
$ git commit -m "added Constant file"
$ git push origin phase2

-- now i pushed the changes to my newly created branch just go to git


remote repo and verify for newely created branch.

-- if u go to below master link


https://github.com/kaushal8707/git-demo/tree/master u can see

-- phase2 had recent pushes 2 seconds ago and you can see Compare & pull
Request

-- Click on Compare & Pull Request.

-- click on Create a Pull Request.

-- Before Merge just click on File Changes link u can review and give a
comment there.

-- so u can move ur cursors on the chnages u can find + symbol so u just


press + on line where u want to give a review comment.

-- now let's merge a pull request if u want u can add some msg then
confirm merge.

-- u can see msg " Pull request successfully merged and closed "

-- let's verify either changes is reflected or not.

><>< Now we are in a phase2 branch in local machine we have to move to master
and take a pull then only changes will be reflect in master in local machine
and then delete the branch phase2
$ git checkout master

Switched to branch 'master'


Your branch is up to date with 'origin/master'.

$ git branch

* master
phase2

$ git pull
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (1/1), 901 bytes | 81.00 KiB/s, done.
From https://github.com/kaushal8707/git-demo
2417c38..2148240 master -> origin/master
Updating 2417c38..2148240
Fast-forward
src/main/java/org/example/Constant.java | 5 +++++
src/main/java/org/example/Main.java | 4 +++-
2 files changed, 8 insertions(+), 1 deletion(-)
create mode 100644 src/main/java/org/example/Constant.java

$ git push origin --delete phase2

To https://github.com/kaushal8707/git-demo.git
- [deleted] phase2

-- phase2 branch has been deleted

===================================================================================
===========

install Git or git bash


open git bash here
><>< Let's check whether any local repositories created or not.
$ git status

fatal: not a git repository (or any of the parent directories): .git

><>< To initialize a local repository


$ git init

><>< Now let's check changes in our code


$ git status

><>< Add these changes to our staging index


$ git add "src" --if you want to add single file or changes to staging index
$ git add . --if you want to add all the files and changes in staging index

><>< Now We need to commit our changes to a local repository


$ git commit -m "added git-demo project to local repo"

><>< Now let's check the status after adding all files to our local repository
$ git status
><>< Now let's check the log
$ git log

commit 2294b5782eb1510fe37fbeac5586c011cd4de733 (HEAD -> master) -- checksum


value(HashCode) on each commit generate to identify the changes
Author: Kaushal Kumar <[email protected]>
Date: Thu Feb 22 14:07:31 2024 +0530
added git-demo project to local repo -- comments
we have added

$ git commit -m "added if condition to check the data"

><>< Now let's check the log again after second commit
$ git log

commit e47507a61ebe8e9d8c2a4c63fed6068bf608eb2b (HEAD -> master)


Author: Kaushal Kumar <[email protected]>
Date: Thu Feb 22 14:19:10 2024 +0530

added if condition to check the data

commit 2294b5782eb1510fe37fbeac5586c011cd4de733
Author: Kaushal Kumar <[email protected]>
Date: Thu Feb 22 14:07:31 2024 +0530

added git-demo project to local repo

><>< Let's push local repository changes to Remote Repository

><>< Need to set Local Repository to Remote Repository through Command Line.

$ git remote add origin https://github.com/kaushal8707/git-demo.git

><>< Now Let's push the changes now to Remote Repository

$ git push -u origin master

<><><><><><><><>< Git checkout/clone and git update/pull


><><><><><><><><><><>

===================================================================
><>< let's create a folder first like "Story Name" or "Workspace" name. i have
given name "MyProject".

><>< go to the workspace and open git bash here. (u have the repo link).

$ git clone https://github.com/kaushal8707/git-demo.git

><>< I was unknowing about the changes done on remote repo. so how that changes
will reflect in my local. so for that
** go to your working directory and open git bash here.
** now we are in our local repository.

$ git pull origin master - we want to pull from origin master branch
<><><><><><><><>< Git Branch & Merge
><><><><><><><><><><>

===============================================

><>< Let's check a branch first

$ git branch

* master

><>< Let's create a new branch

$ git branch phase2

><>< Let's check the available branch

$ git branch

* master
phase2

><>< Let's switch the branch form master to phase2

$ git checkout phase2

Switched to branch 'phase2'

><>< Now let's check the branch

$ git branch

master
* phase2
-- so we moved to our phase2 branch, so we are in phase2 branch.

><>< now let's commit and push our changes to our newly created branch phase2
then we will raise pull REQ and merge

$ git status
$ git add .
$ git status
$ git commit -m "added Constant file"
$ git push origin phase2

><>< Now we are in a phase2 branch in local machine we have to move to master
and take a pull then only changes will be reflect in master in local machine
and then delete the branch phase2

$ git checkout master

Switched to branch 'master'


Your branch is up to date with 'origin/master'.

$ git branch

* master
phase2
$ git pull

$ git push origin --delete phase2

To https://github.com/kaushal8707/git-demo.git
- [deleted] phase2

-- phase2 branch has been deleted

You might also like