Git 常用命令(一)

  • Overview

    porcelain commands: user-friendly commands;

    plumbing commands: that do low-level work and were designed to be chained together UNIX-style or called from scripts.

  • git config

    git config lets you get and set configuration variables that control all aspects of how Git looks and operates, these variables can be stored in three different places:

    • [path]/etc/gitconfig file, contains values applied to every user on the system and all their repositories. git config --system
    • ~/.gitconfig or ~/.config/git/config file, contains values specific personally to you, the user, git config --global, this affects all of the repositories you work with on your system
    • config file in the Git directory (.git/config) of whatever repository you’re currently using, Specific to that single repostitory. git config --local, which is default

    Each level overrides values in the previous level, so values in .git/config trump those in [path]/etc/gitconfig

    ####################Checking###################################
    # view all settings and where they are coming from
    $ git config --list --show-origin 
    # check specific key's value
    $ git config user.name  # git config <key>
    $ git config --show-origin user.name  # show which configuration file had the value
    #####################Setting####################################
    # -------------------Your Identity------------------------------
    # override this with a different name or email for specific project, you can run the command without the --global option when you're in that project
    $ git config --global user.name "John Doe"
    $ git config --global user.email [email protected]
    # -------------------Your Editor-------------------------
    $ git config --global core.editor vim # 指定vim text editor
    # -------------------Set Default Branch Name------------
    $ git config --global init.defaultBranch main  # default master
    # ###################Helping##############################
    $ git help config  # git help <verb>
    $ git config --help
    $ man git-config
    
    $ git config -h  # ask for the more concise help
    
  • Getting a Git Repository

    1. You can take a local directory that is currently not under version control, and turn it into a Git repository, or

      # Empty direcotry
      $ cd path/project
      $ git init
      # Directory with files
      $ cd path/project
      $ git init
      $ git add file
      $ git commit -m "Initial version"
      
    2. You can clone an existing Git repository from elsewhere

      $ git clone https://github.com/libgit2/libgit2
      $ git clone https://github.com/libgit2/libgit2 newName # clone the repository into a new directory
      
  • Checking the Status of files

    $ git status
    $ git status -s
    $ git status --short	# short status
    # there are two columns, the lefthand column indicates the status of the staging area, the righthand columns indicates the status of the working tree
    	M	README				# modified, not yet staged
       MM	Rakefile			# modified, staged and modified again
       A	lib/git.rb			# new files have been added to the staging area
       M	lib/simplegit.rb	# modified and staged
       ??	LICENSE.txt			# new files that aren't tracked
    $ git add README # if README is a directory, the command adds all the files in it recursively
    
    

    git add is a multipurpose command: to begin tracking new files, to stage files, to making merge-conflicted files as resolved.

  • Ignoring Files

    $ cat .gitignore
    *.[oa]	# ignore any files ending in '.o' or '.a'
    *~		# any files whose names end with a tilde(~)
    !lib.a	# do track lib.a, even though you're ignoring .a files above
    /TODO	# only ignore the TODO file in the current directory, not subdir
    build/	# ignore all files in any directory named build
    doc/*.txt	# ignore doc/notes.txt, but not doc/server/arch.txt
    doc/**/*.pdf# ignore all .pdf files in the doc/directory and any of its subdirectories
    
  • Viewing Your Staged and Unstaged Changes

    git diff			# what you've changed but not yet staged
    git diff --staged	# compares your staged changes to your last commit
    git diff --cached	# synonyms to --staged
    
  • Committing Your Changes

    $ git commit -v		# -v, puts the diff of your change in the editor so you can see exactly what changes you're committing
    $ git commit -m "message"
    $ git commit -a 	# automatically stage every file that is already tracked before doing the commit, letting you skip the git add part
    
  • Removing Files

    $ git rm test.txt	# rm from the staged area, then commit, 同时从staged area和本地删除
    $ git commit
    
    $ git rm -f test.txt	# if test.txt is modified and added to the staging area, -f is needed
    $ git rm --cached test.txt # keep the file in your working tree but remove it from your staging area
    $ git rm log/\*.log		# you can pass files, directories, file-glob patterns
    
  • Moving Files

    $ git mv oldName newName	# git will take mv as rename
    # is equivalent to 
    $ mv oldName newName
    $ git rm oldName
    $ git add newName
    
    
  • Viewing the Commit History

    $ git log		# lists the commits made in that respository in reverse chronological order
    $ git log -p	# show the difference introduced in each commit
    $ git log -2	# limit the number of log enties displayed
    $ git log -- stat	# abbreviated stats for each commit
    $ git log --pretty=oneline	# short, full, fuller, format are supported
    $ git log --pretty=format:"%h - %an, %ar : %s"	Page 43
    $ git log --pertty=format:"%h %s" --graph	# add a nice little ASCII graph showing your branch and merge history
    $ git log --oneline # equal to --pretty=oneline --abbrev-commit
    $ git log --since=2.weeks	# last two weeks
    $ git log --auther			# filter on a specific author
    $ git log --grep			# search for keywords in the commit messages
    $ git log -S function_name	# shows only those commits that changed the number of occurrences of that string
    $ git log -- path/to/file	# limit the log output to commits that introduced a change to those files
    
    
  • Undoing Things

    $ git commit -amend	# if you want to redo commit, make the additional changs you forgot, stage them, and commit again using the --amend option
    
    

    Only amend commits that are still local and have not been pushed somewhere.

  • Unstaging a Staged File

    $ git reset HEAD file
    $ git restore --staged file		#  after version 2.23
    
    
  • Unmodifying a Modified File

    $ git checkout -- file	# revert the changes, Git replaced that file with the most recently-committed version
    $ git restore file		# dangerous command
    
    

    If you would like to keep the changes you’ve made to that file but still need to get it out of the way for now, you should search for Git Branching.

  • Working with Remote

    # ####################Showing Your Remotes##################################
    $ git remote
    $ git remote -v		# shows the URLs that Git has stored for the shortname to be used when reading and writing to that remote
    # ####################Adding Remote Repositories############################
    $ git remote add pb https://github.com/paulboone/ticgit # to add a new remote Git repository as a shortname
    # ####################Fetching and Pulling from Your Remotes################
    
    $ git fetch <remote>	# to get data from your remote projects
    $ git pull 				# fetch data from the server you originally cloned from, and automatically tries to merge it into the code you're currently working on
    # ####################Pushing to Your Remotes###############################
    $ git push <remote> <branch>	# work only if you cloned from a server to which you have write access and if nobody has pushed in the meantime
    # ####################Inspecting a Remote###################################
    $ git remote show origin	# more information about a particular remote
    # ####################Renaming and Removing Remotes#########################
    $ git remote rename pb paul	# change remote's shortname
    $ git remote remove paul
    $ git remote rm paul		# remove a remote
    
    
  • Tagging

    # ####################Listing Your Tags############################
    $ git tag
    $ git tag -l "v1.8.5*"	# search for tags that match a particular pattern
    # ####################Create Tags#################################
    $ git tag -a v1.4 -m "my version 1.4"	# create an annotated tag, -m specifies a tagging message
    $ git tag v1.4-lw						# create a lightweight tag
    $ git tag -a v1.2 9fceb02				# to tag the specify the commit checksum
    # ####################Check Tags###################################
    $ git show v1.4
    # ####################Sharing Tags#################################
    $ git push origin <tagname>
    $ git push origin --tags	# lots of tags to push up at once
    # ####################Deleting Tags################################
    $ git tag -d <tagName>	# just delete tag in local, not remote
    $ git push <remote> :refs/tag/<tagName>	# delete tag from remote server
    $ git push origin --delete <tagName>	# anohter way delete remote tag
    # ###################Checking out Tags#############################
    $ git chechout v2.2
    
    

    理解detached HEAD

  • Aliases

    $ git config --global alias.co checkout
    $ git config --global alias.br branch
    $ git config --global alias.ci commit # instead of typing 'git commit', you just need to type 'git ci'
    $ git config --global alias.st status
    
    $ git config --global alias.unstage 'reset HEAD --'
    $ git config --global alias.last 'log -1 HEAD'
    
    $ git config --global alias.visual '!gitk'
    
    

    Start the command with a ! character, to run an external command, rather than a Git subcommand.

  • Plumbing Commands

    ##################git hash-object##################
    $ git init test
    $ cd test
    $ find .git/objects  # 空
    $ find .git/objects -type f # 查找content
    
    # git hash-object take the content to return it into unique key and store it in your Git database
    # -w: not simply return the key, but to write it in your Git database
    # --stdin: to get the content to be processed from stdin, otherwise the command would expect a filename argument at the end of the command containg the content to be used
    $ echo 'test content' | git hash-object -w --stdin  
    $ find .git/objects -type f
    # ###################git cat-file#####################
    $ git cat-file -p d67*******
    $ git cat-file -t d67*******  # object type of any object in Git
    
    

    上述命令是保存提取文件内容,但是记住each version of your file isn’t practical, plus, you aren’t storing the filename in your system - just the content. This object type is called a blob.

    # ##########################tree object############################
    $ git update-index 	# add the file to a new staging area
    $ git write-tree	# write teh staging area out to a tree object
    
    
  • References

  1. Chacon, S. & Straub, B. Pro Git. Pro Git (2014) doi:10.1007/978-1-4842-0076-6.

猜你喜欢

转载自blog.csdn.net/The_Time_Runner/article/details/113924223