Git: Difference between revisions
(→Merge) |
|||
| (48 intermediate revisions by the same user not shown) | |||
| Line 11: | Line 11: | ||
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). | 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 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 [[Git#Show_remote_Repository|here]]). | '''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 [[Git#Show_remote_Repository|here]]). | ||
For a graphical description see [https://git-scm.com/book/de/v1/Git-Branching-Was-ist-ein-Branch%3F here]. | For a graphical description see [https://git-scm.com/book/de/v1/Git-Branching-Was-ist-ein-Branch%3F here]. | ||
| Line 37: | Line 37: | ||
====Merge==== | ====Merge==== | ||
There are different methods to merge: | There are different methods to merge: | ||
* fast-forward | * 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'''. | In case that the branches to merge are changed in different directions the merge will result in a new '''merge commit'''. | ||
| Line 52: | Line 53: | ||
===Repository=== | ===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. | A '''Repository''' is essentially a folder on the local hard disk which contains the working directory and the metadata folder (git directory). In most cases there is one repository per project but it is possible to have multiple projects in one repo (monorepo approach). 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. | ||
[[File:GitStructure.JPG|400px]] | [[File:GitStructure.JPG|400px]] | ||
| Line 61: | Line 62: | ||
[[File:GitHubBranchButton.JPG]] | [[File:GitHubBranchButton.JPG]] | ||
'''Tracking Branches''' (external branches) are '''local''' | '''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>. | <REPOSITORY>/<BRANCHNAME>. | ||
If a tracking branch is selected, git knows in case of a <code>git push|pull</code> 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. | If a tracking branch is selected, git knows in case of a <code>git push|pull</code> 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 <code>git pull</code> does as a shortcut for <code>git fetch</code> followed by <code>git merge</code>. | ||
====Directories==== | |||
Local Branches in subdirectories below: | |||
.git/refs/heads | |||
Remote Branches in subdirectories below: | |||
.git/refs/remote | |||
===Working Directory/Tree=== | ===Working Directory/Tree=== | ||
| Line 94: | Line 102: | ||
==Operations== | ==Operations== | ||
===Add File or Directory to be tracked=== | ===Add File or Directory to be tracked or Modified to Stage=== | ||
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): | 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): | ||
[[File:git1.png|400px]] | |||
// see table above | |||
git add . // stages new files and modifications, without deletions (on the current directory and its subdirectories) | |||
git add <Filename> | git add <Filename> | ||
git add <Directoryname> // will add all non-empty recursively | |||
git add <Directoryname> // will add all non-empty recursively | |||
// obviously the --all parameter is not needed | // obviously the --all parameter is not needed | ||
git add -u // stages modifications and deletions, without new files | |||
===Clone a repository=== | ===Clone a repository=== | ||
| Line 106: | Line 118: | ||
===Clone [[GitHub|GitHub]] Repository=== | ====Clone [[GitLab|GitLab]] Repository==== | ||
* goto to target directory e.g. ~/GitRepositories | |||
* copy GitLab URL | |||
[[File:CloneGitLab.PNG|400px]] | |||
* type clone command e.g. <code>git clone https://gitlab.com/UweHeuer/test4.git</code> | |||
====Clone [[GitHub|GitHub]] Repository==== | |||
* go to target directory e.g. C:\Uwes\GitRepositories | * go to target directory e.g. C:\Uwes\GitRepositories | ||
* copy GitHub URL | * copy GitHub URL | ||
| Line 122: | Line 140: | ||
===Delete File from Repository=== | ===Delete File from Repository=== | ||
git rm <File> | git rm <File> | ||
git commit | |||
===Commit Changes=== | ===Commit Changes=== | ||
| Line 135: | Line 154: | ||
===Make an existing Project Directory to a Git Repository/Create Repository=== | ===Make an existing Project Directory to a Git Repository/Create Repository=== | ||
* go into the directory and | * go into the directory and | ||
git init | git init | ||
* this will create .git directory | * this will create .git directory | ||
* then [[Git#Add_File_or_Directory_to_be_tracked|add files]] | * then [[Git#Add_File_or_Directory_to_be_tracked|add files]] | ||
git add <FILENAME> | git add <FILENAME> // e.g. 'git .' or 'git add .\D1\F1.txt' | ||
* if needed connect to a remote repository | * if needed connect to a remote repository | ||
git remote add [shortname] [url] // e.g. git remote add origin https://gitlab.com/UweHeuer/test1.git | 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 | * commit changes | ||
git commit | git commit | ||
| Line 151: | Line 170: | ||
* push branch to remote | * push branch to remote | ||
git push -u origin branch1 // -u important to make branch1 a tracking branch (see git branch -vv) | 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=== | ===Push Something=== | ||
| Line 167: | Line 201: | ||
git branch -vv // shows tracking information for local branches in the form: | 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> | // <BRANCH_NAME> <SHA-1> [<REMOTE BRANCH>: ahead and behind comparison] <LAST_COMMIT_MESSAGE> | ||
git remote show origin // full picture about tracked branches | git remote show origin // full picture about tracked branches and status | ||
====Checkout a branch==== | ====Checkout a branch==== | ||
| Line 178: | Line 212: | ||
====Merge Branches==== | ====Merge Branches==== | ||
A merge of a branch into the master branch (s. [https://git-scm.com/book/de/v1/Git-Branching-Einfaches-Branching-und-Merging here]): | A merge of a branch into the master branch (s. [https://git-scm.com/book/de/v1/Git-Branching-Einfaches-Branching-und-Merging here]): | ||
git | git merge <BRANCHNAME> // <BRANCHNAME> => <CURRENT_BRANCH>, checkout the branch in which the merge should be performed before | ||
git merge | |||
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 | |||
====Compare | diff --git <FILE VERSION A> <FILE VERSION B> | ||
git diff master origin/master | ... | ||
--- <FILE VERSION A> | |||
+++ <FILE VERSION B> | |||
- <CONTENT FILE A> | |||
+ <CONTENT FILE B> | |||
====Track a branch==== | ====Track a branch==== | ||
| Line 196: | Line 238: | ||
git branch -D <BRANCH_NAME> // deletes a local branch, regardless whether it has been merged before | 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 | 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=== | ===Show Configuration=== | ||
git config --list | git config --list | ||
git config --list --show-origin | |||
git config <CONFIG_NAME> // git config user.name | 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=== | ===Show remote Repository=== | ||
| Line 217: | Line 270: | ||
==Installation== | ==Installation== | ||
* [[EDTLaptop1#Git|Windows 10 EDT Laptop]] | |||
* [[EONISLaptopHPNew#Git|Git on EDT Laptop]] | * [[EONISLaptopHPNew#Git|Git on EDT Laptop]] | ||
==Egit== | ==Egit== | ||
* see [[Eclipse#EGit|Eclipse]] | |||
[[ | |||
== | ==Git Gui== | ||
* | * Windows CMD shell | ||
* | git-gui | ||
* | * get overview by 'Repository -> Browse ... Files' | ||
* start [[Git#gitk|gitk]] by 'Repository -> Visualize ...' | |||
==gitk== | ==gitk== | ||
Latest revision as of 17:22, 2 April 2023
Concepts[edit]
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[edit]
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[edit]
Cloning[edit]
Cloning is the process of copying an existing Git repository via the Git tooling.
Checkout[edit]
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[edit]
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[edit]
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[edit]
A git pull does a git fetch followed by a git merge (a fetch does not change the working directory).
Push[edit]
Pushing means sending a new version of the local repository to a remote repositories.
Stash[edit]
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[edit]
A Repository is essentially a folder on the local hard disk which contains the working directory and the metadata folder (git directory). In most cases there is one repository per project but it is possible to have multiple projects in one repo (monorepo approach). 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[edit]
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[edit]
Local Branches in subdirectories below:
.git/refs/heads
Remote Branches in subdirectories below:
.git/refs/remote
Working Directory/Tree[edit]
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[edit]
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[edit]
The HEAD points to head of current local branch.
GitHub[edit]
Although it is not necessary it is very common to use git together with GitHub.
Tag[edit]
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[edit]
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[edit]
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[edit]
Add File or Directory to be tracked or Modified to Stage[edit]
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):
// see table above
git add . // stages new files and modifications, without deletions (on the current directory and its subdirectories)
git add <Filename>
git add <Directoryname> // will add all non-empty recursively
// obviously the --all parameter is not needed
git add -u // stages modifications and deletions, without new files
Clone a repository[edit]
git clone /pfad/zum/repository git clone benutzername@host:/pfad/zum/repository
Clone GitLab Repository[edit]
- 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[edit]
- 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[edit]
git diff --staged
Compare Working directory and Stage[edit]
git diff
Delete File from Repository[edit]
git rm <File> git commit
Commit Changes[edit]
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[edit]
Delete the folder which contains the repository.
Fetch everything from Remote[edit]
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[edit]
- 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[edit]
- 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[edit]
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[edit]
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[edit]
Show Branches[edit]
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[edit]
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[edit]
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[edit]
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[edit]
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[edit]
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[edit]
git reflog --date=local [<BRANCH>] // for full history git log // for commits
Show Configuration[edit]
git config --list git config --list --show-origin git config <CONFIG_NAME> // git config user.name
Rename Directory[edit]
git mv <OLD_NAME> <NEW_NAME> // rename (also) working directory in file system git commit -m "<COMMIT_MESSAGE>" git push // merge ...
Show remote Repository[edit]
git remote -v
Show Status of a Repository/Current Branch[edit]
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[edit]
git stash
Installation[edit]
Egit[edit]
- see Eclipse
Git Gui[edit]
- Windows CMD shell
git-gui
- get overview by 'Repository -> Browse ... Files'
- start gitk by 'Repository -> Visualize ...'