Gitlab CI: git diff shows old modifications - gitlab-ci

I am working on a script launched by gitlab's CI that detects primitive types in code using a git diff.
My script uses the command : git diff origin/master ${CI_COMMIT_SHA}
It's working perfectly on local ie: git diff returns the good changes; linked to my branch.
On the CI git diff is returning a (very) lot of changes that or not linked to my branch.
Do you have any idea about this ?

I fixed this problem using git diff origin/master...origin/${CI_COMMIT_BRANCH}

Related

git command to check if a repository has any uncommitted changes

I need to know if there is a git command using which I can check if there are any uncommitted changes present in a branch for a given repository
If you are only interested in files which are part of the index, the following command shows concise info
git status -suno
http://supercollider.sourceforge.net/wiki/index.php/Developer_cheatsheet_for_git
refer this link for git commands.
show uncommited local changes:
git diff [file]
You might as well use gitk - The Git repository browser

Intellij delete multiple local branches

Using Intellij IDE (I have version 2017.3.5) is there a way to delete multiple local git branches at once
You can delete multiple branches in IntelliJ IDEA directly.
Go to tab Git / Log. There open the Tree view on the left side. Check this picture:
Source: https://youtrack.jetbrains.com/issue/IDEA-131571
// Update Feb 2021:
As mojmir.novak pointed out here: https://stackoverflow.com/a/65954247/1546042 you can do this now in IntelliJ. To remove only merged branches, see answer below:
// Older update:
To clean-up (old) feature branches that have been merged to master you can use the terminal to clean it up.
To delete all local branches that are already merged into the currently checked out branch:
git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d
See https://stackoverflow.com/a/6127884/1546042 for more details.
Cleaning up using run config"
In order to clean up multiple branches at once, using intelliJ. You need to install the Bash Support plugin and use it to create a run config that executes a script with the above command.
Install BashSupport plugin
Create script with the command. (e.g. ~/scripts/clean-branches.sh)
Create a new Bash run config.
Link to the script created in step #2.
Provide working directory of the repo you want to clean.
Run it to clean the branches.
There is a Plugin available for this:
https://plugins.jetbrains.com/plugin/10059-git-branch-cleaner/
To use it once it's installed, in the main menu go to:
VCS > Git > Delete Old Branches
Have been through the pain of cleaning up the unused branches, and found this plugin.
https://plugins.jetbrains.com/plugin/10059-git-branch-cleaner/
But I was not able to see the VCS > Git menu on my Mac - IntelliJ
But was successful in finding a similar option under git > context-menu - Delete old branches...
I am not sure if there is a default option or this is because of the above-mentioned plugin.
Sharing to help others who don't have VCS > Git menu like in my case
It's easy to delete multiple branch on Git Extensions.
http://gitextensions.github.io/

Capistrano, Git, and Rails. (Reset hard head)

So I had been (stupidly) making changes directly on the live server instead of making them on my local machine and deploying them. This messed up my deployment. So now I was to do "git reset --hard".
On my remote server I have a project.git directory (for the repository... which is bare btw) and a project directory (for my actual application).
But when I try to run "git reset --hard" it tells me I'm not on a working tree. If I go into config and change bare to false... it says the same thing.
Ideas?
Found a better solution. :)
First I did a git reset --hard on the local server (since the remote server is just a bare repository.)
Then I did a git commit -a which told me there were no changes but that there were untracked files.
So I did a git add . to add all the files that weren't being tracked.
Finally I ran git commit -a again and git push.
This updated my repository with all the new files and then cap deploy functioned as expected.

Best way to clone a github repository

Ok so here's the scenario.
I made a pretty bad newbie mistake. I was working on some code and didn't create a dev branch to checkout and test in development. So I've made all of these changes to the master branch on my machine that are still broken (in development) and my boss wants me to make some changes to the app in production.
My thoughts on how to do this was:
Rename my project's directory so I have a backup of it
git clone the last remote version back onto my machine
Make the slight changes he wants and re-deploy/commit
Sort out what I've changed in my old version of the app and bring it into a dev branch into the newly cloned master branch.
Any thoughts on this? I really don't want to screw up production right now and I've made too many changes to keep track of to revert to where the code was stable.
No need to go through that much trouble:
git checkout master # get the 'bad' version
git branch bad-master # make a new branch called 'bad-master', cut off master's current state
git fetch # make sure you're up to date with the remote
git reset --hard origin/master # reset your master branch to origin/master's state
<work work work>
git checkout bad-master # when you're ready to work on bad-master again
Note that git reset --hard origin/master will discard any work in your working tree that you haven't checked in... so if you've got work you haven't committed yet, be sure to git stash it.
This is a completely normal situation to wind up in, and one you can easily recover from.
All you need to do is:
Create a new branch pointing to your current version of master
git checkout -b my-feature-branch
Reset your master to the same thing as origin/master
git checkout master
git reset --hard origin/master
That's it. Now your master is the same as it was before you did your feature development, and you have a feature branch set aside which you can later checkout to resume development.

Getting current Git commit version from within Rails app?

How can I retrieve the current Git commit version from within a Ruby on Rails app?
Want to display the Git version (or maybe the last 6 letters or so) to serve as an App version.
Like #meagar said, use backticks to execute the shell command from within your app, but you may find these two commands more useful:
Full hash:
git rev-parse HEAD
First 7 characters of hash:
git rev-parse --short HEAD
You can invoke the git command from within your script:
commit = `git show --pretty=%H`
puts commit
Depending on your environment you may want to use the full path to the git binary, and possibly specify the GIT_DIR via an environment variable or --git-dir.
A more robust solution would be git show --pretty=%H -q. The -q flag quiets the output.
In order to remove the newline that is part of the output, you can use chomp. For example: system('git show --pretty=%H -q').chomp
The selected answer has the potential to actually return the diff when the commit is not a merge commit. Verified on git version 2.16.2.windows.1.
I presume that you want to include the app version in your HTML somewhere? The prerequisite is that you are deploying your repo with Capistrano in the default manner (you are uploading the repo, not sending up an archive file).
You can add some code to the Rails initializer as outlined here. That approach will get the SHA1 from the last commit, and make it available as an environment variable.
The other way to do it is have you Capistrano task generate a static file in the public directory with the commit SHA in it. You could include other info in this file that seems useful.