How can I get the latest tag name in current branch in Git?
2022-10-17
Question What's the simplest way to get the most recent tag in Git? git tag a HEAD git tag b HEAD^^ git tag c HEAD^ git tag output: a b c Should I write a script to get each tag's datetime and compare them? Answer To get the most recent tag (example output afterwards): git describe --tags --abbrev=0 # 0.1.0-dev To get the most recent tag, with the number of additional commits on top of the tagged object & more:…
How do I clone a subdirectory only of a Git repository?
2022-10-17
Question I have my Git repository which, at the root, has two sub directories: /finisht /static When this was in SVN, /finisht was checked out in one place, while /static was checked out elsewhere, like so: svn co svn+ssh://admin@domain.example/home/admin/repos/finisht/static static Is there a way to do this with Git? Answer What you are trying to do is called a sparse checkout, and that feature was added in Git 1.7.0 (Feb. 2012).…
How to reverse apply a stash?
2022-10-17
Question I have a small patch saved away in my git stash. I've applied it to my working copy using git stash apply. Now, I'd like to back out those changes by reverse applying the patch (kind of like what git revert would do but against the stash). Does anyone know how to do this? Clarification: There are other changes in my working copy. My particular case is hard to describe but you can imagine some debugging or experimental code that's in the stash.…
How to get "their" changes in the middle of conflicting Git rebase?
2022-10-16
Question I have conflicting branches, feature_x branched from main. Let's say when rebasing feature_x on current main, while resolving conflicts, I decide to take some (not all) of "their" (i.e. main) files as-is. How do I do that? I tried: git checkout main:foo/bar.java fatal: reference is not a tree: TS-modules-tmp:foo/bar.java git checkout refs/heads/main:foo/bar.java fatal: reference is not a tree: refs/heads/TS-modules-tmp:foo/bar.java Answer You want to use: git checkout --ours foo/bar.java git add foo/bar.…
How to replace local branch with remote branch entirely in Git?
2022-10-16
Question I have two branches: local branch (the one which I work with) remote branch (public, only well-tested commits go there) Recently I seriously messed up my local branch. How would I replace the local branch entirely with the remote one, so I can continue my work from where the remote branch is now? I have already searched SO and checking out to the remote branch locally does not have any effect.…
How to upgrade Git to latest version on macOS?
2022-10-16
Question I just bought a new Mac with OS X Lion and I checked in the Terminal what version of git is installed by default. I got the answer git --version > git version 1.7.5.4 I would like to upgrade git to the latest version 1.7.8.3, so I downloaded the dmg installer "git-1.7.8.3-intel-universal-snow-leopard.dmg" and I launched it. After the install, the Terminal still says that the version is 1.7.5.4. What am I doing wrong?…
Remove sensitive files and their commits from Git history
2022-10-15
Question I would like to put a Git project on GitHub but it contains certain files with sensitive data (usernames and passwords, like /config/deploy.rb for capistrano). I know I can add these filenames to .gitignore, but this would not remove their history within Git. I also don't want to start over again by deleting the /.git directory. Is there a way to remove all traces of a particular file in your Git history?…