Git common operations backup


$git config --global user.name "Z_Z.W"
$git config --global user.email "[email protected]"

$cd $repository$
$ git init

$git add readme.txt
$ git commit -m " wrote a readme file"

$ git status #The command allows us to keep abreast of the current status of the warehouse. The above command tells us that readme.txt has been modified, but there are no modifications ready to be submitted.
$ git diff readme.txt #See what has been modified

$ git add readme.txt #Committing changes and submitting new files are the same two steps
$ git status
$ git commit -m "add distributed"

$ git log (git log --pretty=oneline) #The command displays the commit log from the latest to the furthest


$ git reset --hard HEAD^ #Return to the previous version #The previous version is HEAD^, the previous version is HEAD^^, of course It is easier to write 100 ^ up to 100 versions, so it is written HEAD~100.
$ git reset --hard 3628164 ##commit id is 3628164fb26d48395383f8f31179f24e0882e1e0 The first few digits are fine, and Git will find them automatically

$ git reflog ##Git provides a command git reflog to record your every command


-----When creating a Git repository, Git automatically created the only master branch for us, so now, git commit is to Commit changes on the master branch.


$ git diff HEAD -- readme.txt #The command can check the difference between the latest version in the workspace and the repository

$ git checkout -- readme.txt #The modification of the file in the workspace is all undone, which is to return the file to the latest git The status of commit or git add


$ git rm test.txt
$ git commit -m "remove test.txt" ##If you really want to delete the file from the repository, use the command git rm to delete it, and git commit

-- ----------------------------------------Remote Repository------- -------------------------------------------------- --------
$ ssh-keygen -t rsa -C "[email protected]" #Step 1: Create an SSH Key (generate a .ssh directory in the user's home directory)
Step 2: Log in to GitHub, Open the "Account settings", "SSH Keys" page
and click "Add SSH Key", fill in any Title, paste the content of the id_rsa.pub file in the Key text box to


log in to GitHub, and then find "Create a new" in the upper right corner repo" button to create a new repository
$git remote add origin [email protected]:myhongkongzhen/myPublicRepo.git #Push the local git library to github, the name of the remote library is origin, which is the default name of Git (error execution: git remote rm origin)
$git push -u origin master


%%%%%%%% Develop from scratch, then the best way is to create a remote library first, and then clone from the remote library.
$ git clone [email protected]:myhongkongzhen/dwzspringmvcdemo.git ##Git supports multiple protocols, the default git:// uses ssh, but other protocols such as https can also be used
------------- -------------------------------------------------- -------------------------------------------------- -----


------------------------------Branch-------------- -------------------------------------------------- ---------------------------------------

$ git checkout -b dev to create the dev branch, then Switch to the dev branch
           ($ git branch dev $ git checkout dev)

$ git branch #View the current branch

$ git checkout master #Switch back to the master branch

$ git merge dev #The work results of the dev branch are merged into the master branch $$ Merge the specified branch to the current branch

$ git branch -d dev #Delete the dev branch

                  View branch: git branch
                  Create branch: git branch <name>
                  Switch branch: git checkout <name>
                  Create + switch branch: git checkout -b <name>
                  Merge a branch to the current branch: git merge <name>
                  Delete branch: git branch -d <name>

$ git log --graph --pretty=oneline -- abbrev-commit #See the merge status of the branch

$ git merge --no-ff -m "merge with no-ff" dev #Prepare to merge the dev branch, please pay attention to the --no-ff parameter, which means to disable Fast forward, plus -m parameter, write the commit description into the

%%%%%% master branch should be very stable, that is, it is only used to release new versions, and when you can't work on it

and merge branches, add the --no-ff parameter It can be merged in normal mode. The merged history has branches, and it can be seen that the merge has been done, but the fast forward merge cannot see that it has been merged.

-------------------------------------------------- -------------------------------------------------- ---------------------- #View

 the workspace, it is clean (unless there are files that are not managed by Git)
$git stash
$git stash pop
$ git stash The list

is stashed many times. When restoring, use git stash list to check first, and then restore the specified stash.
$ git stash apply stash@{0}

When the work at hand is not completed, git stash the work site first, and then fix the bug. After repairing, git stash pop again, and return to the work site.

$ git branch -D feature-vulcan #Forcibly delete the branch

$git push origin master (dev branch) #Git will push the branch to the remote branch corresponding to the remote library

$ git checkout -b dev origin/dev #Create a remote origin dev branch to local

$ git pull ###ERROR: Use git pull to grab the latest commit from origin/dev, then merge locally, resolve conflicts, and push
$ git branch --set-upstream dev origin/dev ##Set the link of dev and origin/dev


$ git tag v1.0 ##Make a new tag

$ git log --pretty=oneline --abbrev-commit
$ git tag v0.9 6224937 #The corresponding commit id is 6224937, type a new tag

$ git show v0.9 ##View tag information

$ git tag -d v0. 1 #Delete

$ git push origin v1.0 #Push a tag to the remote

$ git push origin --tags ## Push all local tags that have not been pushed to the remote at one time:

$ git push origin :refs/tags/v0.9 ##It is a little more troublesome to delete the remote label, first delete it from the local, and then delete it from the remote. The delete command is also push



$ git config --global alias.co checkout
$ git config --global alias.ci commit
$ git config --global alias.br branch




---------------- ----------------------------Build Git server --------------------- ----------------------------------------------
Create certificate login:

Collect the public keys of all users who need to log in, which is their own id_rsa.pub file, and import all public keys into the /home/git/.ssh/authorized_keys file, one line per line

# git init --bare zzgitrepo.git #initialization Git repository

Git will create a bare repository. The bare repository has no workspace, because the Git repository on the server is purely for sharing, so users are not allowed to log in directly to the server to change the workspace, and the Git repository on the server usually ends with .git . Then, change the owner to git:

Step 5, disable shell login: For security reasons, the git user created in the second step is not allowed to log in to the shell, which can be done by editing the /etc/passwd file.

Step 6, clone the remote warehouse
$ git clone [email protected]:/opt/zzrepository/zzgitrepo.git

--------------------------------- -------------------------------------------------- -----------------------------------









Guess you like

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