DevOps Tools/SCM/Git
(38 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
− | [[DevOps_Tools | + | [[DevOps_Tools/Overview|Overview]] | [[DevOps_Tools/CI|Continuous Integration (CI)]] | [[DevOps_Tools/SCM|Source Control Management (SCM)]] | [[DevOps_Tools/Containerization|Containerization]] | [[DevOps_Tools/Configuration|Configuration]] | [[DevOps_Tools/Integration|Integration]] |
− | [[DevOps_Tools\SCM\Git|Git]] | + | =[[DevOps_Tools\SCM\Git|Git]]= |
− | =Install and Configure= | + | ==Install and Configure== |
Done on CentOS 7. Serves as an agentless server and client. You can use this package to create a local repository as well as other git functions. | Done on CentOS 7. Serves as an agentless server and client. You can use this package to create a local repository as well as other git functions. | ||
<nowiki>~$ yum install git</nowiki> | <nowiki>~$ yum install git</nowiki> | ||
Line 8: | Line 8: | ||
~$ sudo git config --global user.email "email address" | ~$ sudo git config --global user.email "email address" | ||
~$ sudo git config --system core.editor "/bin/vim"</nowiki> | ~$ sudo git config --system core.editor "/bin/vim"</nowiki> | ||
− | =Initializing Local Repo= | + | ===Configuration Files=== |
+ | At the time of this writing, Git has (4) potential places for configuration files: | ||
+ | ====System==== | ||
+ | Having the most weight, this is located in the main directory. It applies to the entire system. | ||
+ | <nowiki>$(prefix)/etc/gitconfig</nowiki> | ||
+ | ====Global==== | ||
+ | With the second heaviest weight, this file does not exist by default. It is user specific. | ||
+ | <nowiki>~/.gitconfig</nowiki> | ||
+ | ====Custom==== | ||
+ | Another file that does not exist by default it lives in one of (2) places. It is also user specific. | ||
+ | <nowiki>$XDG_CONFIG_HOME/.config/git/config | ||
+ | $HOME/.config/git/config</nowiki> | ||
+ | ====Repository (Local)==== | ||
+ | And last the repository specific config file, sometimes referred to as local. | ||
+ | <nowiki>$GIT_DIR/config</nowiki> | ||
+ | |||
+ | ==Initializing Local Repo== | ||
Create a local directory with nothing in it. This is called initializing the repository | Create a local directory with nothing in it. This is called initializing the repository | ||
<nowiki>~$ mkdir ~/repo | <nowiki>~$ mkdir ~/repo | ||
Line 16: | Line 32: | ||
<nowiki>~$ git config user.name "user" | <nowiki>~$ git config user.name "user" | ||
~$ git config user.email "[email protected]"</nowiki> | ~$ git config user.email "[email protected]"</nowiki> | ||
− | =Basics= | + | ===git clone=== |
− | ==add== | + | You can also initialize a directory by simply doing a git clone of the repo. Without a specified directory, a new folder will be created based on the name of the repository.<br> |
+ | <nowiki>~$ git clone [email protected]:infrastructure/ansible.git</nowiki> | ||
+ | ===multiple repos/ssh keys=== | ||
+ | Personally, I found the simplest way to deal with multiple repos that need different ssh keys is to utilize a parent <code>.gitconfig</code> that lives in the user home directory along with individual <code>.gitconfig</code> files that live in the parent folder of each project (not the repository folder).<br> | ||
+ | for example: | ||
+ | <nowiki>home/.gitconfig | ||
+ | home/git/project-1/.gitconfig | ||
+ | home/git/project-2/.gitconfig</nowiki> | ||
+ | ====home .gitconfig==== | ||
+ | <nowiki>~$cat $HOME/.gitconfig | ||
+ | # project-1 user | ||
+ | [includeIf "gitdir:~/git/project-1/"] | ||
+ | path = ~/git/project-1/.gitconfig | ||
+ | # project-2 user | ||
+ | [includeIf "gitdir:~/git/project-2/"] | ||
+ | path = ~/git/project-2/.gitconfig</nowiki> | ||
+ | ====parent .gitconfig==== | ||
+ | <nowiki>~$ cat $HOME/git/project-1/.gitconfig | ||
+ | [user] | ||
+ | name = My Project-1 Account Name | ||
+ | email = [email protected] | ||
+ | |||
+ | [core] | ||
+ | sshCommand = "ssh -i ~/.ssh/project-1_id_rsa"</nowiki> | ||
+ | |||
+ | ==Basics== | ||
+ | ===add=== | ||
adds a new file to the repo. | adds a new file to the repo. | ||
<nowiki>~$ git add filename | <nowiki>~$ git add filename | ||
Line 23: | Line 65: | ||
~$ git add . ---- adds everything in the current directory | ~$ git add . ---- adds everything in the current directory | ||
</nowiki> | </nowiki> | ||
− | ==status== | + | ===status=== |
shows the current status of the git repository | shows the current status of the git repository | ||
<nowiki>~$ git status</nowiki> | <nowiki>~$ git status</nowiki> | ||
− | ==commit== | + | ===commit=== |
Commits the changes to the repo | Commits the changes to the repo | ||
<nowiki>~$ git commit -m "comment on commit"</nowiki> | <nowiki>~$ git commit -m "comment on commit"</nowiki> | ||
commit anything that is staged, but not anything that is untracked. | commit anything that is staged, but not anything that is untracked. | ||
<nowiki>~$ git commit -a</nowiki> | <nowiki>~$ git commit -a</nowiki> | ||
− | ==delete== | + | ===delete=== |
deletes a file from the git repo. you must delete the file first. | deletes a file from the git repo. you must delete the file first. | ||
<nowiki>~$ rm filename | <nowiki>~$ rm filename | ||
~$ git rm filename</nowiki> | ~$ git rm filename</nowiki> | ||
− | ==log== | + | ===log=== |
Shows you a log of all the commits and changes. | Shows you a log of all the commits and changes. | ||
<nowiki>~$ git log | <nowiki>~$ git log | ||
Line 44: | Line 86: | ||
~$ git log --graph --decorate ------ shows a directory tree view of the changes. | ~$ git log --graph --decorate ------ shows a directory tree view of the changes. | ||
</nowiki> | </nowiki> | ||
− | ==clone== | + | ===clone=== |
Clones a git repository | Clones a git repository | ||
<nowiki>~$ git clone "local path" | <nowiki>~$ git clone "local path" | ||
~$ git clone user@hostname:repo_directory --- assumes the repo directory is in the remote user's home directory. | ~$ git clone user@hostname:repo_directory --- assumes the repo directory is in the remote user's home directory. | ||
~$ git clone https://github.com/repository/project.git | ~$ git clone https://github.com/repository/project.git | ||
+ | ~$ git clone https://[email protected]/username/repository.git | ||
</nowiki> | </nowiki> | ||
− | ==push== | + | |
+ | ===push=== | ||
push changes to repo | push changes to repo | ||
<nowiki>~$ git push origin master | <nowiki>~$ git push origin master | ||
</nowiki> | </nowiki> | ||
+ | ===pull=== | ||
+ | pull changes from repo to local | ||
+ | <nowiki>~$ git pull origin master</nowiki> | ||
+ | pull changes from a feature branch | ||
+ | <nowiki>~$ git pull origin feature/branch_name</nowiki> | ||
+ | |||
+ | ===ignore=== | ||
+ | Ignore file types in local/specific repository | ||
+ | <nowiki>~$ vim .gitignore | ||
+ | #local git ignore in specific repo | ||
+ | *.filetype | ||
+ | /ignored_directory/* | ||
+ | </nowiki> | ||
+ | ===merge=== | ||
+ | merges two branches together. start by selecting the branch you want to merge another branch into. | ||
+ | <nowiki>~$ git merge branch_name</nowiki> | ||
+ | ===rm=== | ||
+ | remove a file from the repo and local | ||
+ | <nowiki>~$ git rm {file name}</nowiki> | ||
+ | ===reset=== | ||
+ | reset any adds | ||
+ | <nowiki>git reset</nowiki> | ||
+ | reset the local branch and remove any local commits | ||
+ | <nowiki>git reset --hard origin/master</nowiki> | ||
+ | |||
+ | ==Global Settings== | ||
+ | ===exclude=== | ||
+ | Show excluded files | ||
+ | <nowiki>~$ git config --global core.excludesfile</nowiki> | ||
+ | Exclude file/path | ||
+ | <nowiki>~$ git config --global core.excludesfile '/etc/gitignore'</nowiki> | ||
+ | Manually ignore files via exclude file | ||
+ | <nowiki>~$ sudo vim /etc/gitignore | ||
+ | # globally ignore files based on type | ||
+ | *.filetype</nowiki> | ||
+ | |||
+ | ==Branch== | ||
+ | Determine current branch | ||
+ | <nowiki>~$ git branch</nowiki> | ||
+ | Create new branch | ||
+ | <nowiki>~$ git branch new_branch</nowiki> | ||
+ | Select new branch | ||
+ | <nowiki>~$ git checkout branch_name</nowiki> | ||
+ | Create new branch and select at the same time | ||
+ | <nowiki>~$ git checkout -b new_branch</nowiki> | ||
+ | List all branches | ||
+ | <nowiki>~$ git branch -a</nowiki> | ||
+ | |||
+ | =Examples= | ||
+ | ==Clone new project and push initial commit== | ||
+ | Assuming you've created the project in GitLab already. This is also using a non-standard port. | ||
+ | <nowiki>~$ git clone ssh://[email protected]:8922/user/project01.git | ||
+ | ~$ cd ./project01 | ||
+ | ~$ touch README.md | ||
+ | ~$ git add ./README.md | ||
+ | ~$ git commit -m "initial commit" | ||
+ | ~$ git push ssh://[email protected]:8922/user/project01.git master</nowiki> | ||
+ | ==Create a new branch for changes, then merge into master== | ||
+ | <nowiki>~$ git pull origin master | ||
+ | ~$ git status | ||
+ | On branch master | ||
+ | ~$ git checkout -b new_feature_01 #create a new branch and switch to it | ||
+ | Switched to a new branch 'new_feature_01' | ||
+ | ~$ git status | ||
+ | On branch new_feature_01 | ||
+ | ~$ vim ./index.html #new changes | ||
+ | ~$ git add . #add new files | ||
+ | ~$ git commit -m "html index" #commit changes | ||
+ | ~$ git push ssh://[email protected]:8922/user/project01.git new_feature_01 #push changes to branch | ||
+ | ~$ git checkout master #switch back to master branch | ||
+ | ~$ git pull origin master #pull master branch down (overwrites local changes committed to new branch) | ||
+ | ~$ git merge new_feature_01 #merge new branch into master | ||
+ | ~$ git push origin master #commit changes to master | ||
+ | ~$ git branch -d new_feature_01 #delete new branch (local) | ||
+ | ~$ git push origin --delete new_feature_01 #delete new branch (remote) | ||
+ | </nowiki> | ||
+ | |||
+ | ==Update existing branch== | ||
+ | <nowiki>~$ git checkout branch01 | ||
+ | ~$ git add . | ||
+ | ~$ git commit -m "new commit message" | ||
+ | ~$ git push origin branch01</nowiki> |
Latest revision as of 22:06, 21 September 2022
Overview | Continuous Integration (CI) | Source Control Management (SCM) | Containerization | Configuration | Integration
Contents |
[edit] Git
[edit] Install and Configure
Done on CentOS 7. Serves as an agentless server and client. You can use this package to create a local repository as well as other git functions.
~$ yum install git
Setup global parameters
~$ sudo git config --global user.name "global username" ~$ sudo git config --global user.email "email address" ~$ sudo git config --system core.editor "/bin/vim"
[edit] Configuration Files
At the time of this writing, Git has (4) potential places for configuration files:
[edit] System
Having the most weight, this is located in the main directory. It applies to the entire system.
$(prefix)/etc/gitconfig
[edit] Global
With the second heaviest weight, this file does not exist by default. It is user specific.
~/.gitconfig
[edit] Custom
Another file that does not exist by default it lives in one of (2) places. It is also user specific.
$XDG_CONFIG_HOME/.config/git/config $HOME/.config/git/config
[edit] Repository (Local)
And last the repository specific config file, sometimes referred to as local.
$GIT_DIR/config
[edit] Initializing Local Repo
Create a local directory with nothing in it. This is called initializing the repository
~$ mkdir ~/repo ~$ git init ~/repo Initialized empty Git repository in /home/user/repo/.git/
Set repo user & email, if you want something different from the global.
~$ git config user.name "user" ~$ git config user.email "[email protected]"
[edit] git clone
You can also initialize a directory by simply doing a git clone of the repo. Without a specified directory, a new folder will be created based on the name of the repository.
~$ git clone [email protected]:infrastructure/ansible.git
[edit] multiple repos/ssh keys
Personally, I found the simplest way to deal with multiple repos that need different ssh keys is to utilize a parent .gitconfig
that lives in the user home directory along with individual .gitconfig
files that live in the parent folder of each project (not the repository folder).
for example:
home/.gitconfig home/git/project-1/.gitconfig home/git/project-2/.gitconfig
[edit] home .gitconfig
~$cat $HOME/.gitconfig # project-1 user [includeIf "gitdir:~/git/project-1/"] path = ~/git/project-1/.gitconfig # project-2 user [includeIf "gitdir:~/git/project-2/"] path = ~/git/project-2/.gitconfig
[edit] parent .gitconfig
~$ cat $HOME/git/project-1/.gitconfig [user] name = My Project-1 Account Name email = [email protected] [core] sshCommand = "ssh -i ~/.ssh/project-1_id_rsa"
[edit] Basics
[edit] add
adds a new file to the repo.
~$ git add filename ~$ git add * ---- adds everything in the current directory ~$ git add . ---- adds everything in the current directory
[edit] status
shows the current status of the git repository
~$ git status
[edit] commit
Commits the changes to the repo
~$ git commit -m "comment on commit"
commit anything that is staged, but not anything that is untracked.
~$ git commit -a
[edit] delete
deletes a file from the git repo. you must delete the file first.
~$ rm filename ~$ git rm filename
[edit] log
Shows you a log of all the commits and changes.
~$ git log ~$ git log --oneline ----- shows the logs in a single line ~$ git log -p ----- shows details of the changes per line per log. ~$ git log --author="username" ------- shows logs of a certain user ~$ git log --grep="search term" ------- shows logs of changes that match the search term ~$ git log --graph --decorate ------ shows a directory tree view of the changes.
[edit] clone
Clones a git repository
~$ git clone "local path" ~$ git clone user@hostname:repo_directory --- assumes the repo directory is in the remote user's home directory. ~$ git clone https://github.com/repository/project.git ~$ git clone https://[email protected]/username/repository.git
[edit] push
push changes to repo
~$ git push origin master
[edit] pull
pull changes from repo to local
~$ git pull origin master
pull changes from a feature branch
~$ git pull origin feature/branch_name
[edit] ignore
Ignore file types in local/specific repository
~$ vim .gitignore #local git ignore in specific repo *.filetype /ignored_directory/*
[edit] merge
merges two branches together. start by selecting the branch you want to merge another branch into.
~$ git merge branch_name
[edit] rm
remove a file from the repo and local
~$ git rm {file name}
[edit] reset
reset any adds
git reset
reset the local branch and remove any local commits
git reset --hard origin/master
[edit] Global Settings
[edit] exclude
Show excluded files
~$ git config --global core.excludesfile
Exclude file/path
~$ git config --global core.excludesfile '/etc/gitignore'
Manually ignore files via exclude file
~$ sudo vim /etc/gitignore # globally ignore files based on type *.filetype
[edit] Branch
Determine current branch
~$ git branch
Create new branch
~$ git branch new_branch
Select new branch
~$ git checkout branch_name
Create new branch and select at the same time
~$ git checkout -b new_branch
List all branches
~$ git branch -a
[edit] Examples
[edit] Clone new project and push initial commit
Assuming you've created the project in GitLab already. This is also using a non-standard port.
~$ git clone ssh://[email protected]:8922/user/project01.git ~$ cd ./project01 ~$ touch README.md ~$ git add ./README.md ~$ git commit -m "initial commit" ~$ git push ssh://[email protected]:8922/user/project01.git master
[edit] Create a new branch for changes, then merge into master
~$ git pull origin master ~$ git status On branch master ~$ git checkout -b new_feature_01 #create a new branch and switch to it Switched to a new branch 'new_feature_01' ~$ git status On branch new_feature_01 ~$ vim ./index.html #new changes ~$ git add . #add new files ~$ git commit -m "html index" #commit changes ~$ git push ssh://[email protected]:8922/user/project01.git new_feature_01 #push changes to branch ~$ git checkout master #switch back to master branch ~$ git pull origin master #pull master branch down (overwrites local changes committed to new branch) ~$ git merge new_feature_01 #merge new branch into master ~$ git push origin master #commit changes to master ~$ git branch -d new_feature_01 #delete new branch (local) ~$ git push origin --delete new_feature_01 #delete new branch (remote)
[edit] Update existing branch
~$ git checkout branch01 ~$ git add . ~$ git commit -m "new commit message" ~$ git push origin branch01