User Tools

Site Tools


notes:tools:git

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
notes:tools:git [2018/09/12]
leszek
notes:tools:git [2018/10/10] (current)
leszek
Line 2: Line 2:
  
 <code text> <code text>
-git branch ​                            show local branches +git init                               initialize a Git repo in the current directory 
-git branch -a                          ​show all (local and remote) branches +git clone https://​github.com/​code.git  ​clone the code.git repository together with history
-git branch -a | grep integ             # show branches which names contain '​integ'​+
 </​code>​ </​code>​
  
 <code text> <code text>
-git clone https://​github.com/​code.git  clone the code.git repository together with history+git add <​filename> ​                    # add a single file to staging 
 +git add -u                             add all updated or deleted files to staging (not untracked new files) 
 +git add -A                             # add all files including untracked ones 
 +</code
 + 
 +<code text> 
 +git commit -m "​comment" ​               # commit all staged files
 git commit --amend --no-edit ​          # add more changes to your last commit git commit --amend --no-edit ​          # add more changes to your last commit
 +</​code>​
 +
 +<code text>
 +git mergetool ​                         # use a merge tool to resolve merge conflicts
 +</​code>​
 +
 +<code text>
 +git diff efd22..611ce ​                 # show differences between the commit efd22 (older) and 611ce (newer)
 +git diff HEAD~1..HEAD ​                 # show diff between one commit back (HEAD~1) and the latest commit (HEAD)
 +git diff HEAD~1.. ​                     # the same as above; HEAD is assumed by default
 +git diff --cached ​                     # compare the repository to the staging area
 +</​code>​
 +
 +<code text>
 +git checkout <​filename> ​               # checkout a file from the repository to revert changes in your working
 +                                       # directory
 +git reset --hard ​                      # remove all the current changes - modifications and deletions
 +git reset --soft HEAD~1 ​               # move all the changes from the last commit back to staging
 +git reset --hard HEAD~1 ​               # remove the last commit and discard all the changes
 +git clean -n                           # list files that would be removed by 'git clean' (untracked files)
 +git clean -f                           # remove the files
 +</​code>​
 +
 +<code text>
 +git log                                # list the commits together with their SHA1 in the chronological order
 +git log --oneline ​                     # list commits, one per line
 +git log --oneline | wc -l              # count the number of commits in the repo
 +git log --oneline --graph ​             # show the commit history on the current branch as a graph
 +                                       # with branches and merges
 +git log --oneline --graph --all        # show the commit history on all branches
 +git log --oneline --graph --all --decorate # additionally apply labels to the commits such as tags, local branches
 +                                       # and remote branches
 +git log --format=short ​                # list commits showing only the author and the commit message
 +git log <​branch_name> ​                 # list commits from the given branch
 +git log origin/​master ​                 # example: branch_name is '​origin/​master'​
 +git shortlog ​                          # list commit messages by authors and the number of commits
 +git shortlog -sne                      # list commit messages and include: s - summary, n - order by the number
 +                                       # of commits decreasing, e - include the user's email
 +git show HEAD                          # show the last commit
 +git show HEAD~1 ​                       # show the commit before the last one
 +git show <​SHA1> ​                       # show the commit with the given SHA1
 +
 +git reflog ​                            # show the history of where HEAD pointed
 </​code>​ </​code>​
  
