Git Commands
Git Commands
logging ><><><><><><><><><><>
===================================================================
go to project directory
$ git status
fatal: not a git repository (or any of the parent directories): .git
$ 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.
$ 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
$ 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.
$ git log
*** 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.
><>< Now let's check the log again after second commit
$ git log
commit 2294b5782eb1510fe37fbeac5586c011cd4de733
Author: Kaushal Kumar <[email protected]>
Date: Thu Feb 22 14:07:31 2024 +0530
*** 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.
===================================================================
** 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
><>< Need to set Local Repository to Remote Repository through Command Line.
** now we set the origin to our remote repository. now we need to push our changes
now.
** currently we are working in a master branch as we don't have any sub branch.
bcz default branch is master branch only.
** 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.
===================================================================
** 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).
><>< 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
*** to avoid the conflict always before pushing the changes to remote
repository first take the pull.
===============================================
** We will see How to create a seperate Branch in git and How we can merged
it to a master branch.
$ git branch
* master
$ git branch
* master
phase2
$ 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
-- phase2 had recent pushes 2 seconds ago and you can see Compare & pull
Request
-- Before Merge just click on File Changes link u can review and give a
comment there.
-- 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 "
><>< 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
$ 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
To https://github.com/kaushal8707/git-demo.git
- [deleted] phase2
===================================================================================
===========
fatal: not a git repository (or any of the parent directories): .git
><>< Now let's check the status after adding all files to our local repository
$ git status
><>< Now let's check the log
$ git log
><>< Now let's check the log again after second commit
$ git log
commit 2294b5782eb1510fe37fbeac5586c011cd4de733
Author: Kaushal Kumar <[email protected]>
Date: Thu Feb 22 14:07:31 2024 +0530
><>< Need to set Local Repository to Remote Repository through Command Line.
===================================================================
><>< 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).
><>< 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
><><><><><><><><><><>
===============================================
$ git branch
* master
$ git branch
* master
phase2
$ 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 branch
* master
phase2
$ git pull
To https://github.com/kaushal8707/git-demo.git
- [deleted] phase2