How to Merge in GIT

  • register modified and untracked files
git add <files-to-add> 
  • Commit changes
git commit -m “commit message”
  • pull changes from origin
git pull --rebase
  • In case, merge conflict appears
git mergetool --tool=opendiff
  • Once merge is complete, save file and complete rebasing
git rebase --continue
  • Push finally
git push

resolve behind revisions

  • Checkout to local-branch
git checkout <local-branch>
E.g.
git checkout master
  • See if it has now ahead/behind revisions differences status
git branch -vv
  • Resolve behind revisions
    • Fetch all to update remote-branch in local cache
   git fetch --all 
    • Merge now remote branch to local branch (fast-forward)
   git merge <remote-branch>
   E.g.
   git merge origin/master
  • pull changes from origin
git pull --rebase
  • In case, merge conflict appears
git mergetool --tool=opendiff
  • Once merge is complete, save file and complete rebasing
git rebase --continue
  • Push finally
git push

* The Content stated above is for informational purpose only. Expert Software Team is not responsible if any part of content found meaningless in any manner or condition.

How to Commit in GIT

Commit code (make sure to add files first which need to be committed)

git commit -m <message>

Commit code to previous commit

git commit --amend
E.g.
git commit -m 'sample commit'
git add forgotten-file
git commit --amend

Undo Add

git reset

Undo Commit

Undo git commit (changes get unstaged i.e. after modification, we need to re-add, re-commit)
git reset HEAD~
Undo git commit (changes remain staged i.e. after modification, we need to re-commit only) -- useful when we need to change only commit message
git reset --soft HEAD~
Above commands are like moving commit pointer to 1 back position. In case, we want to undo multiple commits, we can use like below
git reset HEAD~3 -> * Undoes 3 commits (changes get unstaged)
git reset --soft HEAD~3 -> * Undoes 3 commits (changes remain staged) 

* The Content stated above is for informational purpose only. Expert Software Team is not responsible if any part of content found meaningless in any manner or condition.

What is Stash in GIT

Clear stash list

git stash clear

Show all stashes

git stash list

Drop a stash

Removes latest stash

git stash drop 

Removes given stash name

git stash drop <name-of-stash-to-drop>

* The Content stated above is for informational purpose only. Expert Software Team is not responsible if any part of content found meaningless in any manner or condition.

How to do Undo things done mistakenly in GIT

Remove file added to staging for commit

Undo git add command
git checkout -- <filename>

Discard Changes to added file to staging

git checkout <filename>; i.e. checkout repository's version

Undo last commit

git reset --soft HEAD^
*This command would undo commit, but would keep changes so that we can re-modify and re-commit

Undoing GIT Merge (Not pushed yet)

With git log check which commit is one prior the merge. Then you can reset it using:

1. git log --oneline -n 2
2. git reset --hard <commit_sha_previous_to_merged_one>
OR
git reset --hard HEAD~1

* The Content stated above is for informational purpose only. Expert Software Team is not responsible if any part of content found meaningless in any manner or condition.

How to know the status of files Addition and Deletion in GIT

Status

git status -s

* The Content stated above is for informational purpose only. Expert Software Team is not responsible if any part of content found meaningless in any manner or condition.

How to Delete files from staging area

Delete files

from staging area, local machine, and remote repository

git rm <file-name>

commit now to make changes reflected in local, staging and remote-repository

* The Content stated above is for informational purpose only. Expert Software Team is not responsible if any part of content found meaningless in any manner or condition.

How to Add files to staging area

Add files to staging area

git add <file1> <file2> .... and so on

* The Content stated above is for informational purpose only. Expert Software Team is not responsible if any part of content found meaningless in any manner or condition.

What is GIT ignore

After every update to gitignore, follow below steps (this is useless; if you want just to ignore from git; but still keep in git)

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

First, commit all your changes (required)

# remove first everything

git rm -rf --cached .

# add everything again respecting gitignore file

git add .

git ignore but keep file (USEFUL)

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

— you can update local git repository by running following command. In this case a file is being tracked in the origin repo. You can modify it in your local repo and git will never mark it as changed

git update-index --assume-unchanged <file>

* The Content stated above is for informational purpose only. Expert Software Team is not responsible if any part of content found meaningless in any manner or condition.