Line 15: Line 63:
 git --version git --version
 git status ​                            # show status and untracked files git status ​                            # show status and untracked files
 +git status --short ​                    # show short status: M - modified, A - new, ? - untracked
 +                                       # col1: staged, col2: modified
 +git status -s                          # the same as show short status
 +git tag                                # list versions that have been tagged
 </​code>​ </​code>​
 +
 +<code text>
 +git fetch                              # pull out changes from a remote repo
 +git fetch <​remote_name> ​               # specify a remote to fetch from (if you have multiple remotes)
 +
 +git merge <​branch_name> ​               # merge from the branch branch_name to the current branch
 +git merge feature1 ​                    # example: merge '​feature1'​ branch to the current branch
 +                                       # HEAD, master, feature1 are all together
 +                                       # it's save to delete the feature1 branch because it's been merged
 +
 +git pull                               # = git fetch; git merge origin/​master
 +</​code>​
 +
 +<code text>
 +git stash
 +git stash list
 +git stash apply
 +git stash pop
 +git stash drop
 +git stash branch <​branch_name> ​        # create a new branch and apply the stash to it
 +</​code>​
 +
 +<code text>
 +# Add a remote repo and name it '​origin'​. It does not have to be called '​origin'​. You can call it any name.
 +> git remote add origin https://​github.com/​wbs/​TestRepo.git
 +> git remote ​                          # show the remote repos
 +origin ​                                # '​origin'​ is the remote the source code came from
 +> git remote -v                        # show remote repos with URLs
 +origin ​ https://​github.com/​wbs/​TestRepo.git (fetch)
 +origin ​ https://​github.com/​wbs/​TestRepo.git (push)
 +> git remote rm origin ​                # remove the remote '​origin'​
 +</​code>​
 +
 +<code text>
 +# Extablish a correspondence between your local branch and a remote branch.
 +# Cloning sets the upstream branches automatically.
 +# git branch --set-upstream-to <​local_branch>​ <​remote_branch>​
 +
 +> git branch --set-upstream-to master origin/​master
 +Branch master set up to track remote branch master from origin.
 +
 +> git pull                             # pull the updates from origin/​master
 +
 +# You can also specify the upstream branch explicitly
 +> git pull origin origin/​master
 +
 +# Push the current branch and set the remote as upstream
 +> git push --set-upstream origin origin/​master
 +</​code>​
 +
 +====Branching====
 +
 +Branches are labels on SHA1 hashes of individual commits.
 +
 +<code text>
 +git branch ​                            # show local branches
 +git branch -r                          # show remote branches
 +git branch -a                          # show all (local and remote) branches
 +git branch -a | grep integ             # show branches which the name that contains '​integ'​
 +git branch -m <​old_name>​ <​new_name> ​   # rename (move) a branch
 +git branch -d <​branch_name> ​           # delete a branch; if there are any not merged commits the branch
 +                                       # won't be deleted; it's safe to delete a branch after it's been merged
 +git branch -D <​branch_name> ​           # force deletion of a branch with all the commits
 +git branch <​branch_name>​ <​commit_SHA1>​ # recover a commit
 +</​code>​
 +
 +<code text>
 +git checkout <​branch_name> ​            # checkout an existing branch
 +git checkout -b <​branch_name> ​         # create a new branch and check it out
 +</​code>​
 +
 +Example: Create a new branch and switch to it:
 +<code text>
 +ltara@WBS MINGW64 /​c/​Projects/​Test (origin/​master)
 +$ git branch feature1
 +
 +ltara@WBS MINGW64 /​c/​Projects/​Test (origin/​master)
 +$ git checkout feature1
 +Switched to branch '​feature1'​
 +
 +ltara@WBS MINGW64 /​c/​Projects/​Test (feature1)
 +$ git log --graph --oneline --all --decorate
 +* 77f6a47 (HEAD -> feature1, origin/​master,​ master) Change README
 +* dded7b8 Fix typo in README.txt
 +...
 +</​code>​
 +
 +Example cont'​d:​ Modify a file and create a commit:
 +<code text>
 +ltara@WBS MINGW64 /​c/​Projects/​Test (feature1)
 +$ echo "​Feature1"​ >> README.txt
 +
 +ltara@WBS MINGW64 /​c/​Projects/​Test (feature1)
 +$ git commit -am "Add feature1"​
 +[feature1 b7ef1be] Add feature1
 + 1 file changed, 1 insertion(+)
 +
 +ltara@WBS MINGW64 /​c/​Projects/​Test (feature1)
 +$ git log --graph --oneline --all --decorate
 +* b7ef1be (HEAD -> feature1) Add feature1
 +* 77f6a47 (origin/​master,​ master) Change README
 +* dded7b8 Fix typo in README.txt
 +...
 +</​code>​
 +
 +Example cont'​d:​ Switch back to master:
 +<code text>
 +ltara@WBS MINGW64 /​c/​Projects/​Test (feature1)
 +$ git checkout master
 +Switched to branch '​master'​
 +
 +ltara@WBS MINGW64 /​c/​Projects/​Test (master)
 +$ git log --graph --oneline --all --decorate
 +* b7ef1be (feature1) Add feature1
 +* 77f6a47 (HEAD -> master, origin/​master) Change README
 +* dded7b8 Fix typo in README.txt
 +...
 +</​code>​
 +
 +Example cont'​d:​ Create another branch (feature2) and check it out:
 +<code text>
 +ltara@WBS MINGW64 /​c/​Projects/​Test (master)
 +$ git checkout -b feature2
 +Switched to a new branch '​feature2'​
 +
 +ltara@WBS MINGW64 /​c/​Projects/​Test (feature2)
 +$ git log --graph --oneline --all --decorate
 +* b7ef1be (feature1) Add feature1
 +* 77f6a47 (HEAD -> feature2, origin/​master,​ master) Change README
 +* dded7b8 Fix typo in README.txt
 +...
 +</​code>​
 +
 +Example cont'​d:​ Modify the same file as in feature1 and create a commit:
 +<code text>
 +ltara@WBS MINGW64 /​c/​Projects/​Test (feature2)
 +$ echo "​Feature2"​ >> README.txt
 +
 +ltara@WBS MINGW64 /​c/​Projects/​Test (feature2)
 +$ git commit -am "Add feature2"​
 +[feature2 eca0562] Add feature2
 + 1 file changed, 1 insertion(+)
 +
 +ltara@WBS MINGW64 /​c/​Projects/​Test (feature2)
 +$ git log --graph --oneline --all --decorate
 +* eca0562 (HEAD -> feature2) Add feature2
 +| * b7ef1be (feature1) Add feature1
 +|/
 +* 77f6a47 (origin/​master,​ master) Change README
 +* dded7b8 Fix typo in README.txt
 +...
 +</​code>​
 +
 +
 +====Git Configuration====
 +
 +**System-level configuration** applies to the entire computer Git is installed on.
 +
 +  * Linux: /​etc/​gitconfig
 +  * Windows: C:\Program Files\Git\mingw64\etc
 +
 +**User-level configuration**:​
 +
 +  * Linux: ~/​.gitconfig
 +  * Windows: C:​\Users\USERNAME\.gitconfig ​ -or-  C:​\Users\USERNAME\.gitconfig.d
 +
 +**Repository-level configuration**
 +
 +  * Stored in .git/config in each repo
 +
 +<code text>
 +git config --system [action] ​  # access system config file (system-level)
 +git config --global [action] ​  # access global config file (user-level)
 +git config --local [action] ​   # access repository config file (repo-level)
 +
 +git config --local --list ​     # list the repo-level configuration
 +
 +# remove a setting, for example help.autocorrect
 +git config --global --unset help.autocorrect
 +</​code>​
 +
 +<code text>
 +git config --global user.name "Mike McFerry"​
 +git config --global user.email "​mferry@email.com"​
 +
 +# for editing commit messages and viewing diffs
 +git config --global core.editor notepad ​
 +
 +# enable autocorrecting misspelled git commands, for example 'git statsu'​ --> 'git status'​
 +git config --global help.autocorrect 1
 +
 +# use colors when showing information (only in terminal)
 +git config --global color.ui auto
 +</​code>​
 +
 +<code text>
 +git config --global core.autocrlf true   # convert crlf to lf before storing in repo and then back to crlf when
 +                                         # pulling from repo; good for Windows development
 +
 +git config --global core.autocrlf input  # convert crlf to lf before storing in repo but don't do anything when
 +                                         # pulling from repo; good for Linux and Mac
 +
 +git config --global core.autocrlf false  # do not convert crlf; Windows development only
 +</​code>​
 +
 +Example: Define an alias:
 +<code text>
 +> git config --global alias.lga "log --oneline --graph --all --decorate"​
 +
 +> git lga
 +* 77f6a47 (HEAD -> origin/​master,​ master) Latest changes to README
 +* dded7b8 Fixed typo in README.txt
 +* 977c77f Fixed bug#123
 +* 48bcb3c Added cool new feature
 +* 611ce6b Another update
 +* efd22b4 Added README.txt
 +
 +> cat ~/​.gitconfig
 +[alias]
 +        lga = log --oneline --graph --all --decorate
 +</​code>​
 +
  
 ====General Info==== ====General Info====
