Real time scenarios on GIT

git clone https://github.com/beingsagir/play-java-spring-data-jpa.git
git add -all : add all files new and modified locally

git add -A && git commit -m “Comment formatting, 24 hour cache loader, api/contact.”
add all files new and modified locally, commit them locally.

How to see what has changed before actually doing commit

# show differences between index and working tree
# that is, changes you haven't staged to commit
git diff [filename]
# show differences between current commit and index
# that is, what you're about to commit
git diff --cached [filename]
# show differences between current commit and working tree
git diff HEAD [filename]

FIRST PUSH SEQUENCE

git status
git diff
git status
git pull
git stash
git pull
git stash list
git apply stash@{0}
git stash list
git stash apply stash@{0}
git push -u origin master
git status
git push -u origin master
exit
cd /home/user/projects/******-server
git status
ls
get status
git status
git log
git push -u origin master
git push -u origin master git_tracing=2
clear
git push -u origin master

SECOND PUSH SEQUENCE

git status
git add –all
git status
git add -all -v
git add –all -v
git add -i
git status
git stash list
clear
git add -A && git commit -m “Comment formatting, 24 hour cache loader, api/contact.”
history
git add -A && git commit -m “Resolved Conflicts in conf/routes”
git push -u origin master
git status
git status remote
git status remote origin
clear
git remote show origin
cd /home/user/projects/******-server
git remote show origin

git pull

remote: Counting objects: 8, done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 8 (delta 6), reused 0 (delta 0)
Unpacking objects: 100% (8/8), done.
From https://gitlab.com/*****/*****-server
ecc5a5f..32ce420  master     -> origin/master
Auto-merging conf/routes
CONFLICT (content): Merge conflict in conf/routes
Automatic merge failed; fix conflicts and then commit the result.

git status

In case of CONFLICTS resolve conflicts, first, then do a push.

To resolve conflicts, first determine the files under conflict, by issuing git status or

git log –merge -p conf/routes

Resolve your conflicts in any editor or iDEA

git add -A && git commit -m “Resolved Conflicts in conf/routes” : Perform a local commit

git push -u origin master

git config merge.conflictstyle diff3
git status
git diff
git status
git diff
git status
git diff app/controllers/users/UserController.java
git diff HEAD app/controllers/users/UserController.java
clear
git status

git add -A && git commit -m “Comment formatting, 24 hour cache loader, api/contact.”

before doing a push check always ensure to do a pull

********************************************************************

STASHING

git pull
676  git stash –help
677  git log
678  git stash list
679  git stash save “Tests included for BadRequest, Exception Handling in fetchstatesforcountries”
680  git show
681  clear
682  git stash list

user@sys204:~/projects/datacaptive-server$ git stash list
stash@{0}: On master: Tests included for BadRequest, Exception Handling in fetchstatesforcountries
stash@{1}: WIP on master: 8829572 API Contracts, to return JSON responses. sabhi
683  git pull
684  git apply stash@{0}
685  git stash apply stash@{0}

************************************************************

Determine your remote repository

user@user-HP-Compaq-8100-Elite-SFF-PC:~/projects/*****-server-1$ git config --get remote.origin.url
https://gitlab.com/***********ivedevs/*****-server.git

git remote show origin

—————————-

In certain cases you need to checkout a branch from a remote

git clone https://github.com/matthiasn/sse-chat.git –branch 130707-kibana-demo

 

*******************************************************

git log –graph –oneline –decorate –all

In case of comparing what your local has and what has been done on remote, it is first required to fetch the references of your remote on to your local

user@sys204:~/projects/******-server$ git fetch
Username for ‘https://gitlab.com’: ******@******.com
Password for ‘https://******@******.@*****.com’:
remote: Counting objects: 14, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 14 (delta 8), reused 0 (delta 0)
Unpacking objects: 100% (14/14), done.
From https://gitlab.com/*********/*****
7950246..666039a  master     -> origin/master
user@sys204:~/projects/********-server$

 

After doing a fetch, issuing a log would still result in local changes and not depicting remote changes.

user@sys204:~/projects/******-server$ git log

user@sys204:~/projects/******-server$ git log -all : displays the fetch references and the local refs

user@sys204:~/projects/******-server$ git log origin/master : displays the fetch references and the local refs

 

****************************************

 

 

At times there might be ignored files which would have been indexed and committed to the repo, and later realized that the files should not have been there.

Your .gitignore is working, but it still tracks the files because they were already in the index.

To stop this you have to do : git rm -r --cached .idea/

When you commit the .idea/ directory will be removed from your git repository and the following commits will ignore the .idea/ directory.

PS: You could use .idea/ instead of .idea/* to ignore a directory. You can find more info about the patterns on the .gitignore man page.

 

*******************************************************************************

Roll back a git pull over a local repo:

git pull is a composition of git fetch and git merge. While fetch does retrieve only the refs, the merge is where the local repo files are merged with remote file. To undo a pull we could only git merge –abort, this command would undo the merge operation after the pull.

Rollback/Undo a commit

Undoing a commit is a little scary if you don’t know how it works. But it’s actually amazingly easy if you do understand.

Say you have this, where C is your HEAD and (F) is the state of your files.

   (F)
A-B-C
    ↑
  master

You want to nuke commit C and never see it again. You do this:

git reset --hard HEAD~1

The result is:

 (F)
A-B
  ↑
master

Now B is the HEAD. Because you used --hard, your files are reset to their state at commit B.

Ah, but suppose commit C wasn’t a disaster, but just a bit off. You want to undo the commit but keep your changes for a bit of editing before you do a better commit. Starting again from here, with C as your HEAD:

   (F)
A-B-C
    ↑
  master

You can do this, leaving off the --hard:

git reset HEAD~1

In this case the result is:

   (F)
A-B-C
  ↑
master

In both cases, HEAD is just a pointer to the latest commit. When you do a git reset HEAD~1, you tell Git to move the HEAD pointer back one commit. But (unless you use --hard) you leave your files as they were. So now git status shows the changes you had checked into C. You haven’t lost a thing!

For the lightest touch, you can even undo your commit but leave your files and your index:

git reset --soft HEAD~1

This not only leaves your files alone, it even leaves your index alone. When you do git status, you’ll see that the same files are in the index as before. In fact, right after this command, you could do git commit and you’d be redoing the same commit you just had.

One more thing: Suppose you destroy a commit as in the first example, but then discover you needed it after all? Tough luck, right?

Nope, there’s still a way to get it back. Type git reflog and you’ll see a list of (partial) commit shas that you’ve moved around in. Find the commit you destroyed, and do this:

git checkout -b someNewBranchName shaYouDestroyed

You’ve now resurrected that commit. Commits don’t actually get destroyed in Git for some 90 days, so you can usually go back and rescue one you didn’t mean to get rid of.