Git - Github Tips #
Terminology #
HEAD
: your current local working branchorigin
: the address to your remote git, represent for remote repo- Tracked file: the file git already had before, so when you edit it, git knows this file is modified (
M
files)
- Untracked (new file): the file recently add and git don’t know anything about it (
U
files)
- Remote-tracking branches: References (in the form of
origin/branch-name
) that point to the branches in a remote repository
Commit #
git add .
git commit -m "commit message"
These 2 commands above can combie into 1:
git commit -am "commit message"
Note: this only work with tracked files
Change previous commit message #
Commit amend #
git commit --amend -m "new message to replace the previous message"
Note: this amend
can also simplify by amen
:))))
Rebase reword #
git rebase -i HEAD~1
Vim IDE appear and show a latest commit
- type
i
to begin insert mode, ready to modify - change
pick
tor
orreword
→ means you will change this commit message - press
ESC
key to end insert mode - type
:wq
to save - new Vim IDE appear to let you change the commit message
- change and save like the early steps
git push -f
Opps! Code on wrong branch #
Stash #
git stash
git checkout correct-branch
git stash pop
Note: git stash
will only bring the changes on tracked files to store but don’t worry when checkout to other branch, the untracked files will move to there also
Opps! Commit into local main branch #
Reset #
Solution 1: Erase the current commit and go back to the earlier commit
git reset --hard HEAD~1
Solution 2: Bring the current commit to staged change and go back to the earlier commit
git reset --soft HEAD~1
Update the outdated feature branch #
Relocate branch: rebase #

before rebase

after rebase
git checkout master
git pull
git rebase master topic
git push -f
Note: topic branch will have code from F
and G
of main branch, but if it conflicts with topic branch, the solution will be the same here
Clean up messy commits #
Accumulate commits: rebase fixup #
If you have 3 messy commits per 4 commits on your feature branch
git rebase -i HEAD~4
Vim IDE appear and show 4 latest commits
- Type
i
to change into insert mode - Change
pick
tof
orfixup
→ means you accumulate this 3 commits
- Out insert mode with
ESC
key - Type
:wq
to save
git push -f
before | after |
---|---|
![]() |
![]() |
![]() |
![]() |
Note: the present commit will have all changes from 3 previous commits
Delete all local branches except main branch #
git branch | grep -v "main" | xargs git branch -D
Explain:
- Get all branches (except for the main) via
git branch | grep -v "main"
command - Select every branch with
xargs
command - Delete branch with
git branch -D
Clean up outdated references #
- It only removes remote-tracking branches that no longer exist on the remote
- All local branches you’ve created yourself won’t be affected
git fetch --prune
Refresh outdated local branch #
- If you pull but show some warnings or errors and git show a recommend that is need to type some rebase commands
- Just checkout to another branch, delete your local conflict branch and then checkout to this branch again to download a latest one in remote repo
git checkout dev
git branch -D feature-branch
git fetch
git checkout feature-branch
Ignore all modified and new files and pull the latest version #
# Discard local changes (tracked files)
git reset --hard
# Clean untracked files
git clean -fd
# Pull the latest
git pull
Note: If you’re unsure, stash your changes first so you can recover them later with git stash
Merge PR but get stuck in conflict #
Relocate branch: rebase #
git checkout main
git pull
checkout feature-branch
git rebase main feature-branch
Conflict appears in IDE
→ Resolve conflict and save file
git add .
git rebase --continue
Vim IDE appear to make you confirm change
→:wq
git push -f
Remote origin #
Check remote origin #
git remote -v
Change remote origin #
git remote set-url origin <url>
# E.g
git remote set-url origin git://new.url.here
Remove remote origin #
git remote remove origin
Log pretty #
git log --graph --decorate --oneline
or
git log --graph --decorate
Config #
Show current global credential #
git config --global --list
Configure local repo’s credential #
when you want it’s different with the global one
git config user.name DangPham112000
git config user.email dangpham112000@gmail.com
Switch git user tool #
Connecting to GitHub using SSH keys (Ubuntu) #
Checking for existing SSH keys #
ls -al ~/.ssh
Check the existing of these files:
- id_rsa.pub
- id_ecdsa.pub
- id_ed25519.pub
Generating a new SSH key #
ssh-keygen -t ed25519 -C "dangpham112000@gmail.com"
You can skip all the prompted by Enter
Adding your SSH key to the ssh-agent #
-
Start the ssh-agent in the background
$ eval "$(ssh-agent -s)" > Agent pid 59566
-
Add your SSH private key to the ssh-agent
ssh-add ~/.ssh/id_ed25519
Adding a new SSH key to account #
-
Copy the SSH public key to your clipboard
cat ~/.ssh/id_ed25519.pub
Then select and copy the contents of the
id_ed25519.pub
file displayed in the terminal to your clipboard -
In the upper-right corner of any page on GitHub, click your profile photo, then click Settings
-
In the “Access” section of the sidebar, click SSH and GPG keys
-
Click New SSH key or Add SSH key
-
In the “Title” field, add a descriptive label for the new key. For example, if you’re using a personal laptop, you might call this key “Personal laptop”
-
Select the type of key, either authentication or signing
-
In the “Key” field, paste your public key
-
Click Add SSH key
Testing your SSH connection #
ssh -T git@github.com
You may see a message like this:
Hi DangPham112000! You've successfully authenticated, but GitHub does not provide shell access.
Help improve my blog
Was this page helpful to you?
This page was last modified at 2024-12-31