Git: Difference between revisions
(→gitk) |
|||
| Line 274: | Line 274: | ||
git-gui | git-gui | ||
* get overview by 'Repository -> Browse ... Files' | * get overview by 'Repository -> Browse ... Files' | ||
* start gitk by 'Repository -> Visualize ...' | * start [[Git#gitk|gitk]] by 'Repository -> Visualize ...' | ||
==gitk== | ==gitk== | ||
Revision as of 14:25, 17 June 2020
Concepts
Git is a Distributed Version Control Systems (DVCS). Clients don’t just check out the latest snapshot of the files, they fully mirror the repository. Thus if any server dies, and these systems were collaborating via it, any of the client repositories can be copied back up to the server to restore it. Every clone is really a full backup of all the data.
A Git Version (commit object) (short:commit) represents a version of all files tracked in the repository at the time the commit was created:
For Git a version history is a stream of snapshots of the complete project. Therefore the local repository contains the complete history of a project. A remote repository on a server typically does not require a working tree. A Git repository without a working tree is called a bare repository.
In a DVCS the elemental concept is a change set, it is merely a change to a set of files and a pointer to the previous change set. For identification the change set includes a pointer to the previous change set and which is hashed then. A changeset is merely a change to a set of files. Git changesets are identified by an SHA-1 hash, which is a 40 character checksum of the content of a file. The tip of the current local branch is referred to as HEAD.
Git focus on content (files), therefore empty directories are not in a repository (unless you put an .gitignore in the directory, then it is created).
Origin is an alias on your system for a particular remote repository and sometimes a shorthand name for the remote repository that a project was originally cloned from. More precisely, it is used instead of that original repository's URL - and thereby makes referencing much easier (see here).
For a graphical description see here. .
File States
Each file in the working directory can have one the following states:
- Untracked (unversioned) means the file is not tracked by the Git repository. This means that the file never staged nor committed.
- Tracked (committed) means that the data is safely stored in your local database. The tracked files can be:
- new file: new file being staged by
git addfor the first time for this file - Unmodified: the file was commited and is unchanged
- Modified (unstaged): the files was edited (but not staged)
- Staged (indexed): a modified file is marked in its current version to go into your next commit snapshot by
git add
- new file: new file being staged by
Operations
Cloning
Cloning is the process of copying an existing Git repository via the Git tooling.
Checkout
A checkout copies a local branch (version of the last commit of this branch) into the working directory and set the HEAD accordingly. This can include deletion of files or directories which are part of the current branch but not of the branch to be checkout (commands see here).
Fetch
The git fetch command downloads commits, files, and refs from a remote repository into your local repo (it does not merge anything with local changes). Fetching is what you do when you want to see what everybody else has been working on (see here).
Merge
There are different methods to merge:
- fast-forward (pointer just been moved forward)
- recursive strategy (creates a merge commit)
In case that the branches to merge are changed in different directions the merge will result in a new merge commit.
Pull
A git pull does a git fetch followed by a git merge (a fetch does not change the working directory).
Push
Pushing means sending a new version of the local repository to a remote repositories.
Stash
Stashing takes the dirty state of your working directory — that is, your modified tracked files and staged changes — and saves it on a stack of unfinished changes that you can reapply at any time (even on a different branch).
Repository
A Repository is essentially a folder on the local hard disk which contains the working directory and the metadata folder (git directory). The Git directory (.git) is where Git stores the metadata and object database for your project. This is the most important part of Git, and it is what is copied when cloning a repository from another computer.
Branches
Branching is the way to work on different versions of a repository at one time. By default your repository has one branch named master. Branches are something like a pointer to a element of a version chain.
Tracking Branches (external branches) are local branches with a direct link to a remote branch. They resemble bookmarks to the branches in a remote repositories of the last connection and written as
<REPOSITORY>/<BRANCHNAME>.
If a tracking branch is selected, git knows in case of a git push|pull which server and repository to be used. The remote branch which is referenced is called upstream branch. Tracking Branches are updated only by in case of network operations of git. Remote Branches are like any other branch, but read-only. To use the remote changes the remote branch has to be merged into the local branch (that is what git pull does as a shortcut for git fetch followed by git merge.
Directories
Local Branches in subdirectories below:
.git/refs/heads
Remote Branches in subdirectories below:
.git/refs/remote
Working Directory/Tree
The working directory is the directory used to modify files for the next commit (it seems that it is sometimes also called working tree). By default it is located one level above the .git directory. It is a single checkout of one version of the project. These files are pulled out of the compressed database in the Git directory and placed on disk for you to use or modify. It corresponds to a checkout of one version of the repository with potential changes done by the user. The user can change the files in the working tree by modifying existing files and by creating and removing files.
\<Repository>\ // Working Directory \.git\ // Git Directory
Staging Area
The staging area or index is a file (.git/index) filled by git add, generally contained in your Git directory, that stores information about what will go into your next commit.
Head
The HEAD points to head of current local branch.
GitHub
Although it is not necessary it is very common to use git together with GitHub.
Tag
A tag points to a commit which uniquely identifies a version of the Git repository. The benefit of tags is to mark the repository for a specific reason, e.g., with a release.
Ignore
Git can be configured to ignore certain files and directories for repository operations. This is configured via one or several .gitignore files. Typically, this file is located at the root of your Git repository but it can also be located in sub-directories. In the second case the defined rules are only valid for the sub-directory and below. .gitignore should be part of the versioned files. To add a complete directory create one line with
<DIRECTORY_NAME>
Configuration
See installation information for the different computers (e.g. Eon Laptop New):
- HOME directory: git, EGit, ... need this path for looking up the global user configuration (.gitconfig). HOME should point to your home directory. Git installation routine put .gitconfig to C:\Users\U1728. If %HOME% is not set %HOMEDRIVE%%HOMEPATH% is used.
Operations
Add File or Directory to be tracked
Add is used for different purposes, one is to add a new file to tracked by the VCS (second is to add changes of tracked files to the stage for the next commit):
git add . // adds all files incl. sub-directories
git add <Filename>
git add <Directoryname> // will add all non-empty recursively
// obviously the --all parameter is not needed
Clone a repository
git clone /pfad/zum/repository git clone benutzername@host:/pfad/zum/repository
Clone GitLab Repository
- goto to target directory e.g. ~/GitRepositories
- copy GitLab URL
- type clone command e.g.
git clone https://gitlab.com/UweHeuer/test4.git
Clone GitHub Repository
- go to target directory e.g. C:\Uwes\GitRepositories
- copy GitHub URL
- type clone command e.g.
git clone https://github.com/UweHeuer/patternLockTest.git
This creates a new directory.
Compare Stage with Repo
git diff --staged
Compare Working directory and Stage
git diff
Delete File from Repository
git rm <File>
Commit Changes
git commit // will add all staged changes to the repository, in a cmd shell a editor is opened to specifiy the commit message in file .git/COMMIT_EDITMSG git commit -a [-m <COMMIT_MESSAGE>] // will add all changes (not only the staged) to the repository
Delete a Repository
Delete the folder which contains the repository.
Fetch everything from Remote
git fetch [remote-name] // e.g. git fetch origin => amongst others the tracking branch origin/master will be updated locally
Make an existing Project Directory to a Git Repository/Create Repository
- go into the directory and
git init
- this will create .git directory
- then add files
git add <FILENAME> // e.g. 'git .' or 'git add .\D1\F1.txt'
- if needed connect to a remote repository
git remote add [shortname for remote] [url] // e.g. git remote add origin https://gitlab.com/UweHeuer/test1.git // this will not create a new project remotely but just store the connection
- commit changes
git commit
- push everything to remote
git push -u origin master // on GitLab this will create a new project, -u is important to make master a tracking branch and origin/master the upstream branch
- create a branch
git checkout -b branch1
- add and commit files
- push branch to remote
git push -u origin branch1 // -u important to make branch1 a tracking branch (see git branch -vv) // if -u forgotten at the first call you can set the upstream branch later e.g. by git push -u origin branch1
Get an Overview of an Project
- show remote repositories
git remote -v // -v shows detailled path, if no remote repository there is no output
- update the local repository
git fetch
Pull Something
Often pull cancels because there are local changes, which often can be ignored. Therefore it could make sense to use stash commands before and after:
git stash git pull git stash clear // deletes everything from stash area
Push Something
git push // if current branch is a tracking branch REMOTE_NAME and BRANCH_NAME not needed git push <REMOTE_NAME> <BRANCH_NAME> // e.g. git push origin branch1 git push -u <REMOTE_NAME> <BRANCH_NAME> // -u needed for the first time to make it a tracking branch
Manage Branches
Show Branches
Currently checked-out branch has an asterik in front of the output:
git branch // show all local branches
git branch --merged [--no-merged] // show all local branches which are merged into the current branch (those w/o asterik could be deleted)
git branch --no-merged // show all local branches which are not merged into the current branch
git branch -r // shows only remote branches
git branch -a // shows all branches (local, tracking and remote)
git branch -vv // shows tracking information for local branches in the form:
// <BRANCH_NAME> <SHA-1> [<REMOTE BRANCH>: ahead and behind comparison] <LAST_COMMIT_MESSAGE>
git remote show origin // full picture about tracked branches and status
Checkout a branch
Checkout will not override changed files but will be aborted and show a warning:
git checkout <BRANCH_NAME> // in new git versions it will if the the branch does not exist locally get the remote one (-t) git checkout -b <BRANCH_NAME> // creates a branch and checks it out, short of git branch <BRANCH_NAME> and git checkout <BRANCH_NAME> git checkout --track origin/<BRANCH_NAME> // create a local branch based on remote branch and checks it out git checkout -t origin/<BRANCH_NAME> // s.a.
Merge Branches
A merge of a branch into the master branch (s. here):
git merge <BRANCHNAME> // <BRANCHNAME> => <CURRENT_BRANCH>, checkout the branch in which the merge should be performed before git checkout master git merge origin/master git checkout Branch1 git merge master // merge master into Branch1
Compare Branches
git diff <BRANCH> <BRANCH> [--name-only] // e.g. git diff Branch1 master git diff master origin/master // compare local with remote branch, output for each file is diff --git <FILE VERSION A> <FILE VERSION B> ... --- <FILE VERSION A> +++ <FILE VERSION B> - <CONTENT FILE A> + <CONTENT FILE B>
Track a branch
git branch -u origin/<BRANCH_NAME> // sets the remote branch as upstream branch git checkout -b <NEW_BRANCH_NAME> <REMOTE>/<REMOTE_BRANCH_NAME> // creates a new branch as tracking branch git checkout --track <REMOTE>/<REMOTE_BRANCH_NAME> // does the same
Delete a branch
git branch -d <BRANCH_NAME> // deletes a local branch, but only if it was fully merged before, otherwise use -D option git branch -D <BRANCH_NAME> // deletes a local branch, regardless whether it has been merged before git push origin :<BRANCH_NAME> // delete a remote branch
Show Repository History
git reflog --date=local [<BRANCH>] // for full history git log // for commits
Show Configuration
git config --list git config <CONFIG_NAME> // git config user.name
Rename Directory
git mv <OLD_NAME> <NEW_NAME> // rename (also) working directory in file system git commit -m "<COMMIT_MESSAGE>" git push // merge ...
Show remote Repository
git remote -v
Show Status of a Repository/Current Branch
git status On branch <BRANCHNAME> // shows current branch Changes to be committed: ... <STAGED_FILES_AND/OR_DIRECTORIES IN GREEN> Untracked files: <UNTRACKED_FILES_AND/OR_DIRECTORIES IN RED>
Stashing changes
git stash
Installation
Egit
- see Eclipse
Git Gui
- Windows CMD shell
git-gui
- get overview by 'Repository -> Browse ... Files'
- start gitk by 'Repository -> Visualize ...'