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

Git errors: cannot checkout branch - error: pathspec 'branch_name' did not match any file(s) known to git

 git checkout branch_name

error: pathspec 'branch_name' did not match any file(s) known to git

To fix that you can remove remote origin and link it again.

First, check the remote origin:

git remote -v
origin  git@github.com:company/project_name (fetch)
origin  git@github.com:company/project_name (push)


Then remove origin:

git remote remove origin


And add remote origin again with correct path from your repository (copy from

GitHub/GitLab/etc.):

git remote add origin git@github.com:company/project_name.git


After that run:

git pull --ff-only


And set upstream to origin branch:

git branch --set-upstream-to=origin/current_branch


Steps:

1. git remote -v
2. git remote remove origin
3. git remote add origin git@github.com:company/project_name.git
4. git pull --ff-only
5. git branch --set-upstream-to=origin/current_branch


How can I Remove .DS_Store files from a Git repository?

 Remove existing .DS_Store files from the repository:

find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

Add this line:

.DS_Store

to the file .gitignore, which can be found at the top level of your repository (or create the file if it isn't there already). You can do this easily with this command in the top directory:

echo .DS_Store >> .gitignore

Then commit the file to the repo:

git add .gitignore
git commit -m '.DS_Store banished!'

how to avoid being asked "Enter passphrase for key " when I'm doing ssh operation on a remote host?

 On Mac, add UseKeyChain to ~/.ssh/config


nano ~/.ssh/config

and add the following


Host *

    UseKeychain yes

Clean un tracked files git

Here are some more options for you to delete directories, files, ignored and non-ignored files
  • To remove directories, run git clean -f -d or git clean -fd
  • To remove ignored files, run git clean -f -X or git clean -fX
  • To remove ignored and non-ignored files, run git clean -f -x or git clean -fx
Note the case difference on the X for the two latter commands.

Delete all local branches in GIT

git for-each-ref --format '%(refname:short)' refs/heads | grep -v master | xargs git branch -D

Show GIT Branch on Terminal


Add the below lines into your ~/.bash_prfoile


result
[abc] (scratch/STRY51031697) $ 


function git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1) /'
}
PS1='\[\033[0;37m\][\[\033[01;36m\]\W\[\033[0;37m\]] \[\033[0;32m\]$(git_branch)\[\033[00m\]\$ '


If you are using zsh (echo $SHELL), then use below.

# Add colors for git repo to your terminal
# Load version control information
autoload -Uz vcs_info
precmd() { vcs_info }

# Format the vcs_info_msg_0_ variable
zstyle ':vcs_info:git:*' formats '>%b'

# Set up the prompt (with git branch name)
setopt PROMPT_SUBST
PROMPT='${PWD/#$HOME/~}${vcs_info_msg_0_}> '

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

setopt PROMPT_SUBST
PROMPT='%9c%{%F{green}%}$(parse_git_branch)%{%F{none}%} $ '

#end

Git fetch is not fetching all branches. error: pathspec did not match any file(s) known to git.

The problem can be seen when checking the remote.origin.fetch setting
(The lines starting with $ are bash prompts with the commands I typed. The other lines are the resulting output)
$ git config --get remote.origin.fetch
+refs/heads/master:refs/remotes/origin/master
As you can see, in my case, the remote was set to fetch the master branch specifically and only. I fixed it as per below, including the second command to check the results.
$ git config --replace-all remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
$ git config --get remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
The wildcard * of course means everything under that path.

Merge remote repo into forked repo

1. Add the remote, call it “upstream”:

git remote add upstream https://github.com/whoever/whatever.git

2. Fetch all the branches of that remote into remote-tracking branches,
such as upstream/master:

git fetch upstream

3. Make sure that you’re on your master branch:

git checkout master

4. Rewrite your master branch so that any commits of yours that aren’t already in upstream/master are replayed on top of that other branch:

git rebase upstream/master

5. Push to your remote fork

git push -f origin master

Adding an existing or new project to GitHub using the command line

  1. Create a new repository on GitHub. To avoid errors, do not initialize the new repository with README, license, or gitignore files. You can add these files after your project has been pushed to GitHub.
  2. Open Terminal.
  3. Change the current working directory to your local project.
  4. Initialize the local directory as a Git repository.
    git init
    
  5. Add the files in your new local repository. This stages them for the first commit.
    git add .
    # Adds the files in the local repository and stages them for commit. To unstage a file, use 'git reset HEAD YOUR-FILE'.
    
  6. Commit the files that you've staged in your local repository.
    git commit -m "First commit"
    # Commits the tracked changes and prepares them to be pushed to a remote repository. To remove this commit and modify the file, use 'git reset --soft HEAD~1' and commit and add the file again.
    
  7. Copy remote repository URL fieldAt the top of your GitHub repository's Quick Setup page, click  to copy the remote repository URL.
  8. In Terminal, add the URL for the remote repository where your local repository will be pushed.
    git remote add origin remote repository URL
    # Sets the new remote
    git remote -v
    # Verifies the new remote URL
    
  9. Push the changes in your local repository to GitHub.
    git push -u origin master
    # Pushes the changes in your local repository up to the remote repository you specified as the origin

error: src refspec master does not match any + git push origin -u master

Did you try -> git push origin -u master


After git init, your git repository do not have any commits yet. Try adding a file, commit and then push.


Try this:


>> git add <file_name>
>> git commit -m "fitst commit"
>> git push -u origin master

Works.

fatal: Not a git repository (or any of the parent directories): .git

Try -> git init

After cd to the target folder, try git init. It will inititalize the git respository.

Exclude a file from GIT Diff

git diff -- . ':(exclude)com/watchmouse/rest/utils/abc.java'


git diff -- . ':(exclude)db/irrelevant.php'

Git + How to list all the files in a commit?

git show --pretty="" --name-only bd61ad98

git commit get fatal error “fatal: CRLF would be replaced by LF in”

The usual fix is to convert those files yourself, with dos2unix or Swiss File Knife.
I have always preferred to keep core.autocrlf to false, which means:
git config --global core.autocrlf false

Ignore Files or Folders from Git - Git Ignore or Exclude

In your projects .git\info directory there is an exclude file that is effectively the same thing as .gitignore (I think). You can add files and directories to ignore in that.

Suppose, I want exclude target/classes folder from my project abc.

File Structure :

abc
|
|
target     src     resources   .git
|                                          |
|                                          |
classes   repo   lib              info
                                           |
                                           |
                                           exclude (file)

Now, inside abc there is a .git folder and inside of that there is info folder. Inside the info folder, there is exclude file.

Open it and add the following to ignore target/classes folder from above example.

$: vi exclude

/target/classess

Done. Similarly add the folder as per your project structure to exclude or ignore it from git.





GIT + How to change or remove Passpharse for a GIT SSH Key

>> ssh-keygen -f ~/.ssh/id_rsa -p
 
>> Enter old passphrase: 
>> Key has comment '/home/gsekhar/.ssh/id_castlighthealth_rsa'>>   
Enter new passphrase (empty for no passphrase): >>  
Enter same passphrase again: >> Your identification has been saved with the 
new passphrase. 
 

GIT + How to see or view only file names in a git commit using git show and hash

git show --pretty="format:" --name-only bd61ad98

Git + How to apply diff file to current Git branch

>> patch -p1 < a.diff

Git + How to do or get the cherry-pick without commit

>> git cherry-pick -n <commit>

>> git log