1. 程式人生 > >Git問答;Git使用;Git回退reset --hard;Git刪除某些commits記錄保留原始碼;Git多賬戶

Git問答;Git使用;Git回退reset --hard;Git刪除某些commits記錄保留原始碼;Git多賬戶

Git FAQ

Some notes of using of git

Git

Tip1 - 撤銷回退,git reset –hard 取消

Sometimes we do these operations:

git reset --hard HEAD^^^

Sometimes even:

git reset --hard HEAD^^^^^

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 - 取消一些commits,但是別的留下?

Sometimes you may do these things:

$ git add ./
$ git commit -m "add a new module of part 233" # add 982 lines
$ git add ./.../..
$ git commit -m "fix bug2, f**ck, sh**t !"  # add 162 lines, wrong changes, no need
$ git add ./.../..
$ git commit -m "fix some bugs"  # add 34 lines, This is the right changes
$ git add ./../
$ git commit -m "add a new module of part 243"  # add 378 lines

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?

  • add a temp branch
git branch temp
  • paste some logs of commits to the editor
git log # copy text to editor from LATEST commit start to commit end
  • reset commits
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 - 一些個人筆記

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 - 同一電腦多github賬戶倉庫

1.Open the git bash and input the codes:

 ssh-keygen -t rsa -b 4096 -C "[email protected] (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 [email protected]
The key's randomart image is:
+---[RSA 4096]----+
|%/#=oo..         |
|^%+=o o          |
|*=+ooo .         |
|.+..=..          |
|+  o.o ES        |
|     ..+         |
|      o .        |
|                 |
|                 |
+----[SHA256]-----+

2.Configure the ~/.ssh/config file

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 [email protected].com:USER/SOMETHING.git

If we need use another account whose ssh key is id_ras_cess.pub, we can use

“`
git clone [email protected]:USER/SOMETHING.git