This page helps you to view a diff for a given commit hash. Useful when you want to see specific commits and want to extract and generate a diff in case you lost the commit.
Source: Git – how to view diff for a given commit id – InfoHeap
This page helps you to view a diff for a given commit hash. Useful when you want to see specific commits and want to extract and generate a diff in case you lost the commit.
Source: Git – how to view diff for a given commit id – InfoHeap
This example shows how to revert your git commit.
As an example I have modified README.md file in my website
C:\code\smccake\smccake>git diff
diff --git a/README.md b/README.md
index 44ea578..58e71fc 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
-# smccake
+# smccake test
St. Marys church Cake Project
Committed the code with “git commit command”
C:\code\smccake\smccake>git commit -m "test commit" -a
[main 0c92a65] test commit
1 file changed, 1 insertion(+), 1 deletion(-)
C:\code\smccake\smccake>git log
commit 0c92a659288e78cd277acd57ec5bdb8aec37464e (HEAD -> main)
Author: Yogindar <yogindar@yahoo.com>
Date: Thu Feb 3 16:12:41 2022 +0530
test commit
Note the commit hash id 0c92a659288e78cd277acd57ec5bdb8aec37464e. This is what we used to refer back our commits.
Now we wanted to revert the commit, we can use the “git revert” command with the commit hash.
C:\code\smccake\smccake>git revert 0c92a659288e78cd277acd57ec5bdb8aec37464e
[main 5c26ac1] Revert “test commit”
1 file changed, 1 insertion(+), 1 deletion(-)
You can do a “git push”
I’m going to give you another extra lecture on how to add the reverted changes back.
So we know the commit hash, now generate the diff again using the commit hash.
Create a patch with the diff and apply it.
C:\code\smccake\smccake>git diff “0c92a659288e78cd277acd57ec5bdb8aec37464e^” 0c92a659288e78cd277acd57ec5bdb8aec37464e
git diff “0c92a659288e78cd277acd57ec5bdb8aec37464e^” 0c92a659288e78cd277acd57ec5bdb8aec37464e > changes.patch
diff –git a/README.md b/README.md
index 44ea578..58e71fc 100644
— a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
-# smccake
+# smccake test
St. Marys church Cake Project
C:\code\smccake\smccake>git apply changes.patch
C:\code\smccake\smccake>git diff
diff –git a/README.md b/README.md
index 44ea578..58e71fc 100644
— a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
-# smccake
+# smccake test
St. Marys church Cake Project
Happy giting…:-)
Nice article which guides you well through the fundamentals of Git.
Source: Git How To: Guided Git Tutorial
Whenever you do git pull and you find some files modified locally as part of build and want to overwrite it with the contents in the git repository.
git checkout HEAD^ file/to/overwrite
git pull
To checkout a new git branch and to create a new git branch and checkout
git checkout <branch>
git checkout -b <branch>
To delete a remote branch and local branch
git push –delete origin bugfix/
git branch -d bugfix/
To copy a directory without its history and add as a new folder in git
cp -R old_dir new_dir
git add new_dir
git commit -m "first revision of new_dir's copied files"
Creating a diff file
git diff > 1.patch
Applying a git generated patch file
git apply 1.patch
Saving your changes while checking out a branch
git stash save
if you have to save again
git stash save
stash@{1} – older changes…newest will become stash{0}
git stash list – lists all stash
git stash drop
stash@{0} dropped. stash@{1} will become stash@{0}
git stash apply stash@{2} – to apply changelist in stash{2}
git stash drop stash@{2} – to drop the changelist in stash{2}
git stash pop - To get everything back
git checkout -b newbranch
git stash pop
git stash pop - Restore back to the saved state, but it deletes the stash from the temporary storage.
git stash apply - Restore back to the saved state and leaves the stash list for possible later reuse. Need to use below command to drop them.
git stash drop
git stash apply 1 is equal to git stash@{1} apply
To rename a git local branch
Use -m option to rename a branch. Here I forgot to add bugfix/ prefix int he branch name. So -m command helped me to add it.
git checkout bugfix-branch
git branch -m bugfix/bugfix-branch
To cherry pick a pull request
Create a new branch form the origin branch where you want to add the fix.
git branch cherry-branch origin/master
switch to the new branch
git checkout cherry-branch
Cheery pick the commit using commit id.
Use git log to find the commit id.
git cherry-pick COMMIT-ID
Push the new branch to your fork
git push origin cherry-branch
To Backport changes
To fetch all upstream changes
git fetch –all
create the cherrypick branch from the branch intended to check-in
git branch my_cherrypick_branch rel/release_branch_to_checkin
checkout temporary cherry pick branch
git checkout my_cherrypick_branch
cherry pick the commit
git cherry-pick dc4264056167c7ef9d496b276e9f2699d1b91106
push the cherry pick branch to upstream.
git push –set-upstream origin my_cherrypick_branch
Now create the pull requests.
git lfs uninstall
git pull
git lfs install
git lfs checkout