Useful Git commands

git diff origin/master
git diff master..Prod -- web\en-US\docs\faq.txt

git push --dry-run

git push origin <tag_name>
git push origin --delete <tagname>
git tag -d <local tagname>
git tag -d release/aug2002

git branch --set-upstream-to=origin/master
git restore hello.c
git remote rm origin
#how to add existing git folder to remote
git remote add origin <REMOTE_URL> 
git checkout -b subbranch_of_b1 branch1

git clean -df
git checkout -- .

git reset --hard HEAD~  # if you accidently push your commit to another branch (not current) and don't want to keep it

git pull origin Prod-OS  # then you need to fetch head (pull branch explicitly)

git remote show origin
git rebase origin/develop
git rebase --continue
git rebase --abort
git add/rm <conflicted_files>

#if you get this message - "Your branch and 'origin/develop' have diverged"
git fetch --all
git branch backup-develop
git reset --hard origin/develop
git pull

#if you have local changes when switching branches
git stash mycurrentBranch
git checkout -f anotherBranch

#how to delete a remote branch called "fun_feature"
git checkout master
git branch -d fun_feature
#Delete a remote branch
git push origin --delete remoteBranchName
git branch --all

#undo last commit and don't want to save it for future use
git reset --hard HEAD~1

#revert commit on featured branch and merge with current develop branch
#On branch feature/googleTagManager
git revert --no-commit c99f68fb
git merge develop

#revert most recent commit
git revert c99f68fb..HEAD
git commit

#Always checkout single file from origin to avoid getting old version
#Remember to pull first!!!
git pull
git checkout origin/master web\en-US\docs\faq.txt

#Reset local files to match the remote branch
git reset --hard origin/phobos

#how to find the date a commit is done
git show -s --format=%ci <commit>

#if you get SSL local certificate error, run this:
git config --global http.sslbackend schannel

#stash
git stash
git stash list
git stash apply

#how to delete remote and local branch
git push -d <remote_name> <branchname>
git branch -d <branchname>

#If your editor can exit with an error code -- Git will abort the commit. When using VIM, type
:cq
#to exit with an non-zero error code and abort the commit.

#How to override last local commit message
git commit --amend -m "DCPE-2773"

#How to reset token - Encode your Email Address: • user@example.com becomes user%40exam
git remote set-url origin https://<your_encoded_email>:<your_token>@gitlab.com/<username>/<repository>.git

#If you found your local branch is clean but is not matching remote 
git branch --set-upstream-to=origin/mergeMain mergeMain

#if your local branch and remote branch are diverged, you can reset it to remote (do not rebase)
git reset --hard origin/<branch-name>

#Remove untracked files and directories:
git clean -fd

#User token to clone (here my user id is 75xxxxx)
git clone https://userid:token@gitlab.com/username/repo.git

#How to revert auto-merge when you are using the git merge command
git reset --soft HEAD~1

#How to include token in pull
git remote set-url origin https://<YOUR_GITHUB_USERNAME>:YOUR_PERSONAL_ACCESS_TOKEN>@github.com/<YOUR_REPO>.git

#If you're doing a merge and found conflicts
#You can override the file that has conflicts with either in the current branch (ours) or the incoming branch (theirs)
git checkout --ours <file>
git checkout --theirs <file>

#How to get back the file you deleted but haven't pushed to remote yet
#Find the commit before you delete your file and then check it out
git log -- path/to/your/file
git checkout <commit-hash> -- path/to/your/file

#I tried to commit a change but I was using another account so it failed.
#Need to update the configured email:
git config user.email "YOUR_EMAIL"

#How to find a corresponding commit of Prod on a dev branch
#On prod branch and the exact commit, run the following and you'll get the commit
git merge-base prod dev

#How to rollback to a previous commit, make change to it, and push it to override master
git push origin HEAD:master --force

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a comment