Remove a git commit which has not been pushed
2022-09-01
Question I did a git commit but I have not pushed it to the repository yet. So when I do git status, I get '# Your branch is ahead of 'master' by 1 commit. So if I want to roll back my top commit, can I just do: git reset --hard eb27bf26dd18c5a34e0e82b929e0d74cfcaab316 given that when I do git log I get: commit eb27bf26dd18c5a34e0e82b929e0d74cfcaab316 Date: Tue Sep 29 11:21:41 2009 -0700 commit db0c078d5286b837532ff5e276dcf91885df2296 Date: Tue Sep 22 10:31:37 2009 -0700…
How do I select rows from a DataFrame based on column values?
2022-08-31
Question How can I select rows from a DataFrame based on values in some column in Pandas? In SQL, I would use: SELECT * FROM table WHERE column_name = some_value Answer To select rows whose column value equals a scalar, some_value, use ==: df.loc[df['column_name'] == some_value] To select rows whose column value is in an iterable, some_values, use isin: df.loc[df['column_name'].isin(some_values)] Combine multiple conditions with &: df.loc[(df['column_name'] >= A) & (df['column_name'] <= B)] Note the parentheses.…
How can I pivot a dataframe?
2022-08-30
Question What is pivot? How do I pivot? Long format to wide format? I've seen a lot of questions that ask about pivot tables, even if they don't know it. It is virtually impossible to write a canonical question and answer that encompasses all aspects of pivoting... But I'm going to give it a go. The problem with existing questions and answers is that often the question is focused on a nuance that the OP has trouble generalizing in order to use a number of the existing good answers.…
How to output git log with the first line only?
2022-08-30
Question I am trying to customize the format for git log. I want all commits to be shown in one line. Each line should only show the first line of the commit message. I found out that git log --pretty=short should do the trick but on my computer it shows the full log as git log does (besides the time stamp). Further, I tried to use the placeholders as defined in the man page.…
How do I discard unstaged changes in Git?
2022-08-29
Question How do I discard changes in my working copy that are not in the index? Answer For all unstaged files in current working directory use: git restore . For a specific file use: git restore path/to/file/to/revert That together with git switch replaces the overloaded git checkout (see here), and thus removes the argument disambiguation. If a file has both staged and unstaged changes, only the unstaged changes shown in git diff are reverted.…
Git diff says subproject is dirty
2022-08-28
Question I have just run a git diff, and I am getting the following output for all of my approx 10 submodules diff --git a/.vim/bundle/bufexplorer b/.vim/bundle/bufexplorer --- a/.vim/bundle/bufexplorer +++ b/.vim/bundle/bufexplorer @@ -1 +1 @@ -Subproject commit 8c75e65b647238febd0257658b150f717a136359 +Subproject commit 8c75e65b647238febd0257658b150f717a136359-dirty What does this mean? How do I fix it? Answer Update Jan. 2021, ten years later: "git diff"(man) showed a submodule working tree with untracked cruft as Submodule commit <objectname>-dirty, but a natural expectation is that the "…
How to pull remote branch from somebody else's repo
2022-08-28
Question I've got a project hosted on GitHub which somebody has forked. On their fork, they've created a new branch "foo" and made some changes. How do I pull their "foo" into a new branch also named "foo" in my repo? I understand they could submit a pull request to me, but I'd like to initiate this process myself. Assume the following: Because they forked my project, both our repos share the same 'history' Although GitHub shows their project was forked from mine, my local repository doesn't have any references to this person's project.…