Size: 4685
Comment:
|
← Revision 46 as of 2025-02-16 10:35:44 ⇥
Size: 8707
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 7: | Line 7: |
* ls ~/.ssh * ssh-keygen # create ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub keys * cat ~/.ssh/id_rsa.pub # shows key to be sent to the git server admin |
{{{#!highlight sh ls ~/.ssh ssh-keygen # create ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub keys cat ~/.ssh/id_rsa.pub # shows key to be sent to the git server admin }}} |
Line 12: | Line 14: |
* git config --global user.name "John Doe" * git config --global user.email johndoe@example.com * git config --list |
{{{#!highlight sh git config --global user.name "John Doe" git config --global user.email johndoe@example.com git config --list }}} |
Line 17: | Line 21: |
* git config user.name "John Doe" * git config user.email johndoe@example.com * git config --local --list * cat .git/config # [user] |
{{{#!highlight sh git config user.name "John Doe" git config user.email johndoe@example.com git config --local --list cat .git/config # [user] }}} |
Line 23: | Line 29: |
* cd <project> * git init #create folder .git * touch README.md * touch .gitignore * git add . * git commit -m 'Initial commit' * git remote add origin git@gitserver:/opt/git/project.git # setup origin for repository * git push origin master Edit .gitignore #for Java |
{{{#!highlight sh cd <project> git init #create folder .git touch README.md touch .gitignore git add . git commit -m 'Initial commit' git remote add origin git@gitserver:/opt/git/project.git # setup origin for repository git push origin master }}} Edit .gitignore for Java |
Line 52: | Line 59: |
* git clone git@git.server.net:repository.git # by ssh * git clone https://git.server.net/repository.git #by https * cd repository |
{{{#!highlight sh git clone git@git.server.net:repository.git # by ssh git clone ssh://git@git.server.net:1234/home/git/repository.git git clone https://git.server.net/repository.git #by https cd repository }}} |
Line 57: | Line 67: |
* git branch #show current branch * git checkout -b B/20130801/BRANCH-XYZ master # create new branch based on branch master * git branch -m B/20130801/BRANCH-XYZ B/20130801/BRANCH-ZZZ # rename local branch * git push origin B/20130801/BRANCH-ZZZ # push branch B/20130801/BRANCH-ZZZ to remote |
{{{#!highlight sh git branch #show current branch git checkout -b B/20130801/BRANCH-XYZ master # create new branch based on branch master git branch -m B/20130801/BRANCH-XYZ B/20130801/BRANCH-ZZZ # rename local branch git push origin B/20130801/BRANCH-ZZZ # push branch B/20130801/BRANCH-ZZZ to remote }}} |
Line 63: | Line 75: |
* git commit -am 'Message commit xyz ' --all #commit locally * git pull origin B/20130801/BRANCH-ZZZ # sync with remote branch # fix conflicts and commit conflict resolution * git push origin B/20130801/BRANCH-ZZZ # send changes to remote branch |
{{{#!highlight sh git commit -am 'Message commit xyz ' --all #commit locally git pull origin B/20130801/BRANCH-ZZZ # sync with remote branch # fix conflicts and commit conflict resolution git push origin B/20130801/BRANCH-ZZZ # send changes to remote branch git commit -am 'Message commit asdf ' --all #commit locally git pull --rebase origin B/20130801/BRANCH-ZZZ # sync with remote branch with rebase # fix conflicts and commit conflict resolution git push origin B/20130801/BRANCH-ZZZ # send changes to remote branch }}} |
Line 69: | Line 87: |
* git checkout master # merge destination branch * git pull origin master # sync dest branch * git merge --no-ff B/20130801/BRANCH-ZZZ # merge branch B/20130801/BRANCH-ZZZ to branch master * git pull origin master * git push origin master |
{{{#!highlight sh git checkout master # merge destination branch git pull origin master # sync dest branch git merge --no-ff B/20130801/BRANCH-ZZZ # merge branch B/20130801/BRANCH-ZZZ to branch master git pull origin master git push origin master }}} {{{#!highlight sh git checkout development git checkout -b feature_branch origin/feature_branch git add . git commit -m "msg" # commit in feature branch git checkout development git pull origin development git checkout feature_branch git merge --no-ff development git pull origin feature_branch git push origin feature_branch }}} |
Line 76: | Line 109: |
* cd <projectFolder> * git rm -r -f --cached . * git add . * git status #check files to be commited = Setup git server on RHEL = |
{{{#!highlight sh cd <projectFolder> git rm -r -f --cached . git add . git status #check files to be commited }}} = Setup git server on RHEL/Debian 12 bookworm = |
Line 84: | Line 119: |
{{{#!highlight bash | Home folder for git user must have permissions only for that user. {{{#!highlight sh chown git . -R chgrp git . -R }}} The bare repositories folders must have permissions only for git user. {{{#!highlight sh chown git . -R chgrp git . -R }}} {{{#!highlight sh |
Line 91: | Line 139: |
git --version # git version 1.7.1 | git --version # git version 1.7.1 git version 2.39.5 |
Line 94: | Line 142: |
ssh-keygen -f id_rsa -p | # ssh-keygen -f id_rsa -p ssh-keygen -t rsa |
Line 112: | Line 161: |
chown git . -R chgrp git . -R |
|
Line 119: | Line 170: |
touch README | touch README.md git config --global user.email "you@example.com" git config --global user.name "Your Name" |
Line 123: | Line 176: |
git remote add origin git@bitarus.allowed.org:/home/git/hello.git | git remote add origin ssh://git@bitarus.allowed.org:1234/home/git/hello.git |
Line 130: | Line 183: |
* git checkout -- testFile.txt | {{{#!highlight sh git checkout -- testFile.txt }}} |
Line 133: | Line 188: |
* git remote -v # check current remote repository * git remote set-url origin git@github.com:user/project.git |
{{{#!highlight sh git remote -v # check current remote repository git remote set-url origin git@github.com:user/project.git }}} |
Line 137: | Line 194: |
* git branch -d development * git checkout -b development origin/development # get local branch from remote branch |
{{{#!highlight sh git branch -d development git checkout -b development origin/development # get local branch from remote branch }}} |
Line 141: | Line 200: |
* git checkout development * git branch # check current branch * git checkout master * git branch # check current branch |
{{{#!highlight sh git checkout development git branch # check current branch git checkout master git branch # check current branch }}} |
Line 147: | Line 208: |
* git fetch origin * git checkout -b wiki origin/wiki # local branch wiki from remote origin/wiki |
{{{#!highlight sh git fetch origin git checkout -b wiki origin/wiki # local branch wiki from remote origin/wiki }}} |
Line 151: | Line 214: |
* git reset --hard HEAD | {{{#!highlight sh git reset --hard HEAD }}} |
Line 154: | Line 219: |
* git config --global core.pager 'less -R' | {{{#!highlight sh git config --global core.pager 'less -R' }}} |
Line 157: | Line 224: |
* git commit --amend | {{{#!highlight sh git commit --amend }}} |
Line 160: | Line 229: |
* git diff --name-only hash1 hash2 * git diff --name-only hash1 HEAD |
{{{#!highlight sh git diff --name-only hash1 hash2 git diff --name-only hash1 HEAD }}} |
Line 164: | Line 235: |
* git clean -n -d * git clean -f -d |
{{{#!highlight sh git clean -n -d git clean -f -d }}} |
Line 169: | Line 242: |
* git fetch origin * git reset --hard origin/master |
{{{#!highlight sh git fetch origin git reset --hard origin/master }}} |
Line 174: | Line 249: |
== Join several commits not pushed == Use the git rebase. {{{#!highlight sh git rebase -i origin/master }}} Pick (choose) several commits and put them all with the "squash" commit that usually is the most recent one. It may be cancelled using '''git rebase --abort'''. == Difference between remote and local branch == {{{#!highlight sh git diff origin/master..master }}} == GitFlow == * https://datasift.github.io/gitflow/IntroducingGitFlow.html {{{ New development (new features, non-emergency bug fixes) are built in feature branches: Feature branches are branched off of the develop branch, and finished features and fixes are merged back into the develop branch when they’re ready for release When it is time to make a release, a release branch is created off of develop: The code in the release branch is deployed onto a suitable test environment, tested, and any problems are fixed directly in the release branch. This deploy -> test -> fix -> redeploy -> retest cycle continues until you’re happy that the release is good enough to release to customers. When the release is finished, the release branch is merged into master and into develop too, to make sure that any changes made in the release branch aren’t accidentally lost by new development. The master branch tracks released code only. The only commits to master are merges from release branches and hotfix branches. Hotfix branches are used to create emergency fixes: They are branched directly from a tagged release in the master branch, and when finished are merged back into both master and develop to make sure that the hotfix isn’t accidentally lost when the next regular release occurs. }}} == Create local branch from commit in current branch == {{{#!highlight sh git checkout -b test <commit sha1> }}} == Show differences in added/staged files with git add == {{{#!highlight sh git add filex git status git diff --staged git diff --cached }}} == Stash == {{{#!highlight sh git stash git stash list git stash show -p > ~/Documents/stash_diff.txt less ~/Documents/stash_diff.txt git stash pop git stash clear # clear all stashes }}} == Update old commit message already pushed == {{{#!highlight sh # mark as reword the commit to update git rebase -i HEAD~2 reword 9b5ca7af9 Old commit pick 7331d8be5 [prefix] good commit with prefix # will show new console to add the missing prefix # [prefix] Old commit git log # replace pick with reword each commit that needs to be changed git push origin develop --force }}} |
git
Git is a distributed version control and source code management (SCM) system.
SSH public key generation
Config
Local config for repository
Create repository
Edit .gitignore for Java
- *.jar
- *.war
- *.ear
- *.zip
Sample .gitignore
*~ .metadata *.class *.jar *.war *.ear *.zip target/
Clone repository
Branch creation based on current branch
Commit to remote branch
1 git commit -am 'Message commit xyz ' --all #commit locally
2 git pull origin B/20130801/BRANCH-ZZZ # sync with remote branch
3 # fix conflicts and commit conflict resolution
4 git push origin B/20130801/BRANCH-ZZZ # send changes to remote branch
5 git commit -am 'Message commit asdf ' --all #commit locally
6 git pull --rebase origin B/20130801/BRANCH-ZZZ # sync with remote branch with rebase
7 # fix conflicts and commit conflict resolution
8 git push origin B/20130801/BRANCH-ZZZ # send changes to remote branch
9
Merge to other branch
1 git checkout development
2 git checkout -b feature_branch origin/feature_branch
3 git add .
4 git commit -m "msg" # commit in feature branch
5 git checkout development
6 git pull origin development
7 git checkout feature_branch
8 git merge --no-ff development
9 git pull origin feature_branch
10 git push origin feature_branch
Reapply .gitignore
Setup git server on RHEL/Debian 12 bookworm
http://git-scm.com/book/en/Git-on-the-Server-Setting-Up-the-Server
Home folder for git user must have permissions only for that user.
The bare repositories folders must have permissions only for git user.
1 sudo bash
2 adduser git
3 su git
4 cd
5 mkdir .ssh
6 cd .ssh
7 git --version # git version 1.7.1 git version 2.39.5
8
9 #generate and paste the pub key from my user on local machine
10 # ssh-keygen -f id_rsa -p
11 ssh-keygen -t rsa
12 cat ~/.ssh/id_rsa.pub #local machine
13
14 # in the git server
15 cd .ssh # git account
16 vim authorized_keys #git server, paste the content of id_rsa.pub
17 cd ~
18 chmod 700 .ssh/
19 chmod 600 .ssh/authorized_keys
20 ssh git@bitarus.allowed.org # test connection from local machine
21 vim /etc/passwd #change shell for git user
22 git:x:505:505::/home/git:/usr/bin/git-shell
23
24 # create project on git server
25 cd /home/git
26 mkdir hello.git
27 cd hello.git
28 git --bare init
29 chown git . -R
30 chgrp git . -R
31
32 #create project on local machine
33 cd ~
34 cd gitRepository
35 mkdir hello
36 cd hello
37 git init
38 touch README.md
39 git config --global user.email "you@example.com"
40 git config --global user.name "Your Name"
41 git add .
42 git commit -m 'initial commit'
43 git config --list #check config
44 git remote add origin ssh://git@bitarus.allowed.org:1234/home/git/hello.git
45 git push origin master
46 git log
Revert file to original
1 git checkout -- testFile.txt
Github change from https to ssh
Delete local branch development and checkout remote development branch
Switch between branches
Get remote branch to local branch
Revert to HEAD revision
1 git reset --hard HEAD
Make colors show properly on git diff and git log
1 git config --global core.pager 'less -R'
Change commit message before a push
1 git commit --amend
Files in branch changed between revisions
Clean untracked files and folders
Reset to version in remote repository
Git for windows (git bash)
Join several commits not pushed
Use the git rebase.
1 git rebase -i origin/master
Pick (choose) several commits and put them all with the "squash" commit that usually is the most recent one. It may be cancelled using git rebase --abort.
Difference between remote and local branch
1 git diff origin/master..master
GitFlow
New development (new features, non-emergency bug fixes) are built in feature branches: Feature branches are branched off of the develop branch, and finished features and fixes are merged back into the develop branch when they’re ready for release When it is time to make a release, a release branch is created off of develop: The code in the release branch is deployed onto a suitable test environment, tested, and any problems are fixed directly in the release branch. This deploy -> test -> fix -> redeploy -> retest cycle continues until you’re happy that the release is good enough to release to customers. When the release is finished, the release branch is merged into master and into develop too, to make sure that any changes made in the release branch aren’t accidentally lost by new development. The master branch tracks released code only. The only commits to master are merges from release branches and hotfix branches. Hotfix branches are used to create emergency fixes: They are branched directly from a tagged release in the master branch, and when finished are merged back into both master and develop to make sure that the hotfix isn’t accidentally lost when the next regular release occurs.
Create local branch from commit in current branch
1 git checkout -b test <commit sha1>
Show differences in added/staged files with git add
Stash
Update old commit message already pushed
1 # mark as reword the commit to update
2 git rebase -i HEAD~2
3 reword 9b5ca7af9 Old commit
4 pick 7331d8be5 [prefix] good commit with prefix
5 # will show new console to add the missing prefix
6 # [prefix] Old commit
7 git log
8 # replace pick with reword each commit that needs to be changed
9 git push origin develop --force