Git is a widely used opensource SCM tool.
This page captures various frequently used git operations and the relevant git commands to achieve them
git log --oneline
See the changes done so far to a specific file
git log -- <file-name>
Check the remote branch being tracked by the local branch
git branch -vv
List remote branches
git branch -a
Write/Modify specific actions
Reset the branch to a specific commit (Say release/feature branch)
Care must taken before performing steps as it has the potential to lose the work. This step is irreversible. Usually performed when we want to permanently remove all the recent changes until a specific change.
Lets say that we have committed and pushed few changes. We might want to undo this and no longer want to remember the history of these commits (Refer git revert in case you want to remember)
Step 1: Switch to the branch
git checkout <branchname>
Step 2: Reset the branch to the commit-id
git reset --hard <commit-id>
--hard indicates to overwrite the file in working directory.
<commit-id> - Id to which HEAD should point to post reset
Revert a specific commit from the branch:
This is useful when you want to remove a commit from the repository. Git shall create a new commit by removing the changes from the commit-id and merging it with latest content
Step 1: Switch to the branch
git checkout <branchname>
Step 2: Revert the branch to the commit-id
git revert <commit-id>
<commit-id> - Id to which HEAD should point to post reset
Step 3: Push the changes
git push origin HEAD:<name of the remote branch>
Checkout a new local branch from a remote branch and track it
git branch --track <name-of-local-branch> origin/<remote-branch>
Create/share/apply patch
Perform the below steps on the sender machine.
Assume that you have few commits in private branch. Switch to the private/local branch from which the patch needs to be generated
git checkout <private-branch>
Generate the patch. <reference-branch> could be the remote branch like master from which the local branch is based.
git format-patch <reference-branch> --stdout > myDiff.patch
The new patch file created could be shared via email.
Perform the below steps on the receiver machine
The below command shows the stats about what it'll do. You can as well do this with your favorite editor.
git apply --stat myDiff.patch
Verify the cleanliness of the patch using the below command
git apply --check myDiff.patch
Finally, apply the patch as
git am --signoff < myDiff.patch
This page captures various frequently used git operations and the relevant git commands to achieve them
Read/View specific actions
View the log oneline
git log --oneline
See the changes done so far to a specific file
git log -- <file-name>
Check the remote branch being tracked by the local branch
git branch -vv
List remote branches
git branch -a
Write/Modify specific actions
Reset the branch to a specific commit (Say release/feature branch)
Care must taken before performing steps as it has the potential to lose the work. This step is irreversible. Usually performed when we want to permanently remove all the recent changes until a specific change.
Lets say that we have committed and pushed few changes. We might want to undo this and no longer want to remember the history of these commits (Refer git revert in case you want to remember)
Step 1: Switch to the branch
git checkout <branchname>
Step 2: Reset the branch to the commit-id
git reset --hard <commit-id>
--hard indicates to overwrite the file in working directory.
<commit-id> - Id to which HEAD should point to post reset
Revert a specific commit from the branch:
This is useful when you want to remove a commit from the repository. Git shall create a new commit by removing the changes from the commit-id and merging it with latest content
Step 1: Switch to the branch
git checkout <branchname>
Step 2: Revert the branch to the commit-id
git revert <commit-id>
<commit-id> - Id to which HEAD should point to post reset
Step 3: Push the changes
git push origin HEAD:<name of the remote branch>
Checkout a new local branch from a remote branch and track it
git branch --track <name-of-local-branch> origin/<remote-branch>
Create/share/apply patch
Perform the below steps on the sender machine.
Assume that you have few commits in private branch. Switch to the private/local branch from which the patch needs to be generated
git checkout <private-branch>
Generate the patch. <reference-branch> could be the remote branch like master from which the local branch is based.
git format-patch <reference-branch> --stdout > myDiff.patch
The new patch file created could be shared via email.
Perform the below steps on the receiver machine
The below command shows the stats about what it'll do. You can as well do this with your favorite editor.
git apply --stat myDiff.patch
Verify the cleanliness of the patch using the below command
git apply --check myDiff.patch
Finally, apply the patch as
git am --signoff < myDiff.patch