Git common commands use

 
Overview:
     Workspace: workspace, the parent directory of the .git directory, the directory where the file is located
     Staging library: index, an area in the .git directory, used to record the changes of all workspaces and temporarily store them (ie: git add commits changes to git, but does not commit to branches)
     Current branch: repository, an area in the .git directory, local repository, stored version
 
One arrangement
git config --global user.email "sl"
git config --global user.name "sl"
 
Two local warehouses
 
1) Create a repository
 
    Create a directory locally, enter the directory, and call git init to create a local warehouse. After completion, the .git directory will appear in the folder.
[root @ gittest ~] # mkdir gittest
[root @ gittest ~] # cd gittest /
[root @ gittest gittest] # git init
Initialized empty Git repository in /root/gittest/.git/
 
2) Add files and submit
(a) Add and modify files in the workspace and edit and modify them directly
echo ’testgit’ >> README.md
 
(b) Add to the staging repository (index): git add
git add README.md
 
(c) View the status of the workspace: git status
[root @ gittest gittest] # git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#    new file:   README.md
#
 
(d) delete add: git rm --cached 
[root @ gittest gittest] # git rm --cached README.md
rm 'README.md'
[root @ gittest gittest] # git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    README.md
nothing added to commit but untracked files present (use "git add" to track)
 
(e) Commit: git commit 
[root @ gittest gittest] # git add README.md
[root @ gittest gittest] # git commit README.md
[master (root-commit) fdfa139] first commit
1 file changed, 1 insertion(+)
create mode 100644 README.md
 
    During the period, the vi editor will appear, add a comment, wq can be
 
3) The workspace rolls back to the last commit or add modification: git checkout -- 
[root @ gittest gittest] # cat README.md
gittest
[root@gittest gittest]# echo 'test redo' >> README.md
[root @ gittest gittest] # cat README.md
gittest
test redo
[root @ gittest gittest] # git checkout - README.md
[root @ gittest gittest] # cat README.md
gittest
 
Note: There are two cases of cancellation:
    (a) The modification has not been put into the staging area, now, undo it back to the state of the repository
    (b) After the modification has been added to the temporary storage area, the modification has been made. Now, undo the modification and return to the state after being added to the temporary storage area.
 
Note 2: -- must be included, otherwise it is a switch branch command
 
4) Roll back to a committed version: git reset --hard version number
 
Note: The version can be replaced by HEAD: the previous version is HEAD^, the previous version is HEAD^^, of course, it is easier to write 100 ^ for the previous 100 versions, so it is written as HEAD~100
 
(a) View the command operation log: git reflog
[root @ gittest gittest] # git reflog
3ecc7a9 HEAD@{0}: commit: add date
fdfa139 HEAD@{1}: commit (initial): first commit
 
(b) fallback
[root@gittest gittest]# git reset --hard fdfa139
HEAD is now at fdfa139 first commit
[root @ gittest gittest] # git reflog
fdfa139 HEAD@{0}: reset: moving to fdfa139
3ecc7a9 HEAD@{1}: commit: add date
fdfa139 HEAD@{2}: commit (initial): first commit
 
5) Delete file commit: git rm
After the file is deleted in the workspace, you need to notify git to delete it in the warehouse, the method git rm 
 
[root @ gittest gittest] # rm delete.file
rm: remove regular file 'delete.file'? y
[root @ gittest gittest] # git status
# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    deleted:    delete.file
#
no changes added to commit (use "git add" and/or "git commit -a")
[root @ gittest gittest] # git rm delete.file
rm 'delete.file'
[root @ gittest gittest] # git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    deleted:    delete.file
#
[root @ gittest gittest] # git commit
[master 184c5b3]  Please enter the commit message for your changes. Lines starting
1 file changed, 1 deletion(-)
delete mode 100644 delete.file
[root @ gittest gittest] # git status
# On branch master
nothing to commit, working directory clean
 
Three remote warehouse operations
 
Create a project on gitlib, and the relevant commands will be automatically prompted after completion:
 
1) clone remote warehouse
git clone [email protected]:xxx/gittest.git
cd gittest
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
 
2) Associate remote
      关联:git remote add origin git@server-name:path/repo-name.git;
      Commit all branches: git push -u origin master
cd existing_folder
git init
git remote add origin [email protected]:xxx/gittest.git 
git add .
git commit
git push -u origin master 
 
    Configuration ssh method is omitted;
 
3) Pull the latest data from the remote: git fetch origin master
 
four branches
1) Create a branch: git branch dev
 
2) Switch branch: git chechout dev
[root@gittest gittest]# git checkout dev
Switched to branch 'dev'
 
3) View branch: git branch
[root @ gittest gittest] # git branch
* dev
  master
 
4) Merge branch to trunk: git merge dev
Switch to master: git checkout master, git marge dev
[root @ gittest gittest] # git branch
* dev
  master
