Git

Installation

The git version control system is installed with the following command:

apt-get install git

Commands

git init                                                 #Initialize repository in actual working directory
git config --global user.email "you@example.com"         #set email
git config --global user.name "Your Name"                #Set your account's default identity                              # stored in ~home/<username>/.gitconfig 
git config user.name "Your Name"                         #Set the identity only in this repository
git status                                               #Show which files tracked and active branch
git tag --list                                           #show tags
git tag                                                  #show tags
git branch                                               #show acitve branch
git add file                                             #Add file "file"
git rm --cached myfile                                   #remove file which was previously added
git add .                                                #Add all files in working directory
git commit -m "initial commit"                           #Commit all files with comment
git log                                                  #Show history
git revert ID                                            #Revert to change from ID - other changes after not affected
git checkout ID file                                     #Revert to change from ID
git diff                                                 #Show diff/ changes
git reset --hard HEAD~1                                  #remove last commit
git reset --hard HEAD~2                                  #remove 2 commits
git checkout -b branch-name                              #creates "local" branch, which will be uploaded by "git pull"
tag = read only branch nie wieder veränderbar/ version
git tag --list
git branch --list                                        #show local branches
git branch --all                                         #show all branches and remote
git fetch --prune                                        #update remote branches and also delete branches not existing on repository anymore
git fetch                                                #update remote branches
git checkout <existing_branch>                           #change branch
git checkout -b <new_branch>
git log -- path/to/file
git reset HEAD <file>...                                 #to unstag

Tag: Readonly version of master (version). Tags give the ability to mark specific points in history as being important.

Revert:

git reset --hard commit-ID
git commit -m "revert"

scripts

Push:

root@kmaster:/scripts# cat git-push.sh
#/bin/bash

read -p "Please enter commit message: " COMMIT
echo "Commit message:  \"$COMMIT\""

git add .
git commit -m "${COMMIT}"
#git commit -m update
git push -u origin master

Konfiguration

Every git user should first introduce himself to git, by running these two commands:

git config --global user.email "you@example.com"                                      #set email
git config --global user.email                                                        #get email
git config --global user.name "Your Name"                                             #set user name
git config --global user.name                                                         #get user name
git config -l --show-origin                                                           #show all confg

Basic usage

The above is already sufficient to use git in a distributed and secure way, provided users have access to the machine assuming the server role via SSH. On the server machine, creating a new repository can be done with:

git init --bare /path/to/repository

Note:This creates a bare repository, that cannot be used to edit files directly. If you would rather have a working copy of the contents of the repository on the server, ommit the –bare option.

Any client with SSH access to the machine can then clone the repository with:

git clone username@hostname:/path/to/repository

Once cloned to the client's machine, the client can edit files, then commit and share them with:

cd /path/to/repository
#(edit some files
git commit -a # Commit all changes to the local version of the repository
git push origin master # Push changes to the server's version of the repository

Remote git

eval $(ssh-agent -s)
ssh-add /home/myuser/.ssh/id_rsa
git remote add gitlab git@git.local:myuser/myrepo.git                #"gitlab" = name you can choose
git remote add origin ssh://git@hostname:8822/tmade/myrepo.git
git remote set-url origin ssh://git@hostname:8822/tmade/myrepo.git   #rename origin
git remote                                                           #check, if remote location exists (test/ prod)
git remote -v
git remote remove origin                                             #remove remote "origin"
git add .                                                            #or "git add kubeadm-config.yaml"
git commit -m "initial commit"                                       #to the previously created project on e. g. gitlab
git push gitlab master                                               #update changes to master
git pull origin master                                               #pull changes from master

.gitignore

# Ignore everything
*

# But not these files...
!.gitignore
!hosts
!resolv.conf
linux/git.txt · Last modified: 2024/09/24 15:39 by tmade
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 4.0 International
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki