Install Git in Linux:

apt-get install git

Commit on demo_sept Branch:

git checkout demo_sept (switch branch)

git branch (check current branch)

git branch -b new_branch (To create a new branch and then push the branch in origin if want to keep a record of this branch)

git branch -d delete_branch (To delete the new branch on local)

git status (status of working copy)

git diff home/file1.php home/file2.php > diff (store diff in diff file)

vi diff (view diff)

git add home/file1.php home/file2.php (add files for commit)

git commit -m “my test commit” (commit added files with a message)

git commit -a -m “my test commit” (add and commit files with together)

git push origin demo_sept (push committed changes to branch demo_sept)

If some error comes then

git fetch (fetch data from the server)

git rebase origin/demo_sept (rebase fetch change on a local working branch)

git pull –rebase (fetch and rebase together)

git checkout — apps/config/Logger.php (It will revert all changes in a specific file)

git stash (Move all the working copy changes to stash)

git stash pop (It will pop all stashed changes to working copy)

Merged Commit changes of one branch into another branch:

git branch (check current branch from where we have to merge)

git status (check status of current branch)

git stash (Stash local changes)

git status

git checkout master (switch to the branch where we have to merge)

git branch (check branch switched or not)

git cherry-pick 6268d2d2e40a6965d7yh56r41810057c5f3e1cb6 5322120a0ed2e5fc6acnhbgft2e78087e3ae23c42 (cherry-pick all the commits that we

want to merge)

git cherry-pick -x <sha-of-commit>

-x : When recording the commit, append a line that says “(cherry picked from commit …)” to the original commit message in order to indicate which commit this change was cherry-picked from. This is done only for cherry picks without conflicts.

git cherry-pick -nx <sha-of-commit> (get committed changes on local as a modified file status, not as a new commit)

git status (check status)

git push origin master ( Push changes on the master branch)

If an error comes:

git fetch origin (fetch data from the origin)

git rebase origin/master (rebase to current branch)

git push origin master (Now again push)

git checkout demo_sept (again switch to working branch)

git branch

git status

git stash pop

Other Commands:

git reset –hard HEAD^ (undo last local commit and all changes)

git reset –hard HEAD~1 (undo last local commit and all changes)

git reset –soft HEAD~1 (undo last local commit and get all changes on local)

git revert commit_hash (To revert the specific commit)

git log (check new commit for revert changes)

git log -2 ( To show the log of the last 2 activities)

git show commit_hash (check changes in particular commit)

now push revert commit to the server

remove git commit and keep changes on local:

git reset HEAD~1 –soft

for eg: I have done 3 commits on my local working git repository and now I want to remove those commits from the repository and also want to keep those changes.

so we will pass parameter soft not to remove changes and we will use one carrot symbol for one commit, in the above eg. we use 3 symbols because we want to remove three commits.

The local branch is not getting tracked from remote:

git branch –set-upstream-to=origin/<branch_name>

Link one local repository from an existing remote to another empty remote repository while maintaining the history of commits:

git remote rm origin

git remote add origin https://github.com/piyush-singhal/new_repo.git

git push -u origin master

Create a diff raw file or patch between two commits.

git diff 0da94be  59ff30c > my.patch

Here 0da94be is the start commit and 59ff30c is the end commit.

To apply those changes on the working copy.

git apply my.patch