One of my managing projects is using Git and Jenkins for deployment. In the Jenkins job they have specified a hard reset after git pull operation .I dont understand why it is using there. If there is no codes need to commit it will run the below script in jenkins
git pull origin Production
git reset --hard dbb56hgf
cp -r <jenkins_work_directory> <domain_document_root>```
They probably want to make sure that there are no local changes before running the rest of the script.
The purpose of the “git reset” command is to move the current HEAD to the commit specified (in this case, the dbb56hgf itself, one commit before HEAD and so on)
git reset --hard HEAD (going back to HEAD)
git reset --hard HEAD^ (going back to the commit before HEAD)
git reset --hard HEAD~1 (equivalent to "^")
git reset --hard HEAD~2 (going back two commits before HEAD)
Related
After a recent failure of my hard drive, due to which I have lost 7 days of work, because I forgot about pushing my commits, I decided to create some sort of an automated process that will run and automatically commit and push my changes, since I definetely don't want to trust myself about remebering it.
I didn't want to push into my normal branches on which I worked, because that would create a mess in my git log. A better solution on which I have decided was that I would create a brancha named "backup" to which all my code from my current branch on which I was working, would be pushed to and I would use my backup branch as last resort only.
For example if I am working on a branch feature/add_spinners_for_activities, periodically my code should be copied to the branch "backup" and then pushed to the repo.
I created this script, which I'll schedule to run every 2 hours:
#!/bin/sh
set -x
timestamp() {
date +"at %H:%M:%S on %d/%m/%Y"
}
temp=$(git branch --show-current)
if [[ "$temp" = "backup" ]]
then
echo "error, cannot backup the backup branch"
exit -1
fi
git show-ref --verify --quiet refs/heads/backup
#0 if branch doesn't exist
if [$? == 0]
then
printf "BACKUP DOES NOT EXIST\n"
git stash
git stash branch backup
git checkout backup
git commit -am "Regular auto-commit $(timestamp)"
git push --set-upstream origin backup
git checkout $temp
git stash pop
else
printf "BACKUP EXISTS\n"
git stash
git stash branch temp-backup-branch
git commit -am "Regular auto-commit $(timestamp)"
git checkout backup
git merge -X theirs temp-backup-branch
git branch -D temp-backup-branch
git push --set-upstream origin backup
git checkout $temp
git stash pop
fi
I want to focus on the "else" part, since it is the most important part. Firsty I stash the changes from the branch I am on, then I create a new temporary branch using that stash. After that I commit the files and checkout my "backup" branch. I merge the temporary branch to my backup branch and delete the temporary one. After taht I push the changes to the repo. After that I return to the branch I was working on, and unstash the changes, but unfortunetely I get a merge error.
How is it even possible? My knowledge is probably lacking in this matter, but doesn't executing the command git stash just move my code to the stash and after that I am free to move the changes back to my branch? How is it possible that I even get a merge error, since I don't make any changes on my source branch? What should I do, so I don't have the merge error and what do you think about my solution to the "backup" problem?
I am try to checkout git checkout -f staging this way from widows PowerShell (my operating system is windows 10 )
git checkout -f staging
git checkout -q --track origin/staging
fatal: cannot create directory at 'public/C:\xamppphp8\htdocs\caab-hcc\storage\logs': Invalid argument
I can check out other branch easily from same repository but when I am going to use Staging branch i am getting this error
its Laravel project
> git for-each-ref --format %(refname:short)%00%(upstream:short) refs/heads
> git checkout -q --track origin/staging
fatal: cannot create directory at 'public/C:\xamppphp8\htdocs\caab-hcc\storage\logs': Invalid argument
> git status -z -u
> git symbolic-ref --short HEAD
> git for-each-ref --format=%(refname)%00%(upstream:short)%00%(objectname)%00%(upstream:track) refs/heads/bangla_inpout_field_with_new_api refs/remotes/bangla_inpout_field_with_new_api
> git for-each-ref --sort -committerdate --format %(refname) %(objectname) %(*objectname)
> git remote --verbose
Warning: Failed to watch ref 'c:\xamppphp8\htdocs\caab-hcc-1\.git\refs\remotes\origin\bangla_inpout_field_with_new_api', is most likely packed.
> git config --get commit.template
Without -f flag
git checkout staging
error: The following untracked working tree files would be overwritten by checkout:
bootstrap/cache/config.php
bootstrap/cache/packages.php
bootstrap/cache/routes-v7.php
bootstrap/cache/services.php
Please move or remove them before you switch branches.
Aborting
The error already tells you the problem. You have made changes in your current branch, that will be overwritten by checking out the staging branch.
There are two solutions to your problem:
Remove the changes in your current branch (git checkout .) You will lose your changes.
If you want to also remove added files and you do not care about losing progress, use: git reset --hard;
git clean -df
Use git stash to stash your changes. Your stash can be re-applied at a later point.
Afterwards, git checkout staging should work again.
I've seen lots of posts/answers on how to display all of the local commits for all local branches that have not been commited.
I have a narrow use case, and have not found an answer.
I need to determine from within a bash script, if my CURRENT branch has commits that have not been pushed upstream to the same branch. A count would be fine, but I really just need to know if a push has not yet been done. I don't care about any branches except the current branch, and in this case I've already checked to see if the branch is local (i.e. has not yet set upstream origin).
Mainly, I don't want the commits printed out. I just want to know that the number of unpushed commits is > 0.
The upstream of the current branch is #{u}. # is a synonym for HEAD.
#..#{u} selects the commits which are upstream, but not local. If there are any you are behind. We can count them with git rev-list.
# Move 4 commits behind upstream.
$ git reset --hard #{u}^^^
$ git rev-list #..#{u} --count
4
#{u}..# does the opposite. It selects commits which are local but not upstream.
# Move to upstream
$ git reset --hard #{u}
# Add a commit
$ git commit --allow-empty -m 'test'
[main 6dcf66bda1] test
$ git rev-list #{u}..# --count
1
And if both are 0 you are up to date.
Note: this will only be "up to date" as of your last fetch. Whether you want to combine this with a fetch is up to you, but one should get used to the fact that Git does not regularly talk to the network.
(I've borrowed from ElpieKay's answer).
See gitrevisions for more about #, .. and many ways to select revisions.
Suppose the branch is foo.
git fetch origin foo
git rev-list FETCH_HEAD..foo --count
The 1st command gets the latest head of the foo in the remote repository and stores its commit sha1 in FETCH_HEAD.
git rev-list returns a number. If it's 0, all commits of the foo in the local repository have been included in the foo branch in the remote repository. If it's larger than 0, there are this number of commits not included in the remote foo yet.
Being included is different from being pushed if the procedure involves a pending change like a pull request or a merge request. The commands can determine if the local branch's commits have been merged to(included in) its remote counterpart.
You're looking for git status. It will give you output like:
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
And you can grep for this with:
git status | grep -E "Your branch is ahead of '.*' by ([0-9]*) commit."
I'd like to make an alias like this:
'git pull -f'='MYBRANCH="$(git rev-parse --abbrev-ref HEAD)"; echo "origin/${MYBRANCH}" | git reset --hard'
In simpler terms, it's a git reset --hard origin/my_current_branch.
But bash tells me that git pull -f cannot be a valid alias (probably because there are spaces in it?). Is there a way to get around this?
If I understand correctly, you want to perform the following steps with the alias:
Get the name of your current branch
Fetch the corresponding branch from origin
Hard-reset to the remote branch
Instead of a Bash alias to do this,
I suggest to define a Git alias.
To do that, add this in the [alias] section in your ~/.gitconfig:
myreset = "!f() { br=$(git rev-parse --abbrev-ref HEAD); git fetch origin $br; git reset --hard origin/$br; }; f"
This way, you can run git myreset to perform the operations.
(Feel free to rename "myreset" to whatever you prefer.)
What the title says.
I want to reset each and every local branch to match my Remote repository, including deleting some branches and tags which only exists locally, without having to delete everything and cloning from scratch. All I could find are instructions on how to reset a specific branch, but not the entire repository.
Even better if it can be done from the TortoiseGit Shell extension. But I'm also fine with the command line.
You can do it by following commands:
git checkout --orphan #
git fetch <Remote> refs/*:refs/* --refmap= --prune --force
where <Remote> is remote repository you want to use. You simply fetch all remote refs (refs/*:refs/*) with --prune and --force flags to remove and force update local references.
The following line will reset all local branches that have a configured upstream branch to the state of the upstream branch
git checkout #{0} && git for-each-ref refs/heads --format '%(refname:strip=2)' | xargs -ri sh -c 'git rev-parse {}#{u} >/dev/null 2>&1 && git branch -f {} $(git rev-parse {}#{u})'
You will end up with a detached HEAD due to the first command, because you cannot reset the currently checked out branch, so checkout the branch you want to have in your working directory after doing this.