[root@gittest gittest]# echo '20170729' >> README.md
[root @ gittest gittest] # git commit README.md
[dev 405d48e] add date
1 file changed, 1 insertion(+)
[root @ gittest gittest] # git checkout master
Switched to branch 'master'
[root @ gittest gittest] # git branch
  dev
* master
[root @ gittest gittest] # cat README.md
gittest
[root @ gittest gittest] # git merge dev
Updating 184c5b3..405d48e
Fast-forward
README.md | 1 +
1 file changed, 1 insertion(+)
[root @ gittest gittest] # cat README.md
gittest
20170729
 
5) Conflict resolution
[root @ gittest gittest] # git merge dev
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
[root @ gittest gittest] # git status
# On branch master
# You have unmerged paths.
#   (fix conflicts and run "git commit")
#
# Unmerged paths:
#   (use "git add <file>..." to mark resolution)
#
#    both modified:      README.md
#
no changes added to commit (use "git add" and/or "git commit -a")
 
(a) Edit the conflict file to resolve manually
[root @ gittest gittest] # vi README.md
gittest
<<<<<<< HEAD
201707291632
=======
201707291631
>>>>>>> dev
 
(b) Commit: git add + git commit
[root @ gittest gittest] # git status
# On branch master
# You have unmerged paths.
#   (fix conflicts and run "git commit")
#
# Unmerged paths:
#   (use "git add <file>..." to mark resolution)
#
#    both modified:      README.md
#
no changes added to commit (use "git add" and/or "git commit -a")
[root @ gittest gittest] # git add README.md
[root @ gittest gittest] # git commit README.md
fatal: cannot do a partial commit during a merge.
[root @ gittest gittest] # git commit -i README.md
[master f3cf6f9] Merge branch 'dev'
 
注:出现error: fatal: cannot do a partial commit during a merge.
        Use: git commit -i
 
(c) View the branch merge graph: git log --graph --pretty=oneline
[root@gittest gittest]# git log --graph --pretty=oneline
*   f3cf6f9742af4ba69a956a10e1a63cd43a5a51ab Merge branch 'dev'
|\
| * 6bded575d923bfbfd9d57211f0006ddf3bbe7b5e add time
* | cc33cbf542f16e4fc2cbb79d78573967fb9115e7 add time
|/
* 405d48e85df83aa686dd00d4de01cc44aa12f5d5 add date
* 184c5b3bef0be97eb483e414424e8d25c29e0090  Please enter the commit message for
* 0f7346b632bd645b73646a4d2b03f413a1dfdf8c delete
* fdfa139bc11a1c105c2ac7fa166b98797174a2f8 first commit
 
(d) After merging, the branch can be deleted: git branch -d dev
 
Note: Development branch usage strategy (omitted)
 
Five save the scene
 
[root @ gittest gittest] # git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    modified:   tmp.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@gittest gittest]# git checkout dev
error: Your local changes to the following files would be overwritten by checkout:
    tmp.txt
Please, commit your changes or stash them before you can switch branches.
Aborting
 
Note: When there are related file modifications in the workspace, you cannot switch branches when they are not submitted. At this time, you can use 'git stash' to temporarily store whether the submission is completed or rolled back.
 
1) Staging: git stash
[root @ gittest gittest] # git stash
Saved working directory and index state WIP on master: 1caf27f tmp file
HEAD is now at 1caf27f tmp file
[root @ gittest gittest] # git status
# On branch master
nothing to commit, working directory clean
 
2) View the staging list: git stash list
[root @ gittest gittest] # git stash list
stash@{0}: WIP on master: 1caf27f tmp file
 
3) Two recovery operations after temporary storage
 
(a) Restore and delete: git stash apply restore, delete with git stash drop;
[root@gittest gittest]# git stash apply
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    modified:   tmp.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root @ gittest gittest] # git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    modified:   tmp.txt
#
no changes added to commit (use "git add" and/or "git commit -a”)
[root @ gittest gittest] # git stash drop
Dropped refs/stash@{0} (6288235160baf1e05c2f954428377bc6e4642b0a)
 
(b) Delete directly without recovery: git stash drop
 
Six management tags:
1) Create: git tag
[root @ gittest gittest] # git tag v0.0.1
[root @ gittest gittest] # git tag
v0.0.1
 
Note: Type a version on a commit: git tag v0.0.1 commit_id
[root @ gittest gittest] # git reflog
1caf27f HEAD@{0}: commit: tmp file
cade529 HEAD@{1}: commit (initial): first commit
[root @ gittest gittest] # git tag v0.0.1 1caf27f
[root @ gittest gittest] # git tag
v0.0.1
2) Delete: git tag -d
[root @ gittest gittest] # git tag -d v0.0.1
Deleted tag 'v0.0.1' (was 1caf27f)
[root @ gittest gittest] # git tag
 
3) Push the tag to the remote: git push origin v0.0.1

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326363426&siteId=291194637