Git Tips

0. Remove a git commit which has not been pushed

First checkout your branch (for my case master branch):

git checkout master
Then reset to remote HEAD^ (it’ll remove all your local changes), force clean and pull:

git reset HEAD^ –hard && git clean -df && git pull

Get specific branch from git : git branch –set-upstream-to=origin/04

1. Error : git push

remote: You must verify your email address.
remote: See https://github.com/settings/emails.
fatal: unable to access ‘https://github.com/xxx/xxxx.git/’: The requested URL returned error: 403

1.Solution :
1. Try : git remote rm origin
2. git remote add origin https://xxx@github.com/xxx/xxx.git
3. Git push

If Error appear like : git push
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use

git push –set-upstream origin main

Solution :
git push –set-upstream origin main

2. Remove a git commit which has not been pushed

IF you have NOT pushed your changes to remote

git reset HEAD~1
Check if the working copy is clean by git status.

ELSE you have pushed your changes to remote

git revert HEAD
This command will revert/remove the local commits/change and then you can push
check the git status now

To restore : git restore –staged .

3. git reset –hard will not remove untracked files, where as git-clean will remove any files from the tracked root directory that are not under Git tracking.

git clean -df
git clean -xdf CAUTION! This will also delete ignored files

-d deletes all files in directories recursively

-f : If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f or -i. Git will refuse to modify untracked nested git repositories (directories with a .git subdirectory) unless a second -f is given.

-x Don’t use standard ignore rules but ones specified by -e. This can be used to start a clean build.

4.
1. To revert the latest commit and discard changes in the committed file do: git reset –hard HEAD~1

2. To revert the latest commit but retain the local changes (on disk) do: git reset –soft HEAD~1
This (the later command) will take you to the state you would have been if you did git add.

If you want to unstage the files after that, do : git reset

3. Undo commit and unstage all files: git reset HEAD~ :

Now you can make more changes before adding and then committing again.

5. How to exit git log or git diff [duplicate]
Type q to exit this screen.

git –no-pager log –oneline -n 10
–no-pager will encounter the (END) word
-n 10 will show only the last 10 commits
–oneline will show the commit message, ignore the author, date information

6. How to get back to the latest commit after checking out a previous commit?
git checkout branchname
git checkout –

=======

—-

GIT

Before a commit use check by this
git diff
git diff
git reset –hard (discard al changes)
git rm –cached (remove a file from git tracking)

To unstage :
git reset HEAD

# MERGE

1. Branch-child has 2 commit
2. Master does not have it
3. To bring the branch changes to Master
4. (current branch) git merge
5. Those stwo commit will be pushed
6. Now we can delete child branch
(master)git branch -d
# Suppose you create a new branch, do some changes/add file (or) edit file , pushed changes to commit, now you go back to master branch, now you try to delete the new branch by this command
git branch -d
it will throw error . We cant delete it.
To delete that we need to do force delete(“D”)
git branch -D

# Compare two Branches
-(New Branch) git diff master
Go to your Active branch
based on your present branch and execute command you can see green/red
-To see diff of two commits
(master) git diff ‘xyzxxx’ ‘pqrxxx’
git diff
new added lines will be displayed as ‘greeen color’

#if you are ok to loose changes , when you switch to other branch
git checkout –force master

#GIT STASH

git stash save
git status
git diff
git stash list
git stash apply stash@{0}”stash_id_lst”
-last changes will be in ‘0’
-you switch any branch and come back to the specific branch, we can get the last stashed changes
git stash pop

=> to clear stash list
git stash clear
git stash list

=> To remove specific stash from list
git stash list
git stash drop stash@{}

# To get git config details
git config –list

# To get git remote details
git remote -v

# To go to last commit status
git checkout . [discarding changes]

# Staging : if you create a file in diretive before to commit, it will be in staging ahead (green color), to remove from it
git rm –cached

# Back to previous commits
(master) git checkout

#To go back to latest
(xyz) git checkout master

# How do i checkout a remote Git branch
1. we can see the branch with
git branch -react-native
2. if the remote is single, then
git fetch (fetch all of the remote branches)
git branch -v -a (you can see branches available for checkout)
remotes/origin/ (read only copies of the remote branches)

-To work on a branch, you need to create a local branch from it
git swtich
3. If the remote is multiple
git fetch origin
git branch -v -a
git switch -e test origin/test

#To see the list of branches
git branch -r

#How do ftech all git branches
git fetch –all
git pull –all

to see branch list “git branch -r”

# Filter commits
git log –author=’jon’
git shortlog -s
git –no-pager log–reverse

Leave a Reply

Your email address will not be published. Required fields are marked *