1659526500
radian is an alternative console for the R program with multiline editing and rich syntax highlight. One would consider radian as a ipython clone for R, though its design is more aligned to julia.
;
to enter and <backspace>
to leave~
to enterjedi
)Requirements:
python
(version 3.6 or above) is also required to install radian. If your system doesn't come with a python distribution, it can be downloaded from https://www.python.org/downloads/ or https://conda.io/en/latest/miniconda.html.pip
is optional but it makes the installation a bit easier.# install released version
pip3 install -U radian
# to run radian
radian
# or the development version
pip3 install -U git+https://github.com/randy3k/radian
Alternatively, if you use conda or miniconda,
conda install -c conda-forge radian
You could alias r
to radian by putting
alias r="radian"
in ~/.bash_profile
such that r
would open radian and R
would still open the traditional R console. (R
is still useful, e.g, running R CMD BUILD
.)
radian can be customized by specifying the below options in various locations
$XDG_CONFIG_HOME/radian/profile
or $HOME/.config/radian/profile
(Unix)%USERPROFILE%/radian/profile
(Windows)$HOME/.radian_profile
(Unix)%USERPROFILE%/.radian_profile
(Windows).radian_profile
in the working directoryThe options could be also specified in the .Rprofile
files, however, it is not recommended because
packrat
or renv
.# Do not copy the whole configuration, just specify what you need!
# see https://help.farbox.com/pygments.html
# for a list of supported color schemes, default scheme is "native"
options(radian.color_scheme = "native")
# either `"emacs"` (default) or `"vi"`.
options(radian.editing_mode = "emacs")
# enable various emacs bindings in vi insert mode
options(radian.emacs_bindings_in_vi_insert_mode = FALSE)
# show vi mode state when radian.editing_mode is `vi`
options(radian.show_vi_mode_prompt = TRUE)
options(radian.vi_mode_prompt = "\033[0;34m[{}]\033[0m ")
# indent continuation lines
# turn this off if you want to copy code without the extra indentation;
# but it leads to less elegent layout
options(radian.indent_lines = TRUE)
# auto match brackets and quotes
options(radian.auto_match = TRUE)
# enable the [prompt_toolkit](https://python-prompt-toolkit.readthedocs.io/en/master/index.html) [`auto_suggest` feature](https://python-prompt-toolkit.readthedocs.io/en/master/pages/asking_for_input.html#auto-suggestion)
# this option is experimental and is known to break python prompt, use it with caution
options(radian.auto_suggest = FALSE)
# highlight matching bracket
options(radian.highlight_matching_bracket = FALSE)
# auto indentation for new line and curly braces
options(radian.auto_indentation = TRUE)
options(radian.tab_size = 4)
# pop up completion while typing
options(radian.complete_while_typing = TRUE)
# the minimum length of prefix to trigger auto completions
options(radian.completion_prefix_length = 2)
# timeout in seconds to cancel completion if it takes too long
# set it to 0 to disable it
options(radian.completion_timeout = 0.05)
# add spaces around equals in function argument completion
options(radian.completion_adding_spaces_around_equals = TRUE)
# automatically adjust R buffer size based on terminal width
options(radian.auto_width = TRUE)
# insert new line between prompts
options(radian.insert_new_line = TRUE)
# max number of history records
options(radian.history_size = 20000)
# where the global history is stored, environmental variables will be expanded
# note that "~" is expanded to %USERPROFILE% or %HOME% in Windows
options(radian.global_history_file = "~/.radian_history")
# the filename that local history is stored, this file would be used instead of
# `radian.global_history_file` if it exists in the current working directory
options(radian.local_history_file = ".radian_history")
# when using history search (ctrl-r/ctrl-s in emacs mode), do not show duplicate results
options(radian.history_search_no_duplicates = FALSE)
# ignore case in history search
options(radian.history_search_ignore_case = FALSE)
# do not save debug browser commands such as `Q` in history
options(radian.history_ignore_browser_commands = TRUE)
# custom prompt for different modes
options(radian.prompt = "\033[0;34mr$>\033[0m ")
options(radian.shell_prompt = "\033[0;31m#!>\033[0m ")
options(radian.browse_prompt = "\033[0;33mBrowse[{}]>\033[0m ")
# stderr color format
options(radian.stderr_format = "\033[0;31m{}\033[0m")
# enable reticulate prompt and trigger `~`
options(radian.enable_reticulate_prompt = TRUE)
# allows user defined shortcuts, these keys should be escaped when send through the terminal.
# In the following example, `esc` + `-` sends `<-` and `esc` + `m` sends `%>%`.
# Note that in some terminals, you could mark `alt` as `escape` so you could use `alt` + `-` instead.
options(radian.escape_key_map = list(
list(key = "-", value = " <- "),
list(key = "m", value = " %>% ")
))
The latest version of R supports Unicode codepage directly. However, radian relies on Python and Python doesn't support Unicode in the way that R supports it. A workaround could be found here: https://github.com/randy3k/radian/issues/269#issuecomment-1169663251
It is expected. radian
runs on python and the python runtime used by radian is forced in reticulate. reticulate::py_config()
gives the note:
NOTE: Python version was forced by the current process
In order to use radian with another python runtime, you will need to install radian
on that python environment.
There are serveral options.
--r-binary
, i.e., radian --r-binary=/path/to/R
PATH
variableR_BINARY
could also be used to specify the path to R.R_HOME
could also be used to specify R home directory. Note that it is should be set as the result of R.home()
, not the directory where R
is located. For example, in Unix$ env R_HOME=/usr/local/lib/R radian
Please also make sure that R was installed with the R shared library libR.so
or libR.dylib
or libR.dll
. On Linux, the configure flag ./configure --enable-R-shlib
may be needed to install R from the source. Do not forget to make clean
to force the recompilation of the files with the correct compiler options.
If you encounter
The package setup script has attempted to modify files on your system that are not within the EasyInstall build area.
Please update your setuptools by
pip install -U setuptools
radian maintains its own history file .radian_history
and doesn't use the .Rhistory
file. A local .radian_history
is used if it is found in the launch directory. Otherwise, the global history file ~/.radian_history
would be used. To override the default behavior, you could launch radian with the options: radian --local-history
, radian --global-history
or radian --no-history
.
radian only provides a frontend to the R program, the actual running eventloop is the same as that of the traditional R console. There is no performance sacrifice (or gain) while using this modern command line interface.
Put
let R_app = "radian"
let R_cmd = "R"
let R_hl_term = 0
let R_args = [] " if you had set any
let R_bracketed_paste = 1
in your vim config.
reticulate
Auto CompletionsTo enable reticulate prompt completions, make sure that jedi
is installed.
pip install jedi
Alternatively, if you use conda,
conda install -c conda-forge jedi
It maybe caused by the invalid terminal size, try running stty size
in your terminal to see if it returns a correct size. You could change the values of it from the environmental variables $COLUMNS
and $LINES
when you log-in the docker container.
docker exec -it <container> bash -c "stty cols $COLUMNS rows $LINES && bash"
radian is powered by (π)thon.
radian wouldn't be possible without the creative work prompt_toolkit by Jonathan Slenders.
Author: Randy3k
Source Code: https://github.com/randy3k/radian
License: MIT license
1649209980
A cross-platform command line REPL for the rapid experimentation and exploration of C#. It supports intellisense, installing NuGet packages, and referencing local .NET projects and assemblies.
(click to view animation)
C# REPL provides the following features:
C# REPL is a .NET 6 global tool, and runs on Windows 10, Mac OS, and Linux. It can be installed via:
dotnet tool install -g csharprepl
If you're running on Mac OS Catalina (10.15) or later, make sure you follow any additional directions printed to the screen. You may need to update your PATH variable in order to use .NET global tools.
After installation is complete, run csharprepl
to begin. C# REPL can be updated via dotnet tool update -g csharprepl
.
Run csharprepl
from the command line to begin an interactive session. The default colorscheme uses the color palette defined by your terminal, but these colors can be changed using a theme.json
file provided as a command line argument.
Type some C# into the prompt and press Enter to run it. The result, if any, will be printed:
> Console.WriteLine("Hello World")
Hello World
> DateTime.Now.AddDays(8)
[6/7/2021 5:13:00 PM]
To evaluate multiple lines of code, use Shift+Enter to insert a newline:
> var x = 5;
var y = 8;
x * y
40
Additionally, if the statement is not a "complete statement" a newline will automatically be inserted when Enter is pressed. For example, in the below code, the first line is not a syntactically complete statement, so when we press enter we'll go down to a new line:
> if (x == 5)
| // caret position, after we press Enter on Line 1
Finally, pressing Ctrl+Enter will show a "detailed view" of the result. For example, for the DateTime.Now
expression below, on the first line we pressed Enter, and on the second line we pressed Ctrl+Enter to view more detailed output:
> DateTime.Now // Pressing Enter shows a reasonable representation
[5/30/2021 5:13:00 PM]
> DateTime.Now // Pressing Ctrl+Enter shows a detailed representation
[5/30/2021 5:13:00 PM] {
Date: [5/30/2021 12:00:00 AM],
Day: 30,
DayOfWeek: Sunday,
DayOfYear: 150,
Hour: 17,
InternalKind: 9223372036854775808,
InternalTicks: 637579915804530992,
Kind: Local,
Millisecond: 453,
Minute: 13,
Month: 5,
Second: 0,
Ticks: 637579915804530992,
TimeOfDay: [17:13:00.4530992],
Year: 2021,
_dateData: 9860951952659306800
}
A note on semicolons: C# expressions do not require semicolons, but statements do. If a statement is missing a required semicolon, a newline will be added instead of trying to run the syntatically incomplete statement; simply type the semicolon to complete the statement.
> var now = DateTime.Now; // assignment statement, semicolon required
> DateTime.Now.AddDays(8) // expression, we don't need a semicolon
[6/7/2021 5:03:05 PM]
Use the #r
command to add assembly or nuget references.
#r "AssemblyName"
or #r "path/to/assembly.dll"
#r "path/to/project.csproj"
. Solution files (.sln) can also be referenced.#r "nuget: PackageName"
to install the latest version of a package, or #r "nuget: PackageName, 13.0.5"
to install a specific version (13.0.5 in this case).To run ASP.NET applications inside the REPL, start the csharprepl
application with the --framework
parameter, specifying the Microsoft.AspNetCore.App
shared framework. Then, use the above #r
command to reference the application DLL. See the Command Line Configuration section below for more details.
csharprepl --framework Microsoft.AspNetCore.App
The C# REPL supports multiple configuration flags to control startup, behavior, and appearance:
csharprepl [OPTIONS] [response-file.rsp] [script-file.csx] [-- <additional-arguments>]
Supported options are:
-r <dll>
or --reference <dll>
: Reference an assembly, project file, or nuget package. Can be specified multiple times. Uses the same syntax as #r
statements inside the REPL. For example, csharprepl -r "nuget:Newtonsoft.Json" "path/to/myproj.csproj"
-u <namespace>
or --using <namespace>
: Add a using statement. Can be specified multiple times.-f <framework>
or --framework <framework>
: Reference a shared framework. The available shared frameworks depends on the local .NET installation, and can be useful when running an ASP.NET application from the REPL. Example frameworks are:-t <theme.json>
or --theme <theme.json>
: Read a theme file for syntax highlighting. This theme file associates C# syntax classifications with colors. The color values can be full RGB, or ANSI color names (defined in your terminal's theme). The NO_COLOR standard is supported.--trace
: Produce a trace file in the current directory that logs CSharpRepl internals. Useful for CSharpRepl bug reports.-v
or --version
: Show version number and exit.-h
or --help
: Show help and exit.response-file.rsp
: A filepath of an .rsp file, containing any of the above command line options.script-file.csx
: A filepath of a .csx file, containing lines of C# to evaluate before starting the REPL. Arguments to this script can be passed as <additional-arguments>
, after a double hyphen (--
), and will be available in a global args
variable.If you have dotnet-suggest
enabled, all options can be tab-completed, including values provided to --framework
and .NET namespaces provided to --using
.
C# REPL is a standalone software application, but it can be useful to integrate it with other developer tools:
To add C# REPL as a menu entry in Windows Terminal, add the following profile to Windows Terminal's settings.json
configuration file (under the JSON property profiles.list
):
{
"name": "C# REPL",
"commandline": "csharprepl"
},
To get the exact colors shown in the screenshots in this README, install the Windows Terminal Dracula theme.
To use the C# REPL with Visual Studio Code, simply run the csharprepl
command in the Visual Studio Code terminal. To send commands to the REPL, use the built-in Terminal: Run Selected Text In Active Terminal
command from the Command Palette (workbench.action.terminal.runSelectedText
).
To add the C# REPL to the Windows Start Menu for quick access, you can run the following PowerShell command, which will start C# REPL in Windows Terminal:
$shell = New-Object -ComObject WScript.Shell
$shortcut = $shell.CreateShortcut("$env:appdata\Microsoft\Windows\Start Menu\Programs\csharprepl.lnk")
$shortcut.TargetPath = "wt.exe"
$shortcut.Arguments = "-w 0 nt csharprepl.exe"
$shortcut.Save()
You may also wish to add a shorter alias for C# REPL, which can be done by creating a .cmd
file somewhere on your path. For example, put the following contents in C:\Users\username\.dotnet\tools\csr.cmd
:
wt -w 0 nt csharprepl
This will allow you to launch C# REPL by running csr
from anywhere that accepts Windows commands, like the Window Run dialog.
This project is far from being the first REPL for C#. Here are some other projects; if this project doesn't suit you, another one might!
Visual Studio's C# Interactive pane is full-featured (it has syntax highlighting and intellisense) and is part of Visual Studio. This deep integration with Visual Studio is both a benefit from a workflow perspective, and a drawback as it's not cross-platform. As far as I know, the C# Interactive pane does not support NuGet packages or navigating to documentation/source code. Subjectively, it does not follow typical command line keybindings, so can feel a bit foreign.
csi.exe ships with C# and is a command line REPL. It's great because it's a cross platform REPL that comes out of the box, but it doesn't support syntax highlighting or autocompletion.
dotnet script allows you to run C# scripts from the command line. It has a REPL built-in, but the predominant focus seems to be as a script runner. It's a great tool, though, and has a strong community following.
dotnet interactive is a tool from Microsoft that creates a Jupyter notebook for C#, runnable through Visual Studio Code. It also provides a general framework useful for running REPLs.
Download Details:
Author: waf
Source Code: https://github.com/waf/CSharpRepl
License: MPL-2.0 License
1647064260
Run C# scripts from the .NET CLI, define NuGet packages inline and edit/debug them in VS Code - all of that with full language services support from OmniSharp.
Name | Version | Framework(s) |
---|---|---|
dotnet-script (global tool) | net6.0 , net5.0 , netcoreapp3.1 | |
Dotnet.Script (CLI as Nuget) | net6.0 , net5.0 , netcoreapp3.1 | |
Dotnet.Script.Core | netcoreapp3.1 , netstandard2.0 | |
Dotnet.Script.DependencyModel | netstandard2.0 | |
Dotnet.Script.DependencyModel.Nuget | netstandard2.0 |
The only thing we need to install is .NET Core 3.1 or .NET 5.0 SDK.
.NET Core 2.1 introduced the concept of global tools meaning that you can install dotnet-script
using nothing but the .NET CLI.
dotnet tool install -g dotnet-script
You can invoke the tool using the following command: dotnet-script
Tool 'dotnet-script' (version '0.22.0') was successfully installed.
The advantage of this approach is that you can use the same command for installation across all platforms. .NET Core SDK also supports viewing a list of installed tools and their uninstallation.
dotnet tool list -g
Package Id Version Commands
---------------------------------------------
dotnet-script 0.22.0 dotnet-script
dotnet tool uninstall dotnet-script -g
Tool 'dotnet-script' (version '0.22.0') was successfully uninstalled.
choco install dotnet.script
We also provide a PowerShell script for installation.
(new-object Net.WebClient).DownloadString("https://raw.githubusercontent.com/filipw/dotnet-script/master/install/install.ps1") | iex
curl -s https://raw.githubusercontent.com/filipw/dotnet-script/master/install/install.sh | bash
If permission is denied we can try with sudo
curl -s https://raw.githubusercontent.com/filipw/dotnet-script/master/install/install.sh | sudo bash
A Dockerfile for running dotnet-script in a Linux container is available. Build:
cd build
docker build -t dotnet-script -f Dockerfile ..
And run:
docker run -it dotnet-script --version
You can manually download all the releases in zip
format from the GitHub releases page.
Our typical helloworld.csx
might look like this:
Console.WriteLine("Hello world!");
That is all it takes and we can execute the script. Args are accessible via the global Args array.
dotnet script helloworld.csx
Simply create a folder somewhere on your system and issue the following command.
dotnet script init
This will create main.csx
along with the launch configuration needed to debug the script in VS Code.
.
├── .vscode
│ └── launch.json
├── main.csx
└── omnisharp.json
We can also initialize a folder using a custom filename.
dotnet script init custom.csx
Instead of main.csx
which is the default, we now have a file named custom.csx
.
.
├── .vscode
│ └── launch.json
├── custom.csx
└── omnisharp.json
Note: Executing
dotnet script init
inside a folder that already contains one or more script files will not create themain.csx
file.
Scripts can be executed directly from the shell as if they were executables.
foo.csx arg1 arg2 arg3
OSX/Linux
Just like all scripts, on OSX/Linux you need to have a
#!
and mark the file as executable via chmod +x foo.csx. If you use dotnet script init to create your csx it will automatically have the#!
directive and be marked as executable.
The OSX/Linux shebang directive should be #!/usr/bin/env dotnet-script
#!/usr/bin/env dotnet-script
Console.WriteLine("Hello world");
You can execute your script using dotnet script or dotnet-script, which allows you to pass arguments to control your script execution more.
foo.csx arg1 arg2 arg3
dotnet script foo.csx -- arg1 arg2 arg3
dotnet-script foo.csx -- arg1 arg2 arg3
All arguments after --
are passed to the script in the following way:
dotnet script foo.csx -- arg1 arg2 arg3
Then you can access the arguments in the script context using the global Args
collection:
foreach (var arg in Args)
{
Console.WriteLine(arg);
}
All arguments before --
are processed by dotnet script
. For example, the following command-line
dotnet script -d foo.csx -- -d
will pass the -d
before --
to dotnet script
and enable the debug mode whereas the -d
after --
is passed to script for its own interpretation of the argument.
dotnet script
has built-in support for referencing NuGet packages directly from within the script.
#r "nuget: AutoMapper, 6.1.0"
Note: Omnisharp needs to be restarted after adding a new package reference
We can define package sources using a NuGet.Config
file in the script root folder. In addition to being used during execution of the script, it will also be used by OmniSharp
that provides language services for packages resolved from these package sources.
As an alternative to maintaining a local NuGet.Config
file we can define these package sources globally either at the user level or at the computer level as described in Configuring NuGet Behaviour
It is also possible to specify packages sources when executing the script.
dotnet script foo.csx -s https://SomePackageSource
Multiple packages sources can be specified like this:
dotnet script foo.csx -s https://SomePackageSource -s https://AnotherPackageSource
Dotnet-Script can create a standalone executable or DLL for your script.
Switch | Long switch | description |
---|---|---|
-o | --output | Directory where the published executable should be placed. Defaults to a 'publish' folder in the current directory. |
-n | --name | The name for the generated DLL (executable not supported at this time). Defaults to the name of the script. |
--dll | Publish to a .dll instead of an executable. | |
-c | --configuration | Configuration to use for publishing the script [Release/Debug]. Default is "Debug" |
-d | --debug | Enables debug output. |
-r | --runtime | The runtime used when publishing the self contained executable. Defaults to your current runtime. |
The executable you can run directly independent of dotnet install, while the DLL can be run using the dotnet CLI like this:
dotnet script exec {path_to_dll} -- arg1 arg2
We provide two types of caching, the dependency cache
and the execution cache
which is explained in detail below. In order for any of these caches to be enabled, it is required that all NuGet package references are specified using an exact version number. The reason for this constraint is that we need to make sure that we don't execute a script with a stale dependency graph.
In order to resolve the dependencies for a script, a dotnet restore
is executed under the hood to produce a project.assets.json
file from which we can figure out all the dependencies we need to add to the compilation. This is an out-of-process operation and represents a significant overhead to the script execution. So this cache works by looking at all the dependencies specified in the script(s) either in the form of NuGet package references or assembly file references. If these dependencies matches the dependencies from the last script execution, we skip the restore and read the dependencies from the already generated project.assets.json
file. If any of the dependencies has changed, we must restore again to obtain the new dependency graph.
In order to execute a script it needs to be compiled first and since that is a CPU and time consuming operation, we make sure that we only compile when the source code has changed. This works by creating a SHA256 hash from all the script files involved in the execution. This hash is written to a temporary location along with the DLL that represents the result of the script compilation. When a script is executed the hash is computed and compared with the hash from the previous compilation. If they match there is no need to recompile and we run from the already compiled DLL. If the hashes don't match, the cache is invalidated and we recompile.
You can override this automatic caching by passing --no-cache flag, which will bypass both caches and cause dependency resolution and script compilation to happen every time we execute the script.
The temporary location used for caches is a sub-directory named dotnet-script
under (in order of priority):
DOTNET_SCRIPT_CACHE_LOCATION
, if defined and value is not empty.$XDG_CACHE_HOME
if defined otherwise $HOME/.cache
~/Library/Caches
Path.GetTempPath
for the platform.The days of debugging scripts using Console.WriteLine
are over. One major feature of dotnet script
is the ability to debug scripts directly in VS Code. Just set a breakpoint anywhere in your script file(s) and hit F5(start debugging)
Script packages are a way of organizing reusable scripts into NuGet packages that can be consumed by other scripts. This means that we now can leverage scripting infrastructure without the need for any kind of bootstrapping.
A script package is just a regular NuGet package that contains script files inside the content
or contentFiles
folder.
The following example shows how the scripts are laid out inside the NuGet package according to the standard convention .
└── contentFiles
└── csx
└── netstandard2.0
└── main.csx
This example contains just the main.csx
file in the root folder, but packages may have multiple script files either in the root folder or in subfolders below the root folder.
When loading a script package we will look for an entry point script to be loaded. This entry point script is identified by one of the following.
main.csx
in the root folderIf the entry point script cannot be determined, we will simply load all the scripts files in the package.
The advantage with using an entry point script is that we can control loading other scripts from the package.
To consume a script package all we need to do specify the NuGet package in the #load
directive.
The following example loads the simple-targets package that contains script files to be included in our script.
#load "nuget:simple-targets-csx, 6.0.0"
using static SimpleTargets;
var targets = new TargetDictionary();
targets.Add("default", () => Console.WriteLine("Hello, world!"));
Run(Args, targets);
Note: Debugging also works for script packages so that we can easily step into the scripts that are brought in using the
#load
directive.
Scripts don't actually have to exist locally on the machine. We can also execute scripts that are made available on an http(s)
endpoint.
This means that we can create a Gist on Github and execute it just by providing the URL to the Gist.
This Gist contains a script that prints out "Hello World"
We can execute the script like this
dotnet script https://gist.githubusercontent.com/seesharper/5d6859509ea8364a1fdf66bbf5b7923d/raw/0a32bac2c3ea807f9379a38e251d93e39c8131cb/HelloWorld.csx
That is a pretty long URL, so why don't make it a TinyURL like this:
dotnet script https://tinyurl.com/y8cda9zt
A pretty common scenario is that we have logic that is relative to the script path. We don't want to require the user to be in a certain directory for these paths to resolve correctly so here is how to provide the script path and the script folder regardless of the current working directory.
public static string GetScriptPath([CallerFilePath] string path = null) => path;
public static string GetScriptFolder([CallerFilePath] string path = null) => Path.GetDirectoryName(path);
Tip: Put these methods as top level methods in a separate script file and
#load
that file wherever access to the script path and/or folder is needed.
This release contains a C# REPL (Read-Evaluate-Print-Loop). The REPL mode ("interactive mode") is started by executing dotnet-script
without any arguments.
The interactive mode allows you to supply individual C# code blocks and have them executed as soon as you press Enter. The REPL is configured with the same default set of assembly references and using statements as regular CSX script execution.
Once dotnet-script
starts you will see a prompt for input. You can start typing C# code there.
~$ dotnet script
> var x = 1;
> x+x
2
If you submit an unterminated expression into the REPL (no ;
at the end), it will be evaluated and the result will be serialized using a formatter and printed in the output. This is a bit more interesting than just calling ToString()
on the object, because it attempts to capture the actual structure of the object. For example:
~$ dotnet script
> var x = new List<string>();
> x.Add("foo");
> x
List<string>(1) { "foo" }
> x.Add("bar");
> x
List<string>(2) { "foo", "bar" }
>
REPL also supports inline Nuget packages - meaning the Nuget packages can be installed into the REPL from within the REPL. This is done via our #r
and #load
from Nuget support and uses identical syntax.
~$ dotnet script
> #r "nuget: Automapper, 6.1.1"
> using AutoMapper;
> typeof(MapperConfiguration)
[AutoMapper.MapperConfiguration]
> #load "nuget: simple-targets-csx, 6.0.0";
> using static SimpleTargets;
> typeof(TargetDictionary)
[Submission#0+SimpleTargets+TargetDictionary]
Using Roslyn syntax parsing, we also support multiline REPL mode. This means that if you have an uncompleted code block and press Enter, we will automatically enter the multiline mode. The mode is indicated by the *
character. This is particularly useful for declaring classes and other more complex constructs.
~$ dotnet script
> class Foo {
* public string Bar {get; set;}
* }
> var foo = new Foo();
Aside from the regular C# script code, you can invoke the following commands (directives) from within the REPL:
Command | Description |
---|---|
#load | Load a script into the REPL (same as #load usage in CSX) |
#r | Load an assembly into the REPL (same as #r usage in CSX) |
#reset | Reset the REPL back to initial state (without restarting it) |
#cls | Clear the console screen without resetting the REPL state |
#exit | Exits the REPL |
You can execute a CSX script and, at the end of it, drop yourself into the context of the REPL. This way, the REPL becomes "seeded" with your code - all the classes, methods or variables are available in the REPL context. This is achieved by running a script with an -i
flag.
For example, given the following CSX script:
var msg = "Hello World";
Console.WriteLine(msg);
When you run this with the -i
flag, Hello World
is printed, REPL starts and msg
variable is available in the REPL context.
~$ dotnet script foo.csx -i
Hello World
>
You can also seed the REPL from inside the REPL - at any point - by invoking a #load
directive pointed at a specific file. For example:
~$ dotnet script
> #load "foo.csx"
Hello World
>
The following example shows how we can pipe data in and out of a script.
The UpperCase.csx
script simply converts the standard input to upper case and writes it back out to standard output.
using (var streamReader = new StreamReader(Console.OpenStandardInput()))
{
Write(streamReader.ReadToEnd().ToUpper());
}
We can now simply pipe the output from one command into our script like this.
echo "This is some text" | dotnet script UpperCase.csx
THIS IS SOME TEXT
The first thing we need to do add the following to the launch.config
file that allows VS Code to debug a running process.
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
To debug this script we need a way to attach the debugger in VS Code and the simplest thing we can do here is to wait for the debugger to attach by adding this method somewhere.
public static void WaitForDebugger()
{
Console.WriteLine("Attach Debugger (VS Code)");
while(!Debugger.IsAttached)
{
}
}
To debug the script when executing it from the command line we can do something like
WaitForDebugger();
using (var streamReader = new StreamReader(Console.OpenStandardInput()))
{
Write(streamReader.ReadToEnd().ToUpper()); // <- SET BREAKPOINT HERE
}
Now when we run the script from the command line we will get
$ echo "This is some text" | dotnet script UpperCase.csx
Attach Debugger (VS Code)
This now gives us a chance to attach the debugger before stepping into the script and from VS Code, select the .NET Core Attach
debugger and pick the process that represents the executing script.
Once that is done we should see our breakpoint being hit.
By default, scripts will be compiled using the debug
configuration. This is to ensure that we can debug a script in VS Code as well as attaching a debugger for long running scripts.
There are however situations where we might need to execute a script that is compiled with the release
configuration. For instance, running benchmarks using BenchmarkDotNet is not possible unless the script is compiled with the release
configuration.
We can specify this when executing the script.
dotnet script foo.csx -c release
Starting from version 0.50.0, dotnet-script
supports .Net Core 3.0 and all the C# 8 features. The way we deal with nullable references types in dotnet-script
is that we turn every warning related to nullable reference types into compiler errors. This means every warning between CS8600
and CS8655
are treated as an error when compiling the script.
Nullable references types are turned off by default and the way we enable it is using the #nullable enable
compiler directive. This means that existing scripts will continue to work, but we can now opt-in on this new feature.
#!/usr/bin/env dotnet-script
#nullable enable
string name = null;
Trying to execute the script will result in the following error
main.csx(5,15): error CS8625: Cannot convert null literal to non-nullable reference type.
We will also see this when working with scripts in VS Code under the problems panel.
Download Details:
Author: filipw
Source Code: https://github.com/filipw/dotnet-script
License: MIT License
1659526500
radian is an alternative console for the R program with multiline editing and rich syntax highlight. One would consider radian as a ipython clone for R, though its design is more aligned to julia.
;
to enter and <backspace>
to leave~
to enterjedi
)Requirements:
python
(version 3.6 or above) is also required to install radian. If your system doesn't come with a python distribution, it can be downloaded from https://www.python.org/downloads/ or https://conda.io/en/latest/miniconda.html.pip
is optional but it makes the installation a bit easier.# install released version
pip3 install -U radian
# to run radian
radian
# or the development version
pip3 install -U git+https://github.com/randy3k/radian
Alternatively, if you use conda or miniconda,
conda install -c conda-forge radian
You could alias r
to radian by putting
alias r="radian"
in ~/.bash_profile
such that r
would open radian and R
would still open the traditional R console. (R
is still useful, e.g, running R CMD BUILD
.)
radian can be customized by specifying the below options in various locations
$XDG_CONFIG_HOME/radian/profile
or $HOME/.config/radian/profile
(Unix)%USERPROFILE%/radian/profile
(Windows)$HOME/.radian_profile
(Unix)%USERPROFILE%/.radian_profile
(Windows).radian_profile
in the working directoryThe options could be also specified in the .Rprofile
files, however, it is not recommended because
packrat
or renv
.# Do not copy the whole configuration, just specify what you need!
# see https://help.farbox.com/pygments.html
# for a list of supported color schemes, default scheme is "native"
options(radian.color_scheme = "native")
# either `"emacs"` (default) or `"vi"`.
options(radian.editing_mode = "emacs")
# enable various emacs bindings in vi insert mode
options(radian.emacs_bindings_in_vi_insert_mode = FALSE)
# show vi mode state when radian.editing_mode is `vi`
options(radian.show_vi_mode_prompt = TRUE)
options(radian.vi_mode_prompt = "\033[0;34m[{}]\033[0m ")
# indent continuation lines
# turn this off if you want to copy code without the extra indentation;
# but it leads to less elegent layout
options(radian.indent_lines = TRUE)
# auto match brackets and quotes
options(radian.auto_match = TRUE)
# enable the [prompt_toolkit](https://python-prompt-toolkit.readthedocs.io/en/master/index.html) [`auto_suggest` feature](https://python-prompt-toolkit.readthedocs.io/en/master/pages/asking_for_input.html#auto-suggestion)
# this option is experimental and is known to break python prompt, use it with caution
options(radian.auto_suggest = FALSE)
# highlight matching bracket
options(radian.highlight_matching_bracket = FALSE)
# auto indentation for new line and curly braces
options(radian.auto_indentation = TRUE)
options(radian.tab_size = 4)
# pop up completion while typing
options(radian.complete_while_typing = TRUE)
# the minimum length of prefix to trigger auto completions
options(radian.completion_prefix_length = 2)
# timeout in seconds to cancel completion if it takes too long
# set it to 0 to disable it
options(radian.completion_timeout = 0.05)
# add spaces around equals in function argument completion
options(radian.completion_adding_spaces_around_equals = TRUE)
# automatically adjust R buffer size based on terminal width
options(radian.auto_width = TRUE)
# insert new line between prompts
options(radian.insert_new_line = TRUE)
# max number of history records
options(radian.history_size = 20000)
# where the global history is stored, environmental variables will be expanded
# note that "~" is expanded to %USERPROFILE% or %HOME% in Windows
options(radian.global_history_file = "~/.radian_history")
# the filename that local history is stored, this file would be used instead of
# `radian.global_history_file` if it exists in the current working directory
options(radian.local_history_file = ".radian_history")
# when using history search (ctrl-r/ctrl-s in emacs mode), do not show duplicate results
options(radian.history_search_no_duplicates = FALSE)
# ignore case in history search
options(radian.history_search_ignore_case = FALSE)
# do not save debug browser commands such as `Q` in history
options(radian.history_ignore_browser_commands = TRUE)
# custom prompt for different modes
options(radian.prompt = "\033[0;34mr$>\033[0m ")
options(radian.shell_prompt = "\033[0;31m#!>\033[0m ")
options(radian.browse_prompt = "\033[0;33mBrowse[{}]>\033[0m ")
# stderr color format
options(radian.stderr_format = "\033[0;31m{}\033[0m")
# enable reticulate prompt and trigger `~`
options(radian.enable_reticulate_prompt = TRUE)
# allows user defined shortcuts, these keys should be escaped when send through the terminal.
# In the following example, `esc` + `-` sends `<-` and `esc` + `m` sends `%>%`.
# Note that in some terminals, you could mark `alt` as `escape` so you could use `alt` + `-` instead.
options(radian.escape_key_map = list(
list(key = "-", value = " <- "),
list(key = "m", value = " %>% ")
))
The latest version of R supports Unicode codepage directly. However, radian relies on Python and Python doesn't support Unicode in the way that R supports it. A workaround could be found here: https://github.com/randy3k/radian/issues/269#issuecomment-1169663251
It is expected. radian
runs on python and the python runtime used by radian is forced in reticulate. reticulate::py_config()
gives the note:
NOTE: Python version was forced by the current process
In order to use radian with another python runtime, you will need to install radian
on that python environment.
There are serveral options.
--r-binary
, i.e., radian --r-binary=/path/to/R
PATH
variableR_BINARY
could also be used to specify the path to R.R_HOME
could also be used to specify R home directory. Note that it is should be set as the result of R.home()
, not the directory where R
is located. For example, in Unix$ env R_HOME=/usr/local/lib/R radian
Please also make sure that R was installed with the R shared library libR.so
or libR.dylib
or libR.dll
. On Linux, the configure flag ./configure --enable-R-shlib
may be needed to install R from the source. Do not forget to make clean
to force the recompilation of the files with the correct compiler options.
If you encounter
The package setup script has attempted to modify files on your system that are not within the EasyInstall build area.
Please update your setuptools by
pip install -U setuptools
radian maintains its own history file .radian_history
and doesn't use the .Rhistory
file. A local .radian_history
is used if it is found in the launch directory. Otherwise, the global history file ~/.radian_history
would be used. To override the default behavior, you could launch radian with the options: radian --local-history
, radian --global-history
or radian --no-history
.
radian only provides a frontend to the R program, the actual running eventloop is the same as that of the traditional R console. There is no performance sacrifice (or gain) while using this modern command line interface.
Put
let R_app = "radian"
let R_cmd = "R"
let R_hl_term = 0
let R_args = [] " if you had set any
let R_bracketed_paste = 1
in your vim config.
reticulate
Auto CompletionsTo enable reticulate prompt completions, make sure that jedi
is installed.
pip install jedi
Alternatively, if you use conda,
conda install -c conda-forge jedi
It maybe caused by the invalid terminal size, try running stty size
in your terminal to see if it returns a correct size. You could change the values of it from the environmental variables $COLUMNS
and $LINES
when you log-in the docker container.
docker exec -it <container> bash -c "stty cols $COLUMNS rows $LINES && bash"
radian is powered by (π)thon.
radian wouldn't be possible without the creative work prompt_toolkit by Jonathan Slenders.
Author: Randy3k
Source Code: https://github.com/randy3k/radian
License: MIT license
1649392464
Flight rules for Git
A guide for astronauts (now, programmers using Git) about what to do when things go wrong.
Flight Rules are the hard-earned body of knowledge recorded in manuals that list, step-by-step, what to do if X occurs, and why. Essentially, they are extremely detailed, scenario-specific standard operating procedures. [...]
NASA has been capturing our missteps, disasters and solutions since the early 1960s, when Mercury-era ground teams first started gathering "lessons learned" into a compendium that now lists thousands of problematic situations, from engine failure to busted hatch handles to computer glitches, and their solutions.
— Chris Hadfield, An Astronaut's Guide to Life on Earth.
For clarity's sake all examples in this document use a customized bash prompt in order to indicate the current branch and whether or not there are staged changes. The branch is enclosed in parentheses, and a *
next to the branch name indicates staged changes.
All commands should work for at least git version 2.13.0. See the git website to update your local git version.
Table of Contents generated with DocToc
To initialize an existing directory as a Git repository:
(my-folder) $ git init
To clone (copy) a remote repository, copy the URL for the repository, and run:
$ git clone [url]
This will save it to a folder named the same as the remote repository's. Make sure you have a connection to the remote server you are cloning from (for most purposes this means making sure you are connected to the internet).
To clone it into a folder with a different name than the default repository name:
$ git clone [url] name-of-new-folder
There are a few possible problems here:
If you cloned the wrong repository, simply delete the directory created after running git clone
and clone the correct repository.
If you set the wrong repository as the origin of an existing local repository, change the URL of your origin by running:
$ git remote set-url origin [url of the actual repo]
For more, see this StackOverflow topic.
Git doesn't allow you to add code to someone else's repository without access rights. Neither does GitHub, which is not the same as Git, but rather a hosted service for Git repositories. However, you can suggest code using patches, or, on GitHub, forks and pull requests.
First, a bit about forking. A fork is a copy of a repository. It is not a git operation, but is a common action on GitHub, Bitbucket, GitLab — or anywhere people host Git repositories. You can fork a repository through the hosted UI.
After you've forked a repository, you normally need to clone the repository to your machine. You can do some small edits on GitHub, for instance, without cloning, but this isn't a github-flight-rules list, so let's go with how to do this locally.
# if you are using ssh
$ git clone git@github.com:k88hudson/git-flight-rules.git
# if you are using https
$ git clone https://github.com/k88hudson/git-flight-rules.git
If you cd
into the resulting directory, and type git remote
, you'll see a list of the remotes. Normally there will be one remote - origin
- which will point to k88hudson/git-flight-rules
. In this case, we also want a remote that will point to your fork.
First, to follow a Git convention, we normally use the remote name origin
for your own repository and upstream
for whatever you've forked. So, rename the origin
remote to upstream
$ git remote rename origin upstream
You can also do this using git remote set-url
, but it takes longer and is more steps.
Then, set up a new remote that points to your project.
$ git remote add origin git@github.com:YourName/git-flight-rules.git
Note that now you have two remotes.
origin
references your own repository.upstream
references the original one.From origin, you can read and write. From upstream, you can only read.
When you've finished making whatever changes you like, push your changes (normally in a branch) to the remote named origin
. If you're on a branch, you could use --set-upstream
to avoid specifying the remote tracking branch on every future push using this branch. For instance:
$ (feature/my-feature) git push --set-upstream origin feature/my-feature
There is no way to suggest a pull request using the CLI using Git (although there are tools, like hub, which will do this for you). So, if you're ready to make a pull request, go to your GitHub (or another Git host) and create a new pull request. Note that your host automatically links the original and forked repositories.
After all of this, do not forget to respond to any code review feedback.
Another approach to suggesting code changes that doesn't rely on third party sites such as Github is to use git format-patch
.
format-patch
creates a .patch file for one or more commits. This file is essentially a list of changes that looks similar to the commit diffs you can view on Github.
A patch can be viewed and even edited by the recipient and applied using git am
.
For example, to create a patch based on the previous commit you would run git format-patch HEAD^
which would create a .patch file called something like 0001-My-Commit-Message.patch.
To apply this patch file to your repository you would run git am ./0001-My-Commit-Message.patch
.
Patches can also be sent via email using the git send-email
command. For information on usage and configuration see: https://git-send-email.io
After a while, the upstream
repository may have been updated, and these updates need to be pulled into your origin
repo. Remember that like you, other people are contributing too. Suppose that you are in your own feature branch and you need to update it with the original repository updates.
You probably have set up a remote that points to the original project. If not, do this now. Generally we use upstream
as a remote name:
$ (main) git remote add upstream <link-to-original-repository>
# $ (main) git remote add upstream git@github.com:k88hudson/git-flight-rules.git
Now you can fetch from upstream and get the latest updates.
$ (main) git fetch upstream
$ (main) git merge upstream/main
# or using a single command
$ (main) git pull upstream main
Let's say that you just blindly committed changes with git commit -a
and you're not sure what the actual content of the commit you just made was. You can show the latest commit on your current HEAD with:
(main)$ git show
Or
$ git log -n1 -p
If you want to see a file at a specific commit, you can also do this (where <commitid>
is the commit you're interested in):
$ git show <commitid>:filename
If you wrote the wrong thing and the commit has not yet been pushed, you can do the following to change the commit message without changing the changes in the commit:
$ git commit --amend --only
This will open your default text editor, where you can edit the message. On the other hand, you can do this all in one command:
$ git commit --amend --only -m 'xxxxxxx'
If you have already pushed the message, you can amend the commit and force push, but this is not recommended.
If it's a single commit, amend it
$ git commit --amend --no-edit --author "New Authorname <authoremail@mydomain.com>"
An alternative is to correctly configure your author settings in git config --global author.(name|email)
and then use
$ git commit --amend --reset-author --no-edit
If you need to change all of history, see the man page for git filter-branch
.
In order to remove changes for a file from the previous commit, do the following:
$ git checkout HEAD^ myfile
$ git add myfile
$ git commit --amend --no-edit
In case the file was newly added to the commit and you want to remove it (from Git alone), do:
$ git rm --cached myfile
$ git commit --amend --no-edit
This is particularly useful when you have an open patch and you have committed an unnecessary file, and need to force push to update the patch on a remote. The --no-edit
option is used to keep the existing commit message.
If you need to delete pushed commits, you can use the following. However, it will irreversibly change your history, and mess up the history of anyone else who had already pulled from the repository. In short, if you're not sure, you should never do this, ever.
$ git reset HEAD^ --hard
$ git push --force-with-lease [remote] [branch]
If you haven't pushed, to reset Git to the state it was in before you made your last commit (while keeping your staged changes):
(my-branch*)$ git reset --soft HEAD@{1}
This only works if you haven't pushed. If you have pushed, the only truly safe thing to do is git revert SHAofBadCommit
. That will create a new commit that undoes all the previous commit's changes. Or, if the branch you pushed to is rebase-safe (ie. other devs aren't expected to pull from it), you can just use git push --force-with-lease
. For more, see the above section.
The same warning applies as above. Never do this if possible.
$ git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
$ git push --force-with-lease [remote] [branch]
Or do an interactive rebase and remove the line(s) corresponding to commit(s) you want to see removed.
To https://github.com/yourusername/repo.git
! [rejected] mybranch -> mybranch (non-fast-forward)
error: failed to push some refs to 'https://github.com/tanay1337/webmaker.org.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Note that, as with rebasing (see below), amending replaces the old commit with a new one, so you must force push (--force-with-lease
) your changes if you have already pushed the pre-amended commit to your remote. Be careful when you do this – always make sure you specify a branch!
(my-branch)$ git push origin mybranch --force-with-lease
In general, avoid force pushing. It is best to create and push a new commit rather than force-pushing the amended commit as it will cause conflicts in the source history for any other developer who has interacted with the branch in question or any child branches. --force-with-lease
will still fail, if someone else was also working on the same branch as you, and your push would overwrite those changes.
If you are absolutely sure that nobody is working on the same branch or you want to update the tip of the branch unconditionally, you can use --force
(-f
), but this should be avoided in general.
If you accidentally do git reset --hard
, you can normally still get your commit back, as git keeps a log of everything for a few days.
Note: This is only valid if your work is backed up, i.e., either committed or stashed. git reset --hard
will remove uncommitted modifications, so use it with caution. (A safer option is git reset --keep
.)
(main)$ git reflog
You'll see a list of your past commits, and a commit for the reset. Choose the SHA of the commit you want to return to, and reset again:
(main)$ git reset --hard SHA1234
And you should be good to go.
If you accidentally merged a feature branch to the main development branch before it was ready to be merged, you can still undo the merge. But there's a catch: A merge commit has more than one parent (usually two).
The command to use
(feature-branch)$ git revert -m 1 <commit>
where the -m 1 option says to select parent number 1 (the branch into which the merge was made) as the parent to revert to.
Note: the parent number is not a commit identifier. Rather, a merge commit has a line Merge: 8e2ce2d 86ac2e7
. The parent number is the 1-based index of the desired parent on this line, the first identifier is number 1, the second is number 2, and so on.
If you accidentally pushed files containing sensitive, or private data (passwords, keys, etc.), you can amend the previous commit. Keep in mind that once you have pushed a commit, you should consider any data it contains to be compromised. These steps can remove the sensitive data from your public repo or your local copy, but you cannot remove the sensitive data from other people's pulled copies. If you committed a password, change it immediately. If you committed a key, re-generate it immediately. Amending the pushed commit is not enough, since anyone could have pulled the original commit containing your sensitive data in the meantime.
If you edit the file and remove the sensitive data, then run
(feature-branch)$ git add edited_file
(feature-branch)$ git commit --amend --no-edit
(feature-branch)$ git push --force-with-lease origin [branch]
If you want to remove an entire file (but keep it locally), then run
(feature-branch)$ git rm --cached sensitive_file
echo sensitive_file >> .gitignore
(feature-branch)$ git add .gitignore
(feature-branch)$ git commit --amend --no-edit
(feature-branch)$ git push --force-with-lease origin [branch]
Alternatively store your sensitive data in local environment variables.
If you want to completely remove an entire file (and not keep it locally), then run
(feature-branch)$ git rm sensitive_file
(feature-branch)$ git commit --amend --no-edit
(feature-branch)$ git push --force-with-lease origin [branch]
If you have made other commits in the meantime (i.e. the sensitive data is in a commit before the previous commit), you will have to rebase.
If the file you want to delete is secret or sensitive, instead see how to remove sensitive files.
Even if you delete a large or unwanted file in a recent commit, it still exists in git history, in your repo's .git
folder, and will make git clone
download unneeded files.
The actions in this part of the guide will require a force push, and rewrite large sections of repo history, so if you are working with remote collaborators, check first that any local work of theirs is pushed.
There are two options for rewriting history, the built-in git-filter-branch
or bfg-repo-cleaner
. bfg
is significantly cleaner and more performant, but it is a third-party download and requires java. We will describe both alternatives. The final step is to force push your changes, which requires special consideration on top of a regular force push, given that a great deal of repo history will have been permanently changed.
Using bfg-repo-cleaner requires java. Download the bfg jar from the link here. Our examples will use bfg.jar
, but your download may have a version number, e.g. bfg-1.13.0.jar
.
To delete a specific file.
(main)$ git rm path/to/filetoremove
(main)$ git commit -m "Commit removing filetoremove"
(main)$ java -jar ~/Downloads/bfg.jar --delete-files filetoremove
Note that in bfg you must use the plain file name even if it is in a subdirectory.
You can also delete a file by pattern, e.g.:
(main)$ git rm *.jpg
(main)$ git commit -m "Commit removing *.jpg"
(main)$ java -jar ~/Downloads/bfg.jar --delete-files *.jpg
With bfg, the files that exist on your latest commit will not be affected. For example, if you had several large .tga files in your repo, and then in an earlier commit, you deleted a subset of them, this call does not touch files present in the latest commit
Note, if you renamed a file as part of a commit, e.g. if it started as LargeFileFirstName.mp4
and a commit changed it to LargeFileSecondName.mp4
, running java -jar ~/Downloads/bfg.jar --delete-files LargeFileSecondName.mp4
will not remove it from git history. Either run the --delete-files
command with both filenames, or with a matching pattern.
git-filter-branch
is more cumbersome and has less features, but you may use it if you cannot install or run bfg
.
In the below, replace filepattern
may be a specific name or pattern, e.g. *.jpg
. This will remove files matching the pattern from all history and branches.
(main)$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch filepattern' --prune-empty --tag-name-filter cat -- --all
Behind-the-scenes explanation:
--tag-name-filter cat
is a cumbersome, but simplest, way to apply the original tags to the new commits, using the command cat.
--prune-empty
removes any now-empty commits.
Once you have removed your desired files, test carefully that you haven't broken anything in your repo - if you have, it is easiest to re-clone your repo to start over. To finish, optionally use git garbage collection to minimize your local .git folder size, and then force push.
(main)$ git reflog expire --expire=now --all && git gc --prune=now --aggressive
(main)$ git push origin --force --tags
Since you just rewrote the entire git repo history, the git push
operation may be too large, and return the error “The remote end hung up unexpectedly”
. If this happens, you can try increasing the git post buffer:
(main)$ git config http.postBuffer 524288000
(main)$ git push --force
If this does not work, you will need to manually push the repo history in chunks of commits. In the command below, try increasing <number>
until the push operation succeeds.
(main)$ git push -u origin HEAD~<number>:refs/head/main --force
Once the push operation succeeds the first time, decrease <number>
gradually until a conventional git push
succeeds.
Consider you created some (e.g. three) commits and later realize you missed doing something that belongs contextually into the first of those commits. This bothers you, because if you'd create a new commit containing those changes, you'd have a clean code base, but your commits weren't atomic (i.e. changes that belonged to each other weren't in the same commit). In such a situation you may want to change the commit where these changes belong to, include them and have the following commits unaltered. In such a case, git rebase
might save you.
Consider a situation where you want to change the third last commit you made.
(your-branch)$ git rebase -i HEAD~4
gets you into interactive rebase mode, which allows you to edit any of your last three commits. A text editor pops up, showing you something like
pick 9e1d264 The third last commit
pick 4b6e19a The second to last commit
pick f4037ec The last commit
which you change into
edit 9e1d264 The third last commit
pick 4b6e19a The second to last commit
pick f4037ec The last commit
This tells rebase that you want to edit your third last commit and keep the other two unaltered. Then you'll save (and close) the editor. Git will then start to rebase. It stops on the commit you want to alter, giving you the chance to edit that commit. Now you can apply the changes which you missed applying when you initially committed that commit. You do so by editing and staging them. Afterwards you'll run
(your-branch)$ git commit --amend
which tells Git to recreate the commit, but to leave the commit message unedited. Having done that, the hard part is solved.
(your-branch)$ git rebase --continue
will do the rest of the work for you.
$ git add -u
# to stage files with ext .txt
$ git add -u *.txt
# to stage all files inside directory src
$ git add -u src/
(my-branch*)$ git commit --amend
If you already know you don't want to change the commit message, you can tell git to reuse the commit message:
(my-branch*)$ git commit --amend -C HEAD
Normally, if you want to stage part of a file, you run this:
$ git add --patch filename.x
-p
will work for short. This will open interactive mode. You would be able to use the s
option to split the commit - however, if the file is new, you will not have this option. To add a new file, do this:
$ git add -N filename.x
Then, you will need to use the e
option to manually choose which lines to add. Running git diff --cached
or git diff --staged
will show you which lines you have staged compared to which are still saved locally.
git add
will add the entire file to a commit. git add -p
will allow to interactively select which changes you want to add.
git reset -p
will open a patch mode reset dialog. This is similar to git add -p
, except that selecting "yes" will unstage the change, removing it from the upcoming commit.
In many cases, you should unstage all of your staged files and then pick the file you want and commit it. However, if you want to switch the staged and unstaged edits, you can create a temporary commit to store your staged files, stage your unstaged files and then stash them. Then, reset the temporary commit and pop your stash.
$ git commit -m "WIP"
$ git add . # This will also add untracked files.
$ git stash
$ git reset HEAD^
$ git stash pop --index 0
NOTE 1: The reason to use pop
here is want to keep idempotent as much as possible. NOTE 2: Your staged files will be marked as unstaged if you don't use the --index
flag. (This link explains why.)
$ git checkout -b my-branch
$ git stash
$ git checkout my-branch
$ git stash pop
If you want to discard all your local staged and unstaged changes, you can do this:
(my-branch)$ git reset --hard
# or
(main)$ git checkout -f
This will unstage all files you might have staged with git add
:
$ git reset
This will revert all local uncommitted changes (should be executed in repo root):
$ git checkout .
You can also revert uncommitted changes to a particular file or directory:
$ git checkout [some_dir|file.txt]
Yet another way to revert all uncommitted changes (longer to type, but works from any subdirectory):
$ git reset --hard HEAD
This will remove all local untracked files, so only files tracked by Git remain:
$ git clean -fd
-x
will also remove all ignored files.
When you want to get rid of some, but not all changes in your working copy.
Checkout undesired changes, keep good changes.
$ git checkout -p
# Answer y to all of the snippets you want to drop
Another strategy involves using stash
. Stash all the good changes, reset working copy, and reapply good changes.
$ git stash -p
# Select all of the snippets you want to save
$ git reset --hard
$ git stash pop
Alternatively, stash your undesired changes, and then drop stash.
$ git stash -p
# Select all of the snippets you don't want to save
$ git stash drop
When you want to get rid of one specific file in your working copy.
$ git checkout myFile
Alternatively, to discard multiple files in your working copy, list them all.
$ git checkout myFirstFile mySecondFile
When you want to get rid of all of your unstaged local uncommitted changes
$ git checkout .
When you want to get rid of all of your untracked files
$ git clean -f
Sometimes we have one or more files that accidentally ended up being staged, and these files have not been committed before. To unstage them:
$ git reset -- <filename>
This results in unstaging the file and make it look like it's untracked.
List local branches
$ git branch
List remote branches
$ git branch -r
List all branches (both local and remote)
$ git branch -a
$ git checkout -b <branch> <SHA1_OF_COMMIT>
This is another chance to use git reflog
to see where your HEAD pointed before the bad pull.
(main)$ git reflog
ab7555f HEAD@{0}: pull origin wrong-branch: Fast-forward
c5bc55a HEAD@{1}: checkout: checkout message goes here
Simply reset your branch back to the desired commit:
$ git reset --hard c5bc55a
Done.
Confirm that you haven't pushed your changes to the server.
git status
should show how many commits you are ahead of origin:
(my-branch)$ git status
# On branch my-branch
# Your branch is ahead of 'origin/my-branch' by 2 commits.
# (use "git push" to publish your local commits)
#
One way of resetting to match origin (to have the same as what is on the remote) is to do this:
(main)$ git reset --hard origin/my-branch
Create the new branch while remaining on main:
(main)$ git branch my-branch
Reset the branch main to the previous commit:
(main)$ git reset --hard HEAD^
HEAD^
is short for HEAD^1
. This stands for the first parent of HEAD
, similarly HEAD^2
stands for the second parent of the commit (merges can have 2 parents).
Note that HEAD^2
is not the same as HEAD~2
(see this link for more information).
Alternatively, if you don't want to use HEAD^
, find out what the commit hash you want to set your main branch to (git log
should do the trick). Then reset to that hash. git push
will make sure that this change is reflected on your remote.
For example, if the hash of the commit that your main branch is supposed to be at is a13b85e
:
(main)$ git reset --hard a13b85e
HEAD is now at a13b85e
Checkout the new branch to continue working:
(main)$ git checkout my-branch
Say you have a working spike (see note), with hundreds of changes. Everything is working. Now, you commit into another branch to save that work:
(solution)$ git add -A && git commit -m "Adding all changes from this spike into one big commit."
When you want to put it into a branch (maybe feature, maybe develop
), you're interested in keeping whole files. You want to split your big commit into smaller ones.
Say you have:
solution
, with the solution to your spike. One ahead of develop
.develop
, where you want to add your changes.You can solve it bringing the contents to your branch:
(develop)$ git checkout solution -- file1.txt
This will get the contents of that file in branch solution
to your branch develop
:
# On branch develop
# Your branch is up-to-date with 'origin/develop'.
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: file1.txt
Then, commit as usual.
Note: Spike solutions are made to analyze or solve the problem. These solutions are used for estimation and discarded once everyone gets clear visualization of the problem. ~ Wikipedia.
Say you are on your main branch. Running git log
, you see you have made two commits:
(main)$ git log
commit e3851e817c451cc36f2e6f3049db528415e3c114
Author: Alex Lee <alexlee@example.com>
Date: Tue Jul 22 15:39:27 2014 -0400
Bug #21 - Added CSRF protection
commit 5ea51731d150f7ddc4a365437931cd8be3bf3131
Author: Alex Lee <alexlee@example.com>
Date: Tue Jul 22 15:39:12 2014 -0400
Bug #14 - Fixed spacing on title
commit a13b85e984171c6e2a1729bb061994525f626d14
Author: Aki Rose <akirose@example.com>
Date: Tue Jul 21 01:12:48 2014 -0400
First commit
Let's take note of our commit hashes for each bug (e3851e8
for #21, 5ea5173
for #14).
First, let's reset our main branch to the correct commit (a13b85e
):
(main)$ git reset --hard a13b85e
HEAD is now at a13b85e
Now, we can create a fresh branch for our bug #21:
(main)$ git checkout -b 21
(21)$
Now, let's cherry-pick the commit for bug #21 on top of our branch. That means we will be applying that commit, and only that commit, directly on top of whatever our head is at.
(21)$ git cherry-pick e3851e8
At this point, there is a possibility there might be conflicts. See the There were conflicts section in the interactive rebasing section above for how to resolve conflicts.
Now let's create a new branch for bug #14, also based on main
(21)$ git checkout main
(main)$ git checkout -b 14
(14)$
And finally, let's cherry-pick the commit for bug #14:
(14)$ git cherry-pick 5ea5173
Once you merge a pull request on GitHub, it gives you the option to delete the merged branch in your fork. If you aren't planning to keep working on the branch, it's cleaner to delete the local copies of the branch so you don't end up cluttering up your working checkout with a lot of stale branches.
$ git fetch -p upstream
where, upstream
is the remote you want to fetch from.
If you're regularly pushing to remote, you should be safe most of the time. But still sometimes you may end up deleting your branches. Let's say we create a branch and create a new file:
(main)$ git checkout -b my-branch
(my-branch)$ git branch
(my-branch)$ touch foo.txt
(my-branch)$ ls
README.md foo.txt
Let's add it and commit.
(my-branch)$ git add .
(my-branch)$ git commit -m 'foo.txt added'
(my-branch)$ foo.txt added
1 files changed, 1 insertions(+)
create mode 100644 foo.txt
(my-branch)$ git log
commit 4e3cd85a670ced7cc17a2b5d8d3d809ac88d5012
Author: siemiatj <siemiatj@example.com>
Date: Wed Jul 30 00:34:10 2014 +0200
foo.txt added
commit 69204cdf0acbab201619d95ad8295928e7f411d5
Author: Kate Hudson <katehudson@example.com>
Date: Tue Jul 29 13:14:46 2014 -0400
Fixes #6: Force pushing after amending commits
Now we're switching back to main and 'accidentally' removing our branch.
(my-branch)$ git checkout main
Switched to branch 'main'
Your branch is up-to-date with 'origin/main'.
(main)$ git branch -D my-branch
Deleted branch my-branch (was 4e3cd85).
(main)$ echo oh noes, deleted my branch!
oh noes, deleted my branch!
At this point you should get familiar with 'reflog', an upgraded logger. It stores the history of all the action in the repo.
(main)$ git reflog
69204cd HEAD@{0}: checkout: moving from my-branch to main
4e3cd85 HEAD@{1}: commit: foo.txt added
69204cd HEAD@{2}: checkout: moving from main to my-branch
As you can see we have commit hash from our deleted branch. Let's see if we can restore our deleted branch.
(main)$ git checkout -b my-branch-help
Switched to a new branch 'my-branch-help'
(my-branch-help)$ git reset --hard 4e3cd85
HEAD is now at 4e3cd85 foo.txt added
(my-branch-help)$ ls
README.md foo.txt
Voila! We got our removed file back. git reflog
is also useful when rebasing goes terribly wrong.
To delete a remote branch:
(main)$ git push origin --delete my-branch
You can also do:
(main)$ git push origin :my-branch
To delete a local branch:
(main)$ git branch -d my-branch
To delete a local branch that has not been merged to the current branch or an upstream:
(main)$ git branch -D my-branch
Say you want to delete all branches that start with fix/
:
(main)$ git branch | grep 'fix/' | xargs git branch -d
To rename the current (local) branch:
(main)$ git branch -m new-name
To rename a different (local) branch:
(main)$ git branch -m old-name new-name
To delete the old-name
remote branch and push the new-name
local branch:
(main)$ git push origin :old_name new_name
First, fetch all branches from remote:
(main)$ git fetch --all
Say you want to checkout to daves
from the remote.
(main)$ git checkout --track origin/daves
Branch daves set up to track remote branch daves from origin.
Switched to a new branch 'daves'
(--track
is shorthand for git checkout -b [branch] [remotename]/[branch]
)
This will give you a local copy of the branch daves
, and any update that has been pushed will also show up remotely.
$ git push <remote> HEAD
If you would also like to set that remote branch as upstream for the current one, use the following instead:
$ git push -u <remote> HEAD
With the upstream
mode and the simple
(default in Git 2.0) mode of the push.default
config, the following command will push the current branch with regards to the remote branch that has been registered previously with -u
:
$ git push
The behavior of the other modes of git push
is described in the doc of push.default
.
You can set a remote branch as the upstream for the current local branch using:
$ git branch --set-upstream-to [remotename]/[branch]
# or, using the shorthand:
$ git branch -u [remotename]/[branch]
To set the upstream remote branch for another local branch:
$ git branch -u [remotename]/[branch] [local-branch]
By checking your remote branches, you can see which remote branch your HEAD is tracking. In some cases, this is not the desired branch.
$ git branch -r
origin/HEAD -> origin/gh-pages
origin/main
To change origin/HEAD
to track origin/main
, you can run this command:
$ git remote set-head origin --auto
origin/HEAD set to main
You've made uncommitted changes and realise you're on the wrong branch. Stash changes and apply them to the branch you want:
(wrong_branch)$ git stash
(wrong_branch)$ git checkout <correct_branch>
(correct_branch)$ git stash apply
You've made a lot of commits on a branch and now want to separate it into two, ending with a branch up to an earlier commit and another with all the changes.
Use git log
to find the commit where you want to split. Then do the following:
(original_branch)$ git checkout -b new_branch
(new_branch)$ git checkout original_branch
(original_branch)$ git reset --hard <sha1 split here>
If you had previously pushed the original_branch
to remote, you will need to do a force push. For more information check Stack Overlflow
You may have merged or rebased your current branch with a wrong branch, or you can't figure it out or finish the rebase/merge process. Git saves the original HEAD pointer in a variable called ORIG_HEAD before doing dangerous operations, so it is simple to recover your branch at the state before the rebase/merge.
(my-branch)$ git reset --hard ORIG_HEAD
Unfortunately, you have to force push, if you want those changes to be reflected on the remote branch. This is because you have changed the history. The remote branch won't accept changes unless you force push. This is one of the main reasons many people use a merge workflow, instead of a rebasing workflow - large teams can get into trouble with developers force pushing. Use this with caution. A safer way to use rebase is not to reflect your changes on the remote branch at all, and instead to do the following:
(main)$ git checkout my-branch
(my-branch)$ git rebase -i main
(my-branch)$ git checkout main
(main)$ git merge --ff-only my-branch
For more, see this SO thread.
Let's suppose you are working in a branch that is/will become a pull-request against main
. In the simplest case when all you want to do is to combine all commits into a single one and you don't care about commit timestamps, you can reset and recommit. Make sure the main branch is up to date and all your changes committed, then:
(my-branch)$ git reset --soft main
(my-branch)$ git commit -am "New awesome feature"
If you want more control, and also to preserve timestamps, you need to do something called an interactive rebase:
(my-branch)$ git rebase -i main
If you aren't working against another branch you'll have to rebase relative to your HEAD
. If you want to squash the last 2 commits, for example, you'll have to rebase against HEAD~2
. For the last 3, HEAD~3
, etc.
(main)$ git rebase -i HEAD~2
After you run the interactive rebase command, you will see something like this in your text editor:
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
pick b729ad5 fixup
pick e3851e8 another fix
# Rebase 8074d12..b729ad5 onto 8074d12
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
All the lines beginning with a #
are comments, they won't affect your rebase.
Then you replace pick
commands with any in the list above, and you can also remove commits by removing corresponding lines.
For example, if you want to leave the oldest (first) commit alone and combine all the following commits with the second oldest, you should edit the letter next to each commit except the first and the second to say f
:
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
f b729ad5 fixup
f e3851e8 another fix
If you want to combine these commits and rename the commit, you should additionally add an r
next to the second commit or simply use s
instead of f
:
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
s b729ad5 fixup
s e3851e8 another fix
You can then rename the commit in the next text prompt that pops up.
Newer, awesomer features
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# rebase in progress; onto 8074d12
# You are currently editing a commit while rebasing branch 'main' on '8074d12'.
#
# Changes to be committed:
# modified: README.md
#
If everything is successful, you should see something like this:
(main)$ Successfully rebased and updated refs/heads/main.
--no-commit
performs the merge but pretends the merge failed and does not autocommit, giving the user a chance to inspect and further tweak the merge result before committing. no-ff
maintains evidence that a feature branch once existed, keeping project history consistent.
(main)$ git merge --no-ff --no-commit my-branch
(main)$ git merge --squash my-branch
Sometimes you have several work in progress commits that you want to combine before you push them upstream. You don't want to accidentally combine any commits that have already been pushed upstream because someone else may have already made commits that reference them.
(main)$ git rebase -i @{u}
This will do an interactive rebase that lists only the commits that you haven't already pushed, so it will be safe to reorder/fix/squash anything in the list.
Sometimes the merge can produce problems in certain files, in those cases we can use the option abort
to abort the current conflict resolution process, and try to reconstruct the pre-merge state.
(my-branch)$ git merge --abort
This command is available since Git version >= 1.7.4
Say I have a main branch, a feature-1 branch branched from main, and a feature-2 branch branched off of feature-1. If I make a commit to feature-1, then the parent commit of feature-2 is no longer accurate (it should be the head of feature-1, since we branched off of it). We can fix this with git rebase --onto
.
(feature-2)$ git rebase --onto feature-1 <the first commit in your feature-2 branch that you don't want to bring along> feature-2
This helps in sticky scenarios where you might have a feature built on another feature that hasn't been merged yet, and a bugfix on the feature-1 branch needs to be reflected in your feature-2 branch.
To check if all commits on a branch are merged into another branch, you should diff between the heads (or any commits) of those branches:
(main)$ git log --graph --left-right --cherry-pick --oneline HEAD...feature/120-on-scroll
This will tell you if any commits are in one but not the other, and will give you a list of any nonshared between the branches. Another option is to do this:
(main)$ git log main ^feature/120-on-scroll --no-merges
If you're seeing this:
noop
That means you are trying to rebase against a branch that is at an identical commit, or is ahead of your current branch. You can try:
HEAD~2
or earlier instead
If you are unable to successfully complete the rebase, you may have to resolve conflicts.
First run git status
to see which files have conflicts in them:
(my-branch)$ git status
On branch my-branch
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
both modified: README.md
In this example, README.md
has conflicts. Open that file and look for the following:
<<<<<<< HEAD
some code
=========
some code
>>>>>>> new-commit
You will need to resolve the differences between the code that was added in your new commit (in the example, everything from the middle line to new-commit
) and your HEAD
.
If you want to keep one branch's version of the code, you can use --ours
or --theirs
:
(main*)$ git checkout --ours README.md
--ours
to keep changes from the local branch, or --theirs
to keep changes from the other branch.--theirs
to keep changes from the local branch, or --ours
to keep changes from the other branch. For an explanation of this swap, see this note in the Git documentation.If the merges are more complicated, you can use a visual diff editor:
(main*)$ git mergetool -t opendiff
After you have resolved all conflicts and tested your code, git add
the files you have changed, and then continue the rebase with git rebase --continue
(my-branch)$ git add README.md
(my-branch)$ git rebase --continue
If after resolving all the conflicts you end up with an identical tree to what it was before the commit, you need to git rebase --skip
instead.
If at any time you want to stop the entire rebase and go back to the original state of your branch, you can do so:
(my-branch)$ git rebase --abort
To stash all the edits in your working directory
$ git stash
If you also want to stash untracked files, use -u
option.
$ git stash -u
To stash only one file from your working directory
$ git stash push working-directory-path/filename.ext
To stash multiple files from your working directory
$ git stash push working-directory-path/filename1.ext working-directory-path/filename2.ext
$ git stash save <message>
or
$ git stash push -m <message>
First check your list of stashes with message using
$ git stash list
Then apply a specific stash from the list using
$ git stash apply "stash@{n}"
Here, 'n' indicates the position of the stash in the stack. The topmost stash will be position 0.
Furthermore, using a time-based stash reference is also possible.
$ git stash apply "stash@{2.hours.ago}"
You can manually create a stash commit
, and then use git stash store
.
$ git stash create
$ git stash store -m <message> CREATED_SHA1
To find a certain string which was introduced in any commit, you can use the following structure:
$ git log -S "string to find"
Commons parameters:
--source
means to show the ref name given on the command line by which each commit was reached.
--all
means to start from every branch.
--reverse
prints in reverse order, it means that will show the first commit that made the change.
To find all commits by author/committer you can use:
$ git log --author=<name or email>
$ git log --committer=<name or email>
Keep in mind that author and committer are not the same. The --author
is the person who originally wrote the code; on the other hand, the --committer
, is the person who committed the code on behalf of the original author.
To find all commits containing a specific file you can use:
$ git log -- <path to file>
You would usually specify an exact path, but you may also use wild cards in the path and file name:
$ git log -- **/*.js
While using wildcards, it's useful to inform --name-status
to see the list of committed files:
$ git log --name-status -- **/*.js
To trace the evolution of a single function you can use:
$ git log -L :FunctionName:FilePath
Note that you can combine this with further git log
options, like revision ranges and commit limits.
To find all tags containing a specific commit:
$ git tag --contains <commitid>
$ git clone --recursive git://github.com/foo/bar.git
If already cloned:
$ git submodule update --init --recursive
Creating a submodule is pretty straight-forward, but deleting them less so. The commands you need are:
$ git submodule deinit submodulename
$ git rm submodulename
$ git rm --cached submodulename
$ rm -rf .git/modules/submodulename
$ git checkout <branch-you-want-the-directory-from> -- <folder-name or file-name>
First find the commit when the file last existed:
$ git rev-list -n 1 HEAD -- filename
Then checkout that file:
git checkout deletingcommitid^ -- filename
$ git tag -d <tag_name>
$ git push <remote> :refs/tags/<tag_name>
If you want to recover a tag that was already deleted, you can do so by following these steps: First, you need to find the unreachable tag:
$ git fsck --unreachable | grep tag
Make a note of the tag's hash. Then, restore the deleted tag with following, making use of git update-ref
:
$ git update-ref refs/tags/<tag_name> <hash>
Your tag should now have been restored.
If someone has sent you a pull request on GitHub, but then deleted their original fork, you will be unable to clone their repository or to use git am
as the .diff, .patch URLs become unavailable. But you can checkout the PR itself using GitHub's special refs. To fetch the content of PR#1 into a new branch called pr_1:
$ git fetch origin refs/pull/1/head:pr_1
From github.com:foo/bar
* [new ref] refs/pull/1/head -> pr_1
$ git archive --format zip --output /full/path/to/zipfile.zip main
If there is a tag on a remote repository that has the same name as a branch you will get the following error when trying to push that branch with a standard $ git push <remote> <branch>
command.
$ git push origin <branch>
error: dst refspec same matches more than one.
error: failed to push some refs to '<git server>'
Fix this by specifying you want to push the head reference.
$ git push origin refs/heads/<branch-name>
If you want to push a tag to a remote repository that has the same name as a branch, you can use a similar command.
$ git push origin refs/tags/<tag-name>
(main)$ git mv --force myfile MyFile
(main)$ git fetch --all
(main)$ git reset --hard origin/main
(main)$ git rm --cached log.txt
Assuming the hash of the commit you want is c5f567:
(main)$ git checkout c5f567 -- file1/to/restore file2/to/restore
If you want to revert to changes made just 1 commit before c5f567, pass the commit hash as c5f567~1:
(main)$ git checkout c5f567~1 -- file1/to/restore file2/to/restore
Assuming you want to compare last commit with file from commit c5f567:
$ git diff HEAD:path_to_file/file c5f567:path_to_file/file
Same goes for branches:
$ git diff main:path_to_file/file staging:path_to_file/file
This works great for config templates or other files that require locally adding credentials that shouldn't be committed.
$ git update-index --assume-unchanged file-to-ignore
Note that this does not remove the file from source control - it is only ignored locally. To undo this and tell Git to notice changes again, this clears the ignore flag:
$ git update-index --no-assume-unchanged file-to-stop-ignoring
The git-bisect command uses a binary search to find which commit in your Git history introduced a bug.
Suppose you're on the main
branch, and you want to find the commit that broke some feature. You start bisect:
$ git bisect start
Then you should specify which commit is bad, and which one is known to be good. Assuming that your current version is bad, and v1.1.1
is good:
$ git bisect bad
$ git bisect good v1.1.1
Now git-bisect
selects a commit in the middle of the range that you specified, checks it out, and asks you whether it's good or bad. You should see something like:
$ Bisecting: 5 revision left to test after this (roughly 5 step)
$ [c44abbbee29cb93d8499283101fe7c8d9d97f0fe] Commit message
$ (c44abbb)$
You will now check if this commit is good or bad. If it's good:
$ (c44abbb)$ git bisect good
and git-bisect
will select another commit from the range for you. This process (selecting good
or bad
) will repeat until there are no more revisions left to inspect, and the command will finally print a description of the first bad commit.
On OS X and Linux, your git configuration file is stored in ~/.gitconfig
. I've added some example aliases I use as shortcuts (and some of my common typos) in the [alias]
section as shown below:
[alias]
a = add
amend = commit --amend
c = commit
ca = commit --amend
ci = commit -a
co = checkout
d = diff
dc = diff --changed
ds = diff --staged
extend = commit --amend -C HEAD
f = fetch
loll = log --graph --decorate --pretty=oneline --abbrev-commit
m = merge
one = log --pretty=oneline
outstanding = rebase -i @{u}
reword = commit --amend --only
s = status
unpushed = log @{u}
wc = whatchanged
wip = rebase -i @{u}
zap = fetch -p
day = log --reverse --no-merges --branches=* --date=local --since=midnight --author=\"$(git config --get user.name)\"
delete-merged-branches = "!f() { git checkout --quiet main && git branch --merged | grep --invert-match '\\*' | xargs -n 1 git branch --delete; git checkout --quiet @{-1}; }; f"
You can’t! Git doesn’t support this, but there’s a hack. You can create a .gitignore file in the directory with the following contents:
# Ignore everything in this directory
*
# Except this file
!.gitignore
Another common convention is to make an empty file in the folder, titled .gitkeep.
$ mkdir mydir
$ touch mydir/.gitkeep
You can also name the file as just .keep , in which case the second line above would be touch mydir/.keep
You might have a repository that requires authentication. In which case you can cache a username and password so you don't have to enter it on every push and pull. Credential helper can do this for you.
$ git config --global credential.helper cache
# Set git to use the credential memory cache
$ git config --global credential.helper 'cache --timeout=3600'
# Set the cache to timeout after 1 hour (setting is in seconds)
To find a credential helper:
$ git help -a | grep credential
# Shows you possible credential helpers
For OS specific credential caching:
$ git config --global credential.helper osxkeychain
# For OSX
$ git config --global credential.helper manager
# Git for Windows 2.7.3+
$ git config --global credential.helper gnome-keyring
# Ubuntu and other GNOME-based distros
More credential helpers can likely be found for different distributions and operating systems.
$ git config core.fileMode false
If you want to make this the default behaviour for logged-in users, then use:
$ git config --global core.fileMode false
To configure user information used across all local repositories, and to set a name that is identifiable for credit when review version history:
$ git config --global user.name “[firstname lastname]”
To set an email address that will be associated with each history marker:
git config --global user.email “[valid-email]”
So, you're screwed - you reset
something, or you merged the wrong branch, or you force pushed and now you can't find your commits. You know, at some point, you were doing alright, and you want to go back to some state you were at.
This is what git reflog
is for. reflog
keeps track of any changes to the tip of a branch, even if that tip isn't referenced by a branch or a tag. Basically, every time HEAD changes, a new entry is added to the reflog. This only works for local repositories, sadly, and it only tracks movements (not changes to a file that weren't recorded anywhere, for instance).
(main)$ git reflog
0a2e358 HEAD@{0}: reset: moving to HEAD~2
0254ea7 HEAD@{1}: checkout: moving from 2.2 to main
c10f740 HEAD@{2}: checkout: moving from main to 2.2
The reflog above shows a checkout from main to the 2.2 branch and back. From there, there's a hard reset to an older commit. The latest activity is represented at the top labeled HEAD@{0}
.
If it turns out that you accidentally moved back, the reflog will contain the commit main pointed to (0254ea7) before you accidentally dropped 2 commits.
$ git reset --hard 0254ea7
Using git reset
it is then possible to change main back to the commit it was before. This provides a safety net in case history was accidentally changed.
(copied and edited from Source).
Once you're comfortable with what the above commands are doing, you might want to create some shortcuts for Git Bash. This allows you to work a lot faster by doing complex tasks in really short commands.
alias sq=squash
function squash() {
git rebase -i HEAD~$1
}
Copy those commands to your .bashrc or .bash_profile.
If you are using PowerShell on Windows, you can also set up aliases and functions. Add these commands to your profile, whose path is defined in the $profile
variable. Learn more at the About Profiles page on the Microsoft documentation site.
Set-Alias sq Squash-Commits
function Squash-Commits {
git rebase -i HEAD~$1
}
Other Resources
🌍 English ∙ Español ∙ Русский ∙ 简体中文∙ 한국어 ∙ Tiếng Việt ∙ Français ∙ 日本語
Author: K88hudson
Source Code: https://github.com/k88hudson/git-flight-rules
License: CC-BY-SA-4.0 License
1642173480
Flight rules for Git
🌍 English ∙ Español ∙ Русский ∙ 简体中文∙ 한국어 ∙ Tiếng Việt ∙ Français ∙ 日本語
A guide for astronauts (now, programmers using Git) about what to do when things go wrong.
Flight Rules are the hard-earned body of knowledge recorded in manuals that list, step-by-step, what to do if X occurs, and why. Essentially, they are extremely detailed, scenario-specific standard operating procedures. [...]
NASA has been capturing our missteps, disasters and solutions since the early 1960s, when Mercury-era ground teams first started gathering "lessons learned" into a compendium that now lists thousands of problematic situations, from engine failure to busted hatch handles to computer glitches, and their solutions.
— Chris Hadfield, An Astronaut's Guide to Life on Earth.
For clarity's sake all examples in this document use a customized bash prompt in order to indicate the current branch and whether or not there are staged changes. The branch is enclosed in parentheses, and a *
next to the branch name indicates staged changes.
All commands should work for at least git version 2.13.0. See the git website to update your local git version.
Table of Contents generated with DocToc
To initialize an existing directory as a Git repository:
(my-folder) $ git init
To clone (copy) a remote repository, copy the URL for the repository, and run:
$ git clone [url]
This will save it to a folder named the same as the remote repository's. Make sure you have a connection to the remote server you are cloning from (for most purposes this means making sure you are connected to the internet).
To clone it into a folder with a different name than the default repository name:
$ git clone [url] name-of-new-folder
There are a few possible problems here:
If you cloned the wrong repository, simply delete the directory created after running git clone
and clone the correct repository.
If you set the wrong repository as the origin of an existing local repository, change the URL of your origin by running:
$ git remote set-url origin [url of the actual repo]
For more, see this StackOverflow topic.
Git doesn't allow you to add code to someone else's repository without access rights. Neither does GitHub, which is not the same as Git, but rather a hosted service for Git repositories. However, you can suggest code using patches, or, on GitHub, forks and pull requests.
First, a bit about forking. A fork is a copy of a repository. It is not a git operation, but is a common action on GitHub, Bitbucket, GitLab — or anywhere people host Git repositories. You can fork a repository through the hosted UI.
After you've forked a repository, you normally need to clone the repository to your machine. You can do some small edits on GitHub, for instance, without cloning, but this isn't a github-flight-rules list, so let's go with how to do this locally.
# if you are using ssh
$ git clone git@github.com:k88hudson/git-flight-rules.git
# if you are using https
$ git clone https://github.com/k88hudson/git-flight-rules.git
If you cd
into the resulting directory, and type git remote
, you'll see a list of the remotes. Normally there will be one remote - origin
- which will point to k88hudson/git-flight-rules
. In this case, we also want a remote that will point to your fork.
First, to follow a Git convention, we normally use the remote name origin
for your own repository and upstream
for whatever you've forked. So, rename the origin
remote to upstream
$ git remote rename origin upstream
You can also do this using git remote set-url
, but it takes longer and is more steps.
Then, set up a new remote that points to your project.
$ git remote add origin git@github.com:YourName/git-flight-rules.git
Note that now you have two remotes.
origin
references your own repository.upstream
references the original one.From origin, you can read and write. From upstream, you can only read.
When you've finished making whatever changes you like, push your changes (normally in a branch) to the remote named origin
. If you're on a branch, you could use --set-upstream
to avoid specifying the remote tracking branch on every future push using this branch. For instance:
$ (feature/my-feature) git push --set-upstream origin feature/my-feature
There is no way to suggest a pull request using the CLI using Git (although there are tools, like hub, which will do this for you). So, if you're ready to make a pull request, go to your GitHub (or another Git host) and create a new pull request. Note that your host automatically links the original and forked repositories.
After all of this, do not forget to respond to any code review feedback.
Another approach to suggesting code changes that doesn't rely on third party sites such as Github is to use git format-patch
.
format-patch
creates a .patch file for one or more commits. This file is essentially a list of changes that looks similar to the commit diffs you can view on Github.
A patch can be viewed and even edited by the recipient and applied using git am
.
For example, to create a patch based on the previous commit you would run git format-patch HEAD^
which would create a .patch file called something like 0001-My-Commit-Message.patch.
To apply this patch file to your repository you would run git am ./0001-My-Commit-Message.patch
.
Patches can also be sent via email using the git send-email
command. For information on usage and configuration see: https://git-send-email.io
After a while, the upstream
repository may have been updated, and these updates need to be pulled into your origin
repo. Remember that like you, other people are contributing too. Suppose that you are in your own feature branch and you need to update it with the original repository updates.
You probably have set up a remote that points to the original project. If not, do this now. Generally we use upstream
as a remote name:
$ (main) git remote add upstream <link-to-original-repository>
# $ (main) git remote add upstream git@github.com:k88hudson/git-flight-rules.git
Now you can fetch from upstream and get the latest updates.
$ (main) git fetch upstream
$ (main) git merge upstream/main
# or using a single command
$ (main) git pull upstream main
Let's say that you just blindly committed changes with git commit -a
and you're not sure what the actual content of the commit you just made was. You can show the latest commit on your current HEAD with:
(main)$ git show
Or
$ git log -n1 -p
If you want to see a file at a specific commit, you can also do this (where <commitid>
is the commit you're interested in):
$ git show <commitid>:filename
If you wrote the wrong thing and the commit has not yet been pushed, you can do the following to change the commit message without changing the changes in the commit:
$ git commit --amend --only
This will open your default text editor, where you can edit the message. On the other hand, you can do this all in one command:
$ git commit --amend --only -m 'xxxxxxx'
If you have already pushed the message, you can amend the commit and force push, but this is not recommended.
If it's a single commit, amend it
$ git commit --amend --no-edit --author "New Authorname <authoremail@mydomain.com>"
An alternative is to correctly configure your author settings in git config --global author.(name|email)
and then use
$ git commit --amend --reset-author --no-edit
If you need to change all of history, see the man page for git filter-branch
.
In order to remove changes for a file from the previous commit, do the following:
$ git checkout HEAD^ myfile
$ git add myfile
$ git commit --amend --no-edit
In case the file was newly added to the commit and you want to remove it (from Git alone), do:
$ git rm --cached myfile
$ git commit --amend --no-edit
This is particularly useful when you have an open patch and you have committed an unnecessary file, and need to force push to update the patch on a remote. The --no-edit
option is used to keep the existing commit message.
If you need to delete pushed commits, you can use the following. However, it will irreversibly change your history, and mess up the history of anyone else who had already pulled from the repository. In short, if you're not sure, you should never do this, ever.
$ git reset HEAD^ --hard
$ git push --force-with-lease [remote] [branch]
If you haven't pushed, to reset Git to the state it was in before you made your last commit (while keeping your staged changes):
(my-branch*)$ git reset --soft HEAD@{1}
This only works if you haven't pushed. If you have pushed, the only truly safe thing to do is git revert SHAofBadCommit
. That will create a new commit that undoes all the previous commit's changes. Or, if the branch you pushed to is rebase-safe (ie. other devs aren't expected to pull from it), you can just use git push --force-with-lease
. For more, see the above section.
The same warning applies as above. Never do this if possible.
$ git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
$ git push --force-with-lease [remote] [branch]
Or do an interactive rebase and remove the line(s) corresponding to commit(s) you want to see removed.
To https://github.com/yourusername/repo.git
! [rejected] mybranch -> mybranch (non-fast-forward)
error: failed to push some refs to 'https://github.com/tanay1337/webmaker.org.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Note that, as with rebasing (see below), amending replaces the old commit with a new one, so you must force push (--force-with-lease
) your changes if you have already pushed the pre-amended commit to your remote. Be careful when you do this – always make sure you specify a branch!
(my-branch)$ git push origin mybranch --force-with-lease
In general, avoid force pushing. It is best to create and push a new commit rather than force-pushing the amended commit as it will cause conflicts in the source history for any other developer who has interacted with the branch in question or any child branches. --force-with-lease
will still fail, if someone else was also working on the same branch as you, and your push would overwrite those changes.
If you are absolutely sure that nobody is working on the same branch or you want to update the tip of the branch unconditionally, you can use --force
(-f
), but this should be avoided in general.
If you accidentally do git reset --hard
, you can normally still get your commit back, as git keeps a log of everything for a few days.
Note: This is only valid if your work is backed up, i.e., either committed or stashed. git reset --hard
will remove uncommitted modifications, so use it with caution. (A safer option is git reset --keep
.)
(main)$ git reflog
You'll see a list of your past commits, and a commit for the reset. Choose the SHA of the commit you want to return to, and reset again:
(main)$ git reset --hard SHA1234
And you should be good to go.
If you accidentally merged a feature branch to the main development branch before it was ready to be merged, you can still undo the merge. But there's a catch: A merge commit has more than one parent (usually two).
The command to use
(feature-branch)$ git revert -m 1 <commit>
where the -m 1 option says to select parent number 1 (the branch into which the merge was made) as the parent to revert to.
Note: the parent number is not a commit identifier. Rather, a merge commit has a line Merge: 8e2ce2d 86ac2e7
. The parent number is the 1-based index of the desired parent on this line, the first identifier is number 1, the second is number 2, and so on.
If you accidentally pushed files containing sensitive, or private data (passwords, keys, etc.), you can amend the previous commit. Keep in mind that once you have pushed a commit, you should consider any data it contains to be compromised. These steps can remove the sensitive data from your public repo or your local copy, but you cannot remove the sensitive data from other people's pulled copies. If you committed a password, change it immediately. If you committed a key, re-generate it immediately. Amending the pushed commit is not enough, since anyone could have pulled the original commit containing your sensitive data in the meantime.
If you edit the file and remove the sensitive data, then run
(feature-branch)$ git add edited_file
(feature-branch)$ git commit --amend --no-edit
(feature-branch)$ git push --force-with-lease origin [branch]
If you want to remove an entire file (but keep it locally), then run
(feature-branch)$ git rm --cached sensitive_file
echo sensitive_file >> .gitignore
(feature-branch)$ git add .gitignore
(feature-branch)$ git commit --amend --no-edit
(feature-branch)$ git push --force-with-lease origin [branch]
Alternatively store your sensitive data in local environment variables.
If you want to completely remove an entire file (and not keep it locally), then run
(feature-branch)$ git rm sensitive_file
(feature-branch)$ git commit --amend --no-edit
(feature-branch)$ git push --force-with-lease origin [branch]
If you have made other commits in the meantime (i.e. the sensitive data is in a commit before the previous commit), you will have to rebase.
If the file you want to delete is secret or sensitive, instead see how to remove sensitive files.
Even if you delete a large or unwanted file in a recent commit, it still exists in git history, in your repo's .git
folder, and will make git clone
download unneeded files.
The actions in this part of the guide will require a force push, and rewrite large sections of repo history, so if you are working with remote collaborators, check first that any local work of theirs is pushed.
There are two options for rewriting history, the built-in git-filter-branch
or bfg-repo-cleaner
. bfg
is significantly cleaner and more performant, but it is a third-party download and requires java. We will describe both alternatives. The final step is to force push your changes, which requires special consideration on top of a regular force push, given that a great deal of repo history will have been permanently changed.
Using bfg-repo-cleaner requires java. Download the bfg jar from the link here. Our examples will use bfg.jar
, but your download may have a version number, e.g. bfg-1.13.0.jar
.
To delete a specific file.
(main)$ git rm path/to/filetoremove
(main)$ git commit -m "Commit removing filetoremove"
(main)$ java -jar ~/Downloads/bfg.jar --delete-files filetoremove
Note that in bfg you must use the plain file name even if it is in a subdirectory.
You can also delete a file by pattern, e.g.:
(main)$ git rm *.jpg
(main)$ git commit -m "Commit removing *.jpg"
(main)$ java -jar ~/Downloads/bfg.jar --delete-files *.jpg
With bfg, the files that exist on your latest commit will not be affected. For example, if you had several large .tga files in your repo, and then in an earlier commit, you deleted a subset of them, this call does not touch files present in the latest commit
Note, if you renamed a file as part of a commit, e.g. if it started as LargeFileFirstName.mp4
and a commit changed it to LargeFileSecondName.mp4
, running java -jar ~/Downloads/bfg.jar --delete-files LargeFileSecondName.mp4
will not remove it from git history. Either run the --delete-files
command with both filenames, or with a matching pattern.
git-filter-branch
is more cumbersome and has less features, but you may use it if you cannot install or run bfg
.
In the below, replace filepattern
may be a specific name or pattern, e.g. *.jpg
. This will remove files matching the pattern from all history and branches.
(main)$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch filepattern' --prune-empty --tag-name-filter cat -- --all
Behind-the-scenes explanation:
--tag-name-filter cat
is a cumbersome, but simplest, way to apply the original tags to the new commits, using the command cat.
--prune-empty
removes any now-empty commits.
Once you have removed your desired files, test carefully that you haven't broken anything in your repo - if you have, it is easiest to re-clone your repo to start over. To finish, optionally use git garbage collection to minimize your local .git folder size, and then force push.
(main)$ git reflog expire --expire=now --all && git gc --prune=now --aggressive
(main)$ git push origin --force --tags
Since you just rewrote the entire git repo history, the git push
operation may be too large, and return the error “The remote end hung up unexpectedly”
. If this happens, you can try increasing the git post buffer:
(main)$ git config http.postBuffer 524288000
(main)$ git push --force
If this does not work, you will need to manually push the repo history in chunks of commits. In the command below, try increasing <number>
until the push operation succeeds.
(main)$ git push -u origin HEAD~<number>:refs/head/main --force
Once the push operation succeeds the first time, decrease <number>
gradually until a conventional git push
succeeds.
Consider you created some (e.g. three) commits and later realize you missed doing something that belongs contextually into the first of those commits. This bothers you, because if you'd create a new commit containing those changes, you'd have a clean code base, but your commits weren't atomic (i.e. changes that belonged to each other weren't in the same commit). In such a situation you may want to change the commit where these changes belong to, include them and have the following commits unaltered. In such a case, git rebase
might save you.
Consider a situation where you want to change the third last commit you made.
(your-branch)$ git rebase -i HEAD~4
gets you into interactive rebase mode, which allows you to edit any of your last three commits. A text editor pops up, showing you something like
pick 9e1d264 The third last commit
pick 4b6e19a The second to last commit
pick f4037ec The last commit
which you change into
edit 9e1d264 The third last commit
pick 4b6e19a The second to last commit
pick f4037ec The last commit
This tells rebase that you want to edit your third last commit and keep the other two unaltered. Then you'll save (and close) the editor. Git will then start to rebase. It stops on the commit you want to alter, giving you the chance to edit that commit. Now you can apply the changes which you missed applying when you initially committed that commit. You do so by editing and staging them. Afterwards you'll run
(your-branch)$ git commit --amend
which tells Git to recreate the commit, but to leave the commit message unedited. Having done that, the hard part is solved.
(your-branch)$ git rebase --continue
will do the rest of the work for you.
$ git add -u
# to stage files with ext .txt
$ git add -u *.txt
# to stage all files inside directory src
$ git add -u src/
(my-branch*)$ git commit --amend
If you already know you don't want to change the commit message, you can tell git to reuse the commit message:
(my-branch*)$ git commit --amend -C HEAD
Normally, if you want to stage part of a file, you run this:
$ git add --patch filename.x
-p
will work for short. This will open interactive mode. You would be able to use the s
option to split the commit - however, if the file is new, you will not have this option. To add a new file, do this:
$ git add -N filename.x
Then, you will need to use the e
option to manually choose which lines to add. Running git diff --cached
or git diff --staged
will show you which lines you have staged compared to which are still saved locally.
git add
will add the entire file to a commit. git add -p
will allow to interactively select which changes you want to add.
git reset -p
will open a patch mode reset dialog. This is similar to git add -p
, except that selecting "yes" will unstage the change, removing it from the upcoming commit.
In many cases, you should unstage all of your staged files and then pick the file you want and commit it. However, if you want to switch the staged and unstaged edits, you can create a temporary commit to store your staged files, stage your unstaged files and then stash them. Then, reset the temporary commit and pop your stash.
$ git commit -m "WIP"
$ git add . # This will also add untracked files.
$ git stash
$ git reset HEAD^
$ git stash pop --index 0
NOTE 1: The reason to use pop
here is want to keep idempotent as much as possible. NOTE 2: Your staged files will be marked as unstaged if you don't use the --index
flag. (This link explains why.)
$ git checkout -b my-branch
$ git stash
$ git checkout my-branch
$ git stash pop
If you want to discard all your local staged and unstaged changes, you can do this:
(my-branch)$ git reset --hard
# or
(main)$ git checkout -f
This will unstage all files you might have staged with git add
:
$ git reset
This will revert all local uncommitted changes (should be executed in repo root):
$ git checkout .
You can also revert uncommitted changes to a particular file or directory:
$ git checkout [some_dir|file.txt]
Yet another way to revert all uncommitted changes (longer to type, but works from any subdirectory):
$ git reset --hard HEAD
This will remove all local untracked files, so only files tracked by Git remain:
$ git clean -fd
-x
will also remove all ignored files.
When you want to get rid of some, but not all changes in your working copy.
Checkout undesired changes, keep good changes.
$ git checkout -p
# Answer y to all of the snippets you want to drop
Another strategy involves using stash
. Stash all the good changes, reset working copy, and reapply good changes.
$ git stash -p
# Select all of the snippets you want to save
$ git reset --hard
$ git stash pop
Alternatively, stash your undesired changes, and then drop stash.
$ git stash -p
# Select all of the snippets you don't want to save
$ git stash drop
When you want to get rid of one specific file in your working copy.
$ git checkout myFile
Alternatively, to discard multiple files in your working copy, list them all.
$ git checkout myFirstFile mySecondFile
When you want to get rid of all of your unstaged local uncommitted changes
$ git checkout .
When you want to get rid of all of your untracked files
$ git clean -f
Sometimes we have one or more files that accidentally ended up being staged, and these files have not been committed before. To unstage them:
$ git reset -- <filename>
This results in unstaging the file and make it look like it's untracked.
List local branches
$ git branch
List remote branches
$ git branch -r
List all branches (both local and remote)
$ git branch -a
$ git checkout -b <branch> <SHA1_OF_COMMIT>
This is another chance to use git reflog
to see where your HEAD pointed before the bad pull.
(main)$ git reflog
ab7555f HEAD@{0}: pull origin wrong-branch: Fast-forward
c5bc55a HEAD@{1}: checkout: checkout message goes here
Simply reset your branch back to the desired commit:
$ git reset --hard c5bc55a
Done.
Confirm that you haven't pushed your changes to the server.
git status
should show how many commits you are ahead of origin:
(my-branch)$ git status
# On branch my-branch
# Your branch is ahead of 'origin/my-branch' by 2 commits.
# (use "git push" to publish your local commits)
#
One way of resetting to match origin (to have the same as what is on the remote) is to do this:
(main)$ git reset --hard origin/my-branch
Create the new branch while remaining on main:
(main)$ git branch my-branch
Reset the branch main to the previous commit:
(main)$ git reset --hard HEAD^
HEAD^
is short for HEAD^1
. This stands for the first parent of HEAD
, similarly HEAD^2
stands for the second parent of the commit (merges can have 2 parents).
Note that HEAD^2
is not the same as HEAD~2
(see this link for more information).
Alternatively, if you don't want to use HEAD^
, find out what the commit hash you want to set your main branch to (git log
should do the trick). Then reset to that hash. git push
will make sure that this change is reflected on your remote.
For example, if the hash of the commit that your main branch is supposed to be at is a13b85e
:
(main)$ git reset --hard a13b85e
HEAD is now at a13b85e
Checkout the new branch to continue working:
(main)$ git checkout my-branch
Say you have a working spike (see note), with hundreds of changes. Everything is working. Now, you commit into another branch to save that work:
(solution)$ git add -A && git commit -m "Adding all changes from this spike into one big commit."
When you want to put it into a branch (maybe feature, maybe develop
), you're interested in keeping whole files. You want to split your big commit into smaller ones.
Say you have:
solution
, with the solution to your spike. One ahead of develop
.develop
, where you want to add your changes.You can solve it bringing the contents to your branch:
(develop)$ git checkout solution -- file1.txt
This will get the contents of that file in branch solution
to your branch develop
:
# On branch develop
# Your branch is up-to-date with 'origin/develop'.
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: file1.txt
Then, commit as usual.
Note: Spike solutions are made to analyze or solve the problem. These solutions are used for estimation and discarded once everyone gets clear visualization of the problem. ~ Wikipedia.
Say you are on your main branch. Running git log
, you see you have made two commits:
(main)$ git log
commit e3851e817c451cc36f2e6f3049db528415e3c114
Author: Alex Lee <alexlee@example.com>
Date: Tue Jul 22 15:39:27 2014 -0400
Bug #21 - Added CSRF protection
commit 5ea51731d150f7ddc4a365437931cd8be3bf3131
Author: Alex Lee <alexlee@example.com>
Date: Tue Jul 22 15:39:12 2014 -0400
Bug #14 - Fixed spacing on title
commit a13b85e984171c6e2a1729bb061994525f626d14
Author: Aki Rose <akirose@example.com>
Date: Tue Jul 21 01:12:48 2014 -0400
First commit
Let's take note of our commit hashes for each bug (e3851e8
for #21, 5ea5173
for #14).
First, let's reset our main branch to the correct commit (a13b85e
):
(main)$ git reset --hard a13b85e
HEAD is now at a13b85e
Now, we can create a fresh branch for our bug #21:
(main)$ git checkout -b 21
(21)$
Now, let's cherry-pick the commit for bug #21 on top of our branch. That means we will be applying that commit, and only that commit, directly on top of whatever our head is at.
(21)$ git cherry-pick e3851e8
At this point, there is a possibility there might be conflicts. See the There were conflicts section in the interactive rebasing section above for how to resolve conflicts.
Now let's create a new branch for bug #14, also based on main
(21)$ git checkout main
(main)$ git checkout -b 14
(14)$
And finally, let's cherry-pick the commit for bug #14:
(14)$ git cherry-pick 5ea5173
Once you merge a pull request on GitHub, it gives you the option to delete the merged branch in your fork. If you aren't planning to keep working on the branch, it's cleaner to delete the local copies of the branch so you don't end up cluttering up your working checkout with a lot of stale branches.
$ git fetch -p upstream
where, upstream
is the remote you want to fetch from.
If you're regularly pushing to remote, you should be safe most of the time. But still sometimes you may end up deleting your branches. Let's say we create a branch and create a new file:
(main)$ git checkout -b my-branch
(my-branch)$ git branch
(my-branch)$ touch foo.txt
(my-branch)$ ls
README.md foo.txt
Let's add it and commit.
(my-branch)$ git add .
(my-branch)$ git commit -m 'foo.txt added'
(my-branch)$ foo.txt added
1 files changed, 1 insertions(+)
create mode 100644 foo.txt
(my-branch)$ git log
commit 4e3cd85a670ced7cc17a2b5d8d3d809ac88d5012
Author: siemiatj <siemiatj@example.com>
Date: Wed Jul 30 00:34:10 2014 +0200
foo.txt added
commit 69204cdf0acbab201619d95ad8295928e7f411d5
Author: Kate Hudson <katehudson@example.com>
Date: Tue Jul 29 13:14:46 2014 -0400
Fixes #6: Force pushing after amending commits
Now we're switching back to main and 'accidentally' removing our branch.
(my-branch)$ git checkout main
Switched to branch 'main'
Your branch is up-to-date with 'origin/main'.
(main)$ git branch -D my-branch
Deleted branch my-branch (was 4e3cd85).
(main)$ echo oh noes, deleted my branch!
oh noes, deleted my branch!
At this point you should get familiar with 'reflog', an upgraded logger. It stores the history of all the action in the repo.
(main)$ git reflog
69204cd HEAD@{0}: checkout: moving from my-branch to main
4e3cd85 HEAD@{1}: commit: foo.txt added
69204cd HEAD@{2}: checkout: moving from main to my-branch
As you can see we have commit hash from our deleted branch. Let's see if we can restore our deleted branch.
(main)$ git checkout -b my-branch-help
Switched to a new branch 'my-branch-help'
(my-branch-help)$ git reset --hard 4e3cd85
HEAD is now at 4e3cd85 foo.txt added
(my-branch-help)$ ls
README.md foo.txt
Voila! We got our removed file back. git reflog
is also useful when rebasing goes terribly wrong.
To delete a remote branch:
(main)$ git push origin --delete my-branch
You can also do:
(main)$ git push origin :my-branch
To delete a local branch:
(main)$ git branch -d my-branch
To delete a local branch that has not been merged to the current branch or an upstream:
(main)$ git branch -D my-branch
Say you want to delete all branches that start with fix/
:
(main)$ git branch | grep 'fix/' | xargs git branch -d
To rename the current (local) branch:
(main)$ git branch -m new-name
To rename a different (local) branch:
(main)$ git branch -m old-name new-name
To delete the old-name
remote branch and push the new-name
local branch:
(main)$ git push origin :old_name new_name
First, fetch all branches from remote:
(main)$ git fetch --all
Say you want to checkout to daves
from the remote.
(main)$ git checkout --track origin/daves
Branch daves set up to track remote branch daves from origin.
Switched to a new branch 'daves'
(--track
is shorthand for git checkout -b [branch] [remotename]/[branch]
)
This will give you a local copy of the branch daves
, and any update that has been pushed will also show up remotely.
$ git push <remote> HEAD
If you would also like to set that remote branch as upstream for the current one, use the following instead:
$ git push -u <remote> HEAD
With the upstream
mode and the simple
(default in Git 2.0) mode of the push.default
config, the following command will push the current branch with regards to the remote branch that has been registered previously with -u
:
$ git push
The behavior of the other modes of git push
is described in the doc of push.default
.
You can set a remote branch as the upstream for the current local branch using:
$ git branch --set-upstream-to [remotename]/[branch]
# or, using the shorthand:
$ git branch -u [remotename]/[branch]
To set the upstream remote branch for another local branch:
$ git branch -u [remotename]/[branch] [local-branch]
By checking your remote branches, you can see which remote branch your HEAD is tracking. In some cases, this is not the desired branch.
$ git branch -r
origin/HEAD -> origin/gh-pages
origin/main
To change origin/HEAD
to track origin/main
, you can run this command:
$ git remote set-head origin --auto
origin/HEAD set to main
You've made uncommitted changes and realise you're on the wrong branch. Stash changes and apply them to the branch you want:
(wrong_branch)$ git stash
(wrong_branch)$ git checkout <correct_branch>
(correct_branch)$ git stash apply
You've made a lot of commits on a branch and now want to separate it into two, ending with a branch up to an earlier commit and another with all the changes.
Use git log
to find the commit where you want to split. Then do the following:
(original_branch)$ git checkout -b new_branch
(new_branch)$ git checkout original_branch
(original_branch)$ git reset --hard <sha1 split here>
If you had previously pushed the original_branch
to remote, you will need to do a force push. For more information check Stack Overlflow
You may have merged or rebased your current branch with a wrong branch, or you can't figure it out or finish the rebase/merge process. Git saves the original HEAD pointer in a variable called ORIG_HEAD before doing dangerous operations, so it is simple to recover your branch at the state before the rebase/merge.
(my-branch)$ git reset --hard ORIG_HEAD
Unfortunately, you have to force push, if you want those changes to be reflected on the remote branch. This is because you have changed the history. The remote branch won't accept changes unless you force push. This is one of the main reasons many people use a merge workflow, instead of a rebasing workflow - large teams can get into trouble with developers force pushing. Use this with caution. A safer way to use rebase is not to reflect your changes on the remote branch at all, and instead to do the following:
(main)$ git checkout my-branch
(my-branch)$ git rebase -i main
(my-branch)$ git checkout main
(main)$ git merge --ff-only my-branch
For more, see this SO thread.
Let's suppose you are working in a branch that is/will become a pull-request against main
. In the simplest case when all you want to do is to combine all commits into a single one and you don't care about commit timestamps, you can reset and recommit. Make sure the main branch is up to date and all your changes committed, then:
(my-branch)$ git reset --soft main
(my-branch)$ git commit -am "New awesome feature"
If you want more control, and also to preserve timestamps, you need to do something called an interactive rebase:
(my-branch)$ git rebase -i main
If you aren't working against another branch you'll have to rebase relative to your HEAD
. If you want to squash the last 2 commits, for example, you'll have to rebase against HEAD~2
. For the last 3, HEAD~3
, etc.
(main)$ git rebase -i HEAD~2
After you run the interactive rebase command, you will see something like this in your text editor:
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
pick b729ad5 fixup
pick e3851e8 another fix
# Rebase 8074d12..b729ad5 onto 8074d12
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
All the lines beginning with a #
are comments, they won't affect your rebase.
Then you replace pick
commands with any in the list above, and you can also remove commits by removing corresponding lines.
For example, if you want to leave the oldest (first) commit alone and combine all the following commits with the second oldest, you should edit the letter next to each commit except the first and the second to say f
:
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
f b729ad5 fixup
f e3851e8 another fix
If you want to combine these commits and rename the commit, you should additionally add an r
next to the second commit or simply use s
instead of f
:
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
s b729ad5 fixup
s e3851e8 another fix
You can then rename the commit in the next text prompt that pops up.
Newer, awesomer features
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# rebase in progress; onto 8074d12
# You are currently editing a commit while rebasing branch 'main' on '8074d12'.
#
# Changes to be committed:
# modified: README.md
#
If everything is successful, you should see something like this:
(main)$ Successfully rebased and updated refs/heads/main.
--no-commit
performs the merge but pretends the merge failed and does not autocommit, giving the user a chance to inspect and further tweak the merge result before committing. no-ff
maintains evidence that a feature branch once existed, keeping project history consistent.
(main)$ git merge --no-ff --no-commit my-branch
(main)$ git merge --squash my-branch
Sometimes you have several work in progress commits that you want to combine before you push them upstream. You don't want to accidentally combine any commits that have already been pushed upstream because someone else may have already made commits that reference them.
(main)$ git rebase -i @{u}
This will do an interactive rebase that lists only the commits that you haven't already pushed, so it will be safe to reorder/fix/squash anything in the list.
Sometimes the merge can produce problems in certain files, in those cases we can use the option abort
to abort the current conflict resolution process, and try to reconstruct the pre-merge state.
(my-branch)$ git merge --abort
This command is available since Git version >= 1.7.4
Say I have a main branch, a feature-1 branch branched from main, and a feature-2 branch branched off of feature-1. If I make a commit to feature-1, then the parent commit of feature-2 is no longer accurate (it should be the head of feature-1, since we branched off of it). We can fix this with git rebase --onto
.
(feature-2)$ git rebase --onto feature-1 <the first commit in your feature-2 branch that you don't want to bring along> feature-2
This helps in sticky scenarios where you might have a feature built on another feature that hasn't been merged yet, and a bugfix on the feature-1 branch needs to be reflected in your feature-2 branch.
To check if all commits on a branch are merged into another branch, you should diff between the heads (or any commits) of those branches:
(main)$ git log --graph --left-right --cherry-pick --oneline HEAD...feature/120-on-scroll
This will tell you if any commits are in one but not the other, and will give you a list of any nonshared between the branches. Another option is to do this:
(main)$ git log main ^feature/120-on-scroll --no-merges
If you're seeing this:
noop
That means you are trying to rebase against a branch that is at an identical commit, or is ahead of your current branch. You can try:
HEAD~2
or earlier instead
If you are unable to successfully complete the rebase, you may have to resolve conflicts.
First run git status
to see which files have conflicts in them:
(my-branch)$ git status
On branch my-branch
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
both modified: README.md
In this example, README.md
has conflicts. Open that file and look for the following:
<<<<<<< HEAD
some code
=========
some code
>>>>>>> new-commit
You will need to resolve the differences between the code that was added in your new commit (in the example, everything from the middle line to new-commit
) and your HEAD
.
If you want to keep one branch's version of the code, you can use --ours
or --theirs
:
(main*)$ git checkout --ours README.md
--ours
to keep changes from the local branch, or --theirs
to keep changes from the other branch.--theirs
to keep changes from the local branch, or --ours
to keep changes from the other branch. For an explanation of this swap, see this note in the Git documentation.If the merges are more complicated, you can use a visual diff editor:
(main*)$ git mergetool -t opendiff
After you have resolved all conflicts and tested your code, git add
the files you have changed, and then continue the rebase with git rebase --continue
(my-branch)$ git add README.md
(my-branch)$ git rebase --continue
If after resolving all the conflicts you end up with an identical tree to what it was before the commit, you need to git rebase --skip
instead.
If at any time you want to stop the entire rebase and go back to the original state of your branch, you can do so:
(my-branch)$ git rebase --abort
To stash all the edits in your working directory
$ git stash
If you also want to stash untracked files, use -u
option.
$ git stash -u
To stash only one file from your working directory
$ git stash push working-directory-path/filename.ext
To stash multiple files from your working directory
$ git stash push working-directory-path/filename1.ext working-directory-path/filename2.ext
$ git stash save <message>
or
$ git stash push -m <message>
First check your list of stashes with message using
$ git stash list
Then apply a specific stash from the list using
$ git stash apply "stash@{n}"
Here, 'n' indicates the position of the stash in the stack. The topmost stash will be position 0.
Furthermore, using a time-based stash reference is also possible.
$ git stash apply "stash@{2.hours.ago}"
You can manually create a stash commit
, and then use git stash store
.
$ git stash create
$ git stash store -m <message> CREATED_SHA1
To find a certain string which was introduced in any commit, you can use the following structure:
$ git log -S "string to find"
Commons parameters:
--source
means to show the ref name given on the command line by which each commit was reached.
--all
means to start from every branch.
--reverse
prints in reverse order, it means that will show the first commit that made the change.
To find all commits by author/committer you can use:
$ git log --author=<name or email>
$ git log --committer=<name or email>
Keep in mind that author and committer are not the same. The --author
is the person who originally wrote the code; on the other hand, the --committer
, is the person who committed the code on behalf of the original author.
To find all commits containing a specific file you can use:
$ git log -- <path to file>
You would usually specify an exact path, but you may also use wild cards in the path and file name:
$ git log -- **/*.js
While using wildcards, it's useful to inform --name-status
to see the list of committed files:
$ git log --name-status -- **/*.js
To trace the evolution of a single function you can use:
$ git log -L :FunctionName:FilePath
Note that you can combine this with further git log
options, like revision ranges and commit limits.
To find all tags containing a specific commit:
$ git tag --contains <commitid>
$ git clone --recursive git://github.com/foo/bar.git
If already cloned:
$ git submodule update --init --recursive
Creating a submodule is pretty straight-forward, but deleting them less so. The commands you need are:
$ git submodule deinit submodulename
$ git rm submodulename
$ git rm --cached submodulename
$ rm -rf .git/modules/submodulename
$ git checkout <branch-you-want-the-directory-from> -- <folder-name or file-name>
First find the commit when the file last existed:
$ git rev-list -n 1 HEAD -- filename
Then checkout that file:
git checkout deletingcommitid^ -- filename
$ git tag -d <tag_name>
$ git push <remote> :refs/tags/<tag_name>
If you want to recover a tag that was already deleted, you can do so by following these steps: First, you need to find the unreachable tag:
$ git fsck --unreachable | grep tag
Make a note of the tag's hash. Then, restore the deleted tag with following, making use of git update-ref
:
$ git update-ref refs/tags/<tag_name> <hash>
Your tag should now have been restored.
If someone has sent you a pull request on GitHub, but then deleted their original fork, you will be unable to clone their repository or to use git am
as the .diff, .patch URLs become unavailable. But you can checkout the PR itself using GitHub's special refs. To fetch the content of PR#1 into a new branch called pr_1:
$ git fetch origin refs/pull/1/head:pr_1
From github.com:foo/bar
* [new ref] refs/pull/1/head -> pr_1
$ git archive --format zip --output /full/path/to/zipfile.zip main
If there is a tag on a remote repository that has the same name as a branch you will get the following error when trying to push that branch with a standard $ git push <remote> <branch>
command.
$ git push origin <branch>
error: dst refspec same matches more than one.
error: failed to push some refs to '<git server>'
Fix this by specifying you want to push the head reference.
$ git push origin refs/heads/<branch-name>
If you want to push a tag to a remote repository that has the same name as a branch, you can use a similar command.
$ git push origin refs/tags/<tag-name>
(main)$ git mv --force myfile MyFile
(main)$ git fetch --all
(main)$ git reset --hard origin/main
(main)$ git rm --cached log.txt
Assuming the hash of the commit you want is c5f567:
(main)$ git checkout c5f567 -- file1/to/restore file2/to/restore
If you want to revert to changes made just 1 commit before c5f567, pass the commit hash as c5f567~1:
(main)$ git checkout c5f567~1 -- file1/to/restore file2/to/restore
Assuming you want to compare last commit with file from commit c5f567:
$ git diff HEAD:path_to_file/file c5f567:path_to_file/file
Same goes for branches:
$ git diff main:path_to_file/file staging:path_to_file/file
This works great for config templates or other files that require locally adding credentials that shouldn't be committed.
$ git update-index --assume-unchanged file-to-ignore
Note that this does not remove the file from source control - it is only ignored locally. To undo this and tell Git to notice changes again, this clears the ignore flag:
$ git update-index --no-assume-unchanged file-to-stop-ignoring
The git-bisect command uses a binary search to find which commit in your Git history introduced a bug.
Suppose you're on the main
branch, and you want to find the commit that broke some feature. You start bisect:
$ git bisect start
Then you should specify which commit is bad, and which one is known to be good. Assuming that your current version is bad, and v1.1.1
is good:
$ git bisect bad
$ git bisect good v1.1.1
Now git-bisect
selects a commit in the middle of the range that you specified, checks it out, and asks you whether it's good or bad. You should see something like:
$ Bisecting: 5 revision left to test after this (roughly 5 step)
$ [c44abbbee29cb93d8499283101fe7c8d9d97f0fe] Commit message
$ (c44abbb)$
You will now check if this commit is good or bad. If it's good:
$ (c44abbb)$ git bisect good
and git-bisect
will select another commit from the range for you. This process (selecting good
or bad
) will repeat until there are no more revisions left to inspect, and the command will finally print a description of the first bad commit.
On OS X and Linux, your git configuration file is stored in ~/.gitconfig
. I've added some example aliases I use as shortcuts (and some of my common typos) in the [alias]
section as shown below:
[alias]
a = add
amend = commit --amend
c = commit
ca = commit --amend
ci = commit -a
co = checkout
d = diff
dc = diff --changed
ds = diff --staged
extend = commit --amend -C HEAD
f = fetch
loll = log --graph --decorate --pretty=oneline --abbrev-commit
m = merge
one = log --pretty=oneline
outstanding = rebase -i @{u}
reword = commit --amend --only
s = status
unpushed = log @{u}
wc = whatchanged
wip = rebase -i @{u}
zap = fetch -p
day = log --reverse --no-merges --branches=* --date=local --since=midnight --author=\"$(git config --get user.name)\"
delete-merged-branches = "!f() { git checkout --quiet main && git branch --merged | grep --invert-match '\\*' | xargs -n 1 git branch --delete; git checkout --quiet @{-1}; }; f"
You can’t! Git doesn’t support this, but there’s a hack. You can create a .gitignore file in the directory with the following contents:
# Ignore everything in this directory
*
# Except this file
!.gitignore
Another common convention is to make an empty file in the folder, titled .gitkeep.
$ mkdir mydir
$ touch mydir/.gitkeep
You can also name the file as just .keep , in which case the second line above would be touch mydir/.keep
You might have a repository that requires authentication. In which case you can cache a username and password so you don't have to enter it on every push and pull. Credential helper can do this for you.
$ git config --global credential.helper cache
# Set git to use the credential memory cache
$ git config --global credential.helper 'cache --timeout=3600'
# Set the cache to timeout after 1 hour (setting is in seconds)
To find a credential helper:
$ git help -a | grep credential
# Shows you possible credential helpers
For OS specific credential caching:
$ git config --global credential.helper osxkeychain
# For OSX
$ git config --global credential.helper manager
# Git for Windows 2.7.3+
$ git config --global credential.helper gnome-keyring
# Ubuntu and other GNOME-based distros
More credential helpers can likely be found for different distributions and operating systems.
$ git config core.fileMode false
If you want to make this the default behaviour for logged-in users, then use:
$ git config --global core.fileMode false
To configure user information used across all local repositories, and to set a name that is identifiable for credit when review version history:
$ git config --global user.name “[firstname lastname]”
To set an email address that will be associated with each history marker:
git config --global user.email “[valid-email]”
So, you're screwed - you reset
something, or you merged the wrong branch, or you force pushed and now you can't find your commits. You know, at some point, you were doing alright, and you want to go back to some state you were at.
This is what git reflog
is for. reflog
keeps track of any changes to the tip of a branch, even if that tip isn't referenced by a branch or a tag. Basically, every time HEAD changes, a new entry is added to the reflog. This only works for local repositories, sadly, and it only tracks movements (not changes to a file that weren't recorded anywhere, for instance).
(main)$ git reflog
0a2e358 HEAD@{0}: reset: moving to HEAD~2
0254ea7 HEAD@{1}: checkout: moving from 2.2 to main
c10f740 HEAD@{2}: checkout: moving from main to 2.2
The reflog above shows a checkout from main to the 2.2 branch and back. From there, there's a hard reset to an older commit. The latest activity is represented at the top labeled HEAD@{0}
.
If it turns out that you accidentally moved back, the reflog will contain the commit main pointed to (0254ea7) before you accidentally dropped 2 commits.
$ git reset --hard 0254ea7
Using git reset
it is then possible to change main back to the commit it was before. This provides a safety net in case history was accidentally changed.
(copied and edited from Source).
Once you're comfortable with what the above commands are doing, you might want to create some shortcuts for Git Bash. This allows you to work a lot faster by doing complex tasks in really short commands.
alias sq=squash
function squash() {
git rebase -i HEAD~$1
}
Copy those commands to your .bashrc or .bash_profile.
If you are using PowerShell on Windows, you can also set up aliases and functions. Add these commands to your profile, whose path is defined in the $profile
variable. Learn more at the About Profiles page on the Microsoft documentation site.
Set-Alias sq Squash-Commits
function Squash-Commits {
git rebase -i HEAD~$1
}
Other Resources
Author: K88hudson
Source Code: https://github.com/k88hudson/git-flight-rules
License: CC-BY-SA-4.0 License