Showing posts with label Git. Show all posts
Showing posts with label Git. Show all posts

Wednesday, March 11, 2020

Read and Updated the content of a file of a GitLab with GitLab API using Python

 Below is the script to Read and Updated the content of the files in GitLab with GitLab API using Python

You need to install the  "gitlab" Python package using pip before run the script.

#### install gitlab python package with root user
### pip install gitlab

import gitlab
import base64

gl = gitlab.Gitlab('https://xxxxxxxxxxxxxxxxxxxx/', private_token='xxxxxxxxxxxxxxxx')
gl.auth()

project = gl.projects.get(PROJECT_ID)  ####You can get the project PROJECT_ID from GITLab repo

f = project.files.get(file_path='pom.xml', ref='master')
file_content = f.decode().decode('utf-8')

print("====================BEFORE UPDATE================")
print(file_content)

old_line = '<groupId>com.test3</groupId>'
new_line = '<groupId>com.test4</groupId>'
new_file = file_content.replace(old_line, new_line)

print("====================AFTER UPDATE================")
print(new_file)

### COMMIT the changes to GIT
data = {
'branch': 'master',
'commit_message': 'TEST GITLABAPI',
'actions': [
{
'action': 'update',
'file_path': 'pom.xml',
'content': new_file,
}
]
}
commit = project.commits.create(data)

Saturday, January 4, 2020

GIT logs Useful Commands

For full path names of changed files:
git log --name-only
For full path names and status of changed files:
git log --name-status
For abbreviated pathnames and a diffstat of changed files:
git log --sta

Sunday, December 16, 2018

GIT - Setup Line Ending Preferences

core.autocrlf


If you’re programming on Windows and working with people who are not (or vice-versa), you’ll probably run into line-ending issues at some point. This is because Windows uses both a carriage-return character and a linefeed character for newlines in its files, whereas Mac and Linux systems use only the linefeed character. This is a subtle but incredibly annoying fact of cross-platform work; many editors on Windows silently replace existing LF-style line endings with CRLF, or insert both line-ending characters when the user hits the enter key.

Git can handle this by auto-converting CRLF line endings into LF when you add a file to the index, and vice versa when it checks out code onto your filesystem. You can turn on this functionality with the core.autocrlf setting. If you’re on a Windows machine, set it to true — this converts LF endings into CRLF when you check out code:

f you’re a Windows programmer doing a Windows-only project, then you can turn off this functionality, recording the carriage returns in the repository by setting the config value to false:

Unix/Mac users:

git config --global core.autocrlf input

git config --global core.safecrlf true

And for Windows users:

git config --global core.autocrlf true

git config --global core.safecrlf true


Saturday, December 15, 2018

Git for Beginners

GIT Setup

Configuring user information used across all local repositories.

git config --global user.name "First-Name Last-Name"
Set a name that is identifiable for credit when review version history.

git config --global user.email "Valid-EmailID"
Set an email address that will be associated with each history marker.

git config --global color.ui auto
Set automating command line coloring for Git easy reviewing.


GIT Setup and Initializing

Configuring users information, Initializing and Cloning Repositories

git init
Initializing existing directory as a Git Repository.

git clone [URL]
retrieve an empty repository from a hosted location via URL.


GIT Staging and Snapshots

Working with GIT Snapshots and GIT Staging area.

git status
Show modified files in working directory, stage for your next commit.

git add [filename[s]]
Add a file or files as it looks now to your next commit(stage).

git reset [file]
Unstage a file while retaining changes the changes in working directory.

git diff
diff of what changed but not staged.

git diff --staged
diff of what staged but not yet committed.

git commit -m ["Commit description"]
Commit your staged content as a new commit snapshot.

GIT Branching and Merging

Isolating work in branches, Changing context, and integrating changes.

git branch
list your branches. a * will appear next to the currently active branch.

git branch [branch-name]
create a new branch at the current commit.

git checkout
switch to another branch and check it out into your working directory.

git merge [branch]
merge the specified branch’s history into the current one.

git log
show all commits in the current branch’s history.

GIT INSPECT & COMPARE


Examining logs, diffs and object information

git log
show the commit history for the currently active branch.

git log branchB..branchA
show the commits on branchA that are not on branchB.

git log --follow [file]
show the commits that changed file, even across renames.

git diff branchB...branchA
show the diff of what is in branchA that is not in branchB.

git show [SHA]
show any object in Git in human-readable format.

GIT TRACKING PATH CHANGES


Versioning file removes and path changes.

git rm [file]
delete the file from project and stage the removal for commit.

git mv [existing-path] [new-path]
change an existing file path and stage the move.

git log --stat -M
show all commit logs with indication of any paths that moved.

GIT IGNORING PATTERNS


Preventing unintentional staging or commiting of files.

logs/
*.notes
pattern*/
Save a file with desired paterns as .gitignore with either direct string matches or wildcard globs.

git config --global core.excludesfile [file]
system wide ignore patern for all local repositories.

GIT SHARE & UPDATE


Retrieving updates from another repository and updating local repos.

git remote add [alias] [url]
add a git URL as an alias.

git fetch [alias]
fetch down all the branches from that Git remote.

git merge [alias]/[branch]
merge a remote branch into your current branch to bring it up to date.

git push [alias] [branch]
Transmit local branch commits to the remote repository branch.

git pull
fetch and merge any commits from the tracking remote branch.

GIT REWRITE HISTORY


Rewriting branches, updating commits and clearing history.

git rebase [branch]
apply any commits of current branch ahead of specified one.

git reset --hard [commit]
clear staging area, rewrite working tree from specified commit.

GIT TEMPORARY COMMITS


Temporarily store modified, tracked files in order to change branches.

git stash
Save modified and staged changes.

git stash list
list stack-order of stashed file changes.

git stash pop
write working from top of stash stack.

git stash drop

discard the changes from top of stash stack.

GIT GUI Clients

Git comes with built-in GUI tools for committing (git-gui) and browsing (gitk), but there are several third-party tools for users looking for platform-specific experience.

If you want to add another GUI Tools follow the below link.

GIT GUI Clients

GIT : Aliasing commands

Git aliasing is one of the good feature in GIT. You can create your own GIT Commands from combination of other Git commands and other OS commands with help of Git aliasing. you an define the Git alias commands in git configuration in the alias section.

Let create an alias for 'git status'.

git config --global alias.s "status"



Let's create single command for 'git status' and 'ls -lrth'

git config --global alias.s '!git status && ls -lrth'



These configuration are stored under "~/.gitconfig".







Monday, December 10, 2018

Show current git branch in command line

Add the below lines in ~/.bash_profile and run the 'source ~/.bash_profile'.

parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

In here parse_git_branch() function extract the branch name when your are in git repository. 
$ git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
 (master)

function output used in PS1 variable in order to prompt the branch name.

In above PS1 we defined following properties
* \u@\h \[\033[32m\] - user, host name and its displaying color
* \w\[\033[33m\] - current working directory and its displaying color
* \$(parse_git_branch)\[\033[00m\] - git branch name and its displaying color

Saturday, December 8, 2018

Git Commands

git init - to create empty git repository.
git status - to show the modified files in git repo.
git add <filename> - to add the file to repo.
git commit -m "commit message" - to commit the changes to repo.
.gitignore - specify the type of files you want to excluded from the git while push.
echo '*.tmp' > .gitignore
git add .gitignore
git commit -m "gitignore file"
git -f add <file> - to add the gitignore files forcefully.
Your environment is currently being packaged as a Docker container and the download will begin shortly. To run the image locally, once Docker has been installed, use the commands
cat scrapbook_git_1_container.tar | docker load
docker run -it yourmail@gmail.com/git_1:20180626