Git FAQ
Some notes of using of git
Git
- Git official website
- Download Git
- Git documentation
Tip1 - What if we inadvertently use the Reset command and lose large changes?
Sometimes we do these operations:
--
Sometimes even:
--
What if we want to cancel the rollback?
Use
git reflog
Then it shows the reset –hard Logs
389**09 HEAD@{1}: reset: moving to HEAD^^^
d02**f9 HEAD@{2}: reset: moving to HEAD^^
912**c7 HEAD@{3}: reset: moving to HEAD^
e4e**1d (origin/dev) HEAD@{4}: reset: moving to HEAD
e4e**1d (origin/dev) HEAD@{5}: checkout: moving from master to dev
181**f4 (origin/master, master) HEAD@{6}: commit: remove prints
5e9**1d HEAD@{7}: commit (merge): update
634**96 HEAD@{8}: pull origin master: Fast-forward
cde**7c HEAD@{9}: reset: moving to HEAD
cde**7c HEAD@{10}: pull origin master: Fast-forward
389**09 HEAD@{11}: reset: moving to HEAD^^^
d02**f9 HEAD@{12}: reset: moving to HEAD^^
912**c7 HEAD@{13}: reset: moving to HEAD^
Then you can reset to some version by using:
get reset HASH
Tip2 - What if we don’t want some of the submitted records?
Sometimes you may do these things:
$ git add ./
$ git commit -m "add a new module of part 233"
$ git add ./.../..
$ git commit -m "fix bug2, f**ck, sh**t !"
$ git add ./.../..
$ git commit -m "fix some bugs"
$ git add ./../
$ git commit -m "add a new module of part 243"
Because of your upsetting bugs, accidentally leaving a few swear words on the git commit, but this interspersed in the commit record, what should you do?
git branch temp
- paste some logs of commits to the editor
git log # copy text to editor from LATEST commit start to commit end
git reset HASH # to the "add a new module of part 233" one
- chose some commits to recover
git cherry-pick HASH1
git cherry-pick HASH2
....
Finally, the record you don’t need will disappear in your current version.
Tip3 - Some individuals use notes
The following content is some personal notes I recorded
Type |
Command String |
Test |
Introduction |
Config |
git config –global user.name “[name]” |
PASS |
Sets the name you want attached to your commit transactions |
|
git config –global user.email “[email address]” |
PASS |
Sets the email you want attached to your commit transactions |
Init |
git init [project-name] |
PASS |
Creates a new local repository with the specified name |
|
git clone [url] |
PASS |
Downloads a project and its entire version history |
CHANGES |
git status |
PASS |
Lists all new or modified files to be committed |
|
git diff |
PASS |
Shows file differences not yet staged |
|
git add [file] |
PASS |
Snapshots the file in preparation for versioning(. means all files) |
|
git diff –staged |
PASS |
Shows file differences between staging and the last file version |
|
git reset [file] |
PASS |
Unstages the file, but preserve its contents |
|
git commit -m “[descriptive message]” |
PASS |
Records file snapshots permanently in version history |
Branch |
git branch |
PASS |
Lists all local branches in the current repository |
|
git branch [branch-name] |
PASS |
Creates a new branch |
|
git checkout [branch-name] |
PASS |
Switches to the specified branch and updates the working directory |
|
git merge [branch] |
PASS |
Combines the specified branch’s history into the current branch |
|
git branch -d [branch-name] |
PASS |
Deletes the specified branch |
REFACTOR |
git rm [file] |
PASS |
Deletes the file from the working directory and stages the deletion |
|
git rm –cached [file] |
PASS |
Removes the file from version control but preserves the file locally |
|
git mv [file-original] [file-renamed] |
PASS |
Changes the file name and prepares it for commit |
TRACKING |
git ls-files –other –ignored –exclude-standard |
PASS |
Lists all ignored files in this project |
SAVE FRAGMENTS |
git stash |
PASS |
Temporarily stores all modified tracked files |
|
git stash pop |
PASS |
Restores the most recently stashed files |
|
git stash list |
PASS |
Lists all stashed changesets |
|
git stash drop |
PASS |
Discards the most recently stashed changeset |
SYNCHRONIZE CHANGES |
git fetch [bookmark] |
PASS |
Downloads all history from the repository bookmark |
|
git merge [bookmark]/[branch] |
PASS |
Combines bookmark’s branch into current local branch |
|
git push [alias] [branch] |
PASS |
Uploads all local branch commits to GitHub |
|
git pull [remote] [branch] |
PASS |
Downloads bookmark history and incorporates changes |
Tip4 - Set multi github account in One PC
ssh-keygen -t rsa -b 4096 -C "shinepans@live.com (Your Email)"
And it outcomes:
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/shinepans/.ssh/id_rsa): id_rsa_cess (id_rsa_cess:We need to create a new name that is different from the previous file name )
And it outcomes:
Enter passphrase (empty for no passphrase): (Enter)
Enter same passphrase again:(Enter)
Your identification has been saved in id_rsa_cess.
Your public key has been saved in id_rsa_cess.pub.
The key fingerprint is:
SHA256:jgkhEZIjeQNF6CZWU+sIpCUbZBIip+AdgQcZlnXlon0 shinepans@live.com
The key
+---[RSA 4096]----+
|%/#=oo.. |
|^%+=o o |
|*=+ooo . |
|.+..=.. |
|+ o.o ES |
| ..+ |
| o . |
| |
| |
+----[SHA256]-----+
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
Host cess.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_cess
2.Add the new generated SSH key to the associated Github account
Add the newly generated key in id_rsa_cess.pub to the associated Github account.
And Then Use
$ ssh -T git@cess.github.com
to test whether the association is successful. The xxx.github.com used here is the name of the second host in the previous config.
And it should be outcomes:
$ Hi cessuser! You've successfully authenticated, but GitHub does not provide shell access.
3.Use multiple SSH Key
If we need use default account whose ssh key is id_ras.pub, we can use
git clone git@github.com:USER/SOMETHING.git
If we need use another account whose ssh key is id_ras_cess.pub, we can use
“`
git clone git@cess.github.com:USER/SOMETHING.git