git commands


Bunch of git commands I use frequently.

# add all the modified files to staging
git add `git st | grep modified | cut -c14-120`
# add the removal all the deleted files from the repo to staging
git rm `git st | grep deleted | cut -c14-120`

git reset of a single file


git reset will discard any local changes you have in the current branch you have checked out.

Sometimes, however, you want to reset a single file, not the whole branch. In this case, use checkout, but use the special "option" --, which is unix for "treat every argument after this point as a file name, no matter what it looks like."

# general case
git checkout HEAD -- [file]
# my specific case used ALL THE TIME
git checkout HEAD -- package-lock.json

Apply branch changes without merging with Git


You're working in one branch and you want the changes from another branch (say, you've done a pull request, and want the changes made in that other branch in your current branch), but don't want to merge the changes, you just want them available.

Assuming you want them all in one go, merge without committing, with a squash, then unstage the staged files that will come over with the merge.

If you want specific changes, use git cherrypick -n

git merge --no-commit --squash branch_with_commits
git reset HEAD

View Git Stash Contents Without Applying to Codebase


Use git stash list to view all stashes

Use git stash show with options to view a stash without applying it to the codebase.

# see a "git diff" of what's in the first stash 
git stash show -p stash@{0}

And, a shell script to view these. Save this into a file, chmod +x the file, and run in the terminal.

readonly STASH="$1"
if [[ "xx${STASH}" != "xx" ]]; then
    git stash show -p stash@{${STASH}}
    git stash list

Hide .git repo with Apache


Solution 2.

Don't allow apache to serve anything from the .git repo that might be in the DocumentRoot . This is great because it hides that there even IS a git repo on the webserver.

Add this to the .htaccess in the DocumentRoot after the RewriteEngine On line.

RedirectMatch 404 /\.git

Git remove from repo, keeping on file system


git rm will remove a file from the file system and the repository. If you want to keep the file locally (say, because you're adding it to the .gitignore file), remove it from the repository cache:

# for a file
git rm --cached filename
# for a directory
git rm --cached -r directoryname

Unstage a file in git


Add a file / directory to staging

git add filePath

Now, if you want to remove it from stage, but want to keep the changes you've made locally.

# unstage the file, remove it from the index, don't remove it from the working directory
# a commit will remove it from the repo
$ git rm --cached filePath
# unstage a file, keep the local changes
$ git reset filePath

Remove deleted files from a git repository


When updating a drupal site with drush pm-update, files can be removed. In order to remove them from the git repository, they need to be deleted with git rm. Doing them individually is painful, so do it on the command line with little effort.

Yes, those are backticks.

$ git rm `git st  | grep deleted | cut -f2 -d:`

List git remotes


When talking remote repositories, origin is the most well known. We can have other remote repositories. Adding and listing them below.

$ git remote add remotename https://git.repository/path/to/my-repo.git
$ git remote -v
remote1 (fetch)
remote1 (push)
remotename https://git.repository/path/to/my-repo.git (fetch)
remotename https://git.repository/path/to/my-repo.git (push)

git merge resolve conflicts by always choosing merged branch


There are times when a branch has diverged so far from master or itself that doing a fetch-merge is just impossible without conflicts. Those with changing binary files or gigantic files? The worst.

$ git reset --hard HEAD
HEAD is now at 82a18be this is my last change, no really
$ git pull origin my_branch
 * branch             my_branch -> FETCH_HEAD
Auto-merging some-project/project.xcodeproj/project.pbxproj
CONFLICT (content): Merge conflict in some-project/project.xcodeproj/project.pbxproj
Auto-merging some-project.xcworkspace/contents.xcworkspacedata
Automatic merge failed; fix conflicts and then commit the result.

Solution, just take what the branch has, don't try to merge in changes, in case of conflict.

git merge -s recursive -X theirs branch-name