Line 24: Line 298:
  
   * Local VCS: The version db is stored locally on your computer. Each change is stored as a version patch in the db.   * Local VCS: The version db is stored locally on your computer. Each change is stored as a version patch in the db.
-  * Centralized VCS+  * Centralized VCS: The version db is located on a central server. Collaborators have a snapshot of a single file they are currently working on. 
 +  * Distributed VCS: The version db resides on each contributor'​s computer i.e., each client has a full backup of the entire project. 
 + 
 +Git checksums:​ 
 + 
 +  * Each version of a file in Git has a unique 40-chars long checksum. 
 +  * If the checksum is the same, the files must be the same. 
 +  * The checksums are generated by SHA1 hashing algorithm. 
 + 
 +The three stages of a tracked file in Git: 
 + 
 +  * **Committed** (stored in the local Git database) 
 +  * **Modified** (work in progress) 
 +  * **Staged** (marked for commit) 
 +   
 +The three states of a Git project: 
 + 
 +  * **Working Directory** (Updated) - This is a copy of a single version of the project from Origin. It contains updated files that have not been added to the staging area yet. 
 +  * **Staging Area** (Index) - It contains one or more updated files staged for the next commit. 
 +  * **.git Repository** (Origin) - These are the files cloned from a remote server and stored locally as the Git database and metadata. It contains committed files. 
 + 
 +^  command ​ ^  effect ​ ^ 
 +| git checkout | Origin --> Working Directory | 
 +| git add | Working Directory --> Staging | 
 +| git commit | Staging --> Origin | 
 + 
 + 
 +====Links==== 
 +   
 +[[https://​chris.beams.io/​posts/​git-commit/​|How to Write a Git Commit Message]]
  
notes/tools/git.1536760088.txt.gz · Last modified: 2018/09/12 by